码迷,mamicode.com
首页 > Windows程序 > 详细

Swing

时间:2016-05-04 01:04:05      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:

Swing是轻量级组件,用于开发Java GUI应用程序

javax.swing 包
    常见容器:能够容纳其他Swing组件的一种组件,对其包含的组件布局
    1.JFrame
        创建窗口,包含边框、标题和用于关闭和图标化窗口的按钮
    2.JPanel
        中间容器
        将组件组合在一起
        与内容窗格相似,面板在默认情况下不透明
    3.JScrollPane
        管理视区、滚动条及可选的行和列的标题视区

========================Swing GUI组件==========================
置于用户界面,使其显示或者改变大小
1.JLable
    方法                    说明
    JLabel()                创建一个默认的 JLabel 实例
    JLabel(String text)        以指定的文本创建 JLabel 实例
    JLabel(Icon image)         以指定的图像创建 JLabel 实例
    getText()                 返回标签显示的文本字符串
    setIcon(Icon icon)         定义 标签将显示的内容
    setText(String text)     定义此组件将要显示的单行文本
    
2.JTextField
    方法                                说明
    JTextField()                         构造一个新的文本输入框  
    JTextField(String text)             构造一个新的文本输入框 ,以指定文本作为初始文本
    getColumns()                        返回文本字段中的列数
    setColumns(int columns)                设置文本字段中的列数,然后使布局无效
    setHorizontalAlignment(int value)    设置文本字段中文本的水平对齐方式:LEFT\CENTER\RIGHT
    
3.JTextArea
    方法                    说明
    JTextArea()             构造一个新的文本区
    JTextArea(String text)     用指定的显示文本构造一个新的文本区
    setFont(Font f)            设置文本区的字体
    getText()                 获取文本区中的文本字符串
    
4.JButton
    方法                                说明
    JButton( )                             创建不带文本和图标的按钮
    JButton(Icon icon)                     创建带图标的按钮
    JButton(String text)                 创建带文本的按钮
    JButton(String text, Icon icon)     创建带文本和图标的按钮
    setRolloverIcon(Icon img)            当鼠标经过时,显示指定的图标
    setSelectedIcon(Icon img)            当选择按钮时,显示 img 指定的图标

5.JCheckBox
    方法                                        说明
    JCheckBox( )                                 创建初始非选中的复选框,且不带文本或图标
    JCheckBox(String text)                         用指定的文本创建初始非选中的复选框。
    JCheckBox(String text, boolean selected)     用指定文本创建一个复选框,并指定是否初始选中该复选框

6.RadioButton
    方法                                            说明
    JRadioButton()                                     创建初始非选中的单选按钮,不设置其文本
    JRadioButton(String text)                         用指定的文本创建非选中的单选按钮
    JRadioButton(String text, boolean selected)     用指定的文本和选择状态创建单选按钮

7.JComboBox
    方法                    说明
    JcomboBox()                创建一个下拉框实例
    addItem(Object obj)        将项添加至项的列表
    getItemAt(int index)     返回指定索引位置的列表项
    getItemCount()            返回列表(作为对象)中的项数
    getSelectedItem()        将当前选择的项作为一个对象返回
    getSelectedIndex()        返回当前选择项的索引位置

================================事件处理程序==========================
Event 类        接口
ActionEvent        ActionListener
AdjustmentEvent    AdjustmentListener
ComponentEvent    ComponentListener
FocusEvent        FocusListener
ItemEvent        ItemListener
WindowEvent        WindowListener
TextEvent        TextListener
MouseEvent        MouseListener, MouseMotionListener
KeyEvent        KeyListener

================================布局管理器==========================
1.实现java.AWT.LayoutManager接口,帮助在容器中放置组件
2.orderLayout\FlowLayout\GridLayout

