Java操作应用——Java Swing的简单示例
有了Java的swing,你便可以编写GUI应用了。Java所提供的javax包中就包含了swing。使用swing来编写GUI程序首先需要继承下JFrame。然后在里面添加Box,然后便可以往里面添加诸如按钮,多选按钮,文本框等控件了。这些Box是放在Container的最外层的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.awt.*; import javax.swing.*; public class SwingsDemo extends JFrame { public SwingsDemo() { String path = "//home//user//Documents//images" ; Container contentPane = getContentPane(); contentPane.setLayout( new FlowLayout()); Box myHorizontalBox = Box. createHorizontalBox(); Box myVerticleBox = Box. createVerticalBox(); myHorizontalBox.add( new JButton( "My Button 1" )); myHorizontalBox.add( new JButton( "My Button 2" )); myHorizontalBox.add( new JButton( "My Button 3" )); myVerticleBox.add( new JButton( new ImageIcon(path + "//Image1.jpg" ))); myVerticleBox.add( new JButton( new ImageIcon(path + "//Image2.jpg" ))); myVerticleBox.add( new JButton( new ImageIcon(path + "//Image3.jpg" ))); contentPane.add(myHorizontalBox); contentPane.add(myVerticleBox); pack(); setVisible( true ); } public static void main(String args[]) { new SwingsDemo(); } } |
点击加载更多评论>>