JavaGUI实例

 1 public abstract class CenterFrame extends JFrame{
 2     /** 获得系统默认工具箱,用来获得当前屏幕的分辨率 */    
 3     private static Toolkit toolkit = Toolkit.getDefaultToolkit();
 4 
 5     public CenterFrame(int width,int height){
 6         this.setSize(width, height);
 7         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 8         //1、获得屏幕的分辨率
 9         Dimension screenSize = toolkit.getScreenSize();
10         //2.根据计算,设置当前面板的位置
11         this.setLocation(
12                 ((int)screenSize.getWidth() - this.getWidth())/2,
13                 ((int)screenSize.getHeight() - this.getHeight())/2
14         );
15     }
16 }

 

 1 public class ImagesDemo extends CenterFrame{
 2     private JLabel lblImage = new JLabel();
 3     
 4     public ImagesDemo(int width, int height) {
 5         super(width, height);
 6         
 7         //实例化图像对象
 8         ImageIcon image = new ImageIcon("images/PersonalInfohead.jpg");
 9         //为标签设置图像
10         lblImage.setIcon(image);
11         //将标签添加到内容面板
12         JPanel contentPane = (JPanel) getContentPane();
13         contentPane.setLayout(null);
14         //设置图像标签的位置和大小
15         lblImage.setBounds(0, 0, 299, 30);
16         //在内容面板上添加图像标签
17         contentPane.add(lblImage);
18         
19         this.setVisible(true);
20     }
21     
22     public static void main(String[] args) {
23         new ImagesDemo(500, 400);
24     }
25 }

 

 1 public class JComboBoxDemo extends CenterFrame{
 2     private JComboBox<String> cbCity = new JComboBox<String>(
 3             new String[]{"北京","上海","广州","成都","深圳"}
 4     );
 5     
 6     public JComboBoxDemo(int width, int height) {
 7         super(width, height);
 8         initComponent();
 9         setVisible(true);
10     }
11     
12     /**
13      * 初始化组件方法
14      */
15     private void initComponent(){
16         JPanel contentPan = (JPanel) getContentPane();
17         contentPan.setLayout(null);
18         cbCity.setBounds(20, 20, 100, 25);
19         contentPan.add(cbCity);
20         
21         //事件处理
22         cbCity.addActionListener(new ActionListener() {
23             @Override
24             public void actionPerformed(ActionEvent e) {
25                 JOptionPane.showMessageDialog(null, cbCity.getSelectedItem());
26             }
27         });
28     }
29     
30     public static void main(String[] agrs) {
31         new JComboBoxDemo(500, 400);
32     }
33 }
34 
35 public class JLableDemo extends JFrame {
36     private JLabel lblQQId1 = new JLabel("QQ号码:");
37     /*private JLabel lblQQId2 = new JLabel("QQ号码:");
38     private JLabel lblQQId3 = new JLabel("QQ号码:");
39     private JLabel lblQQId4 = new JLabel("QQ号码:");
40     private JLabel lblQQId5 = new JLabel("QQ号码:");*/
41     private JTextField txtqq = new JTextField(25);
42     private JButton btnLogin = new JButton("登录");
43     private JButton btnCancel = new JButton("取消");
44 
45     public JLableDemo() {
46         // 设置窗体默认关闭的操作
47         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48         // 设置窗体标题
49         this.setTitle("测试窗体");
50         // 设置窗体大小
51         this.setSize(500, 400);
52         // 设置窗体的显示状态
53         this.setVisible(true);
54         
55         // 获得内容面板,以便在内容面板上添加组件
56         JPanel contentPan = (JPanel) this.getContentPane();
57         // 将内容面板设置为无布局,方便控制组件的位置
58         contentPan.setLayout(null);
59         // 设置标签课件的x,y,宽,高
60         lblQQId1.setBounds(80, 80, 80, 15);
61         txtqq.setBounds(lblQQId1.getX() + lblQQId1.getWidth(), lblQQId1.getY() - 5, 150, 25);
62         btnLogin.setBounds(80, 180, 60, 30);
63         btnCancel.setBounds(260, 180, 60, 30);
64         
65         contentPan.add(lblQQId1);
66         contentPan.add(txtqq);
67         contentPan.add(btnLogin);
68         contentPan.add(btnCancel);
69         
70         //为按钮添加事件处理
71         btnLogin.addActionListener(new ActionListener() {
72             @Override
73             public void actionPerformed(ActionEvent e) {
74                 //当点击btnLogin按钮时,本方法自动调用
75                 System.out.println(txtqq.getText());
76             }
77         });
78         
79         /*
80          * contentPan.add(lblQQId1,BorderLayout.EAST);
81          * contentPan.add(lblQQId2,BorderLayout.WEST);
82          * contentPan.add(lblQQId3,BorderLayout.NORTH);
83          * contentPan.add(lblQQId4,BorderLayout.SOUTH);
84          * contentPan.add(lblQQId5,BorderLayout.CENTER);
85          */
86     }
87 
88     public static void main(String[] args) {
89         new JLableDemo();
90     }
91 }

 

 1 public class JScrollPaneDemo extends CenterFrame{
 2     private JTextArea txtContent = new JTextArea("协议内容...");
 3 //    private JCheckBox chkFav= new JCheckBox("篮球");
 4     public JScrollPaneDemo(int width, int height) {
 5         super(width, height);
 6         
 7         //为多行文本框添加滚动面板--固定代码
 8         JScrollPane scrollPane = new JScrollPane();
 9         //将文本框控件添加到滚动面板组件上
10         scrollPane.getViewport().add(txtContent);
11         //将滚动面板添加到内容面板上
12         getContentPane().add(scrollPane);
13         
14         /*//获得复选框是否被选中
15         chkFav.isSelected();    
16         getContentPane().add(chkFav);*/
17         
18         this.setVisible(true);
19     }
20 
21     public static void main(String[] args) {
22         new JScrollPaneDemo(500, 400);
23     }
24 }

 

 1 /**
 2  * 用来演示Java面板的基本用法
 3  * @author Administrator
 4  * extends JFrame 可以关掉窗口
 5  * @author Administrator
 6  * extends Frame  不可以关掉窗口
 7  */
 8 public class MainFrame extends JFrame{
 9     //当前登录用户的qq号码
10     private String currLoginId = null;
11     
12     public MainFrame(String currLoginId){
13         if (currLoginId == null) {
14             this.currLoginId = "1000";
15         } else {
16             this.currLoginId = currLoginId;
17         }
18         
19         //设置窗体默认关闭的操作
20         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21         //设置窗体标题
22         this.setTitle("测试窗体");
23         //设置窗体大小
24         this.setSize(500, 400);
25         //设置窗体的显示状态
26         this.setVisible(true);
27     }
28 }

 








    

    
    
   

Swing

标签:

原文地址:http://www.cnblogs.com/ivy-xu/p/5456942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!