GUI(图形界面编程)

1.GUI:Graphical User Interface 图形用户接口  

  CLI:Command Line Interface 命令行用户接口,就是常见的dos命令操作

  Java AWT:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能,属于重量级控件。

  Javax.Swing:在AWT的基础上,建立的一套图形界面系统。

2.案例

 案例一:初试如何创建窗体、加按钮等

public static void main(String[] args) {
//        1.创建窗体对象
        Frame frame = new Frame("练习GUI");/* 可以直接写标题名字 new Frame("这里写标题"),也可以单独调用title方法设置 */

//        设置窗体属性(大小setSize、位置setLocation、颜色color、布局等)
        frame.setBounds(800, 300, 500, 500);

//        (窗体中组件的排列方式:流式布局FlowLayout、边界布局BorderLayout、网格布局GridLayout)
        frame.setLayout(new FlowLayout());

//        2.创建按钮对象
        JButton button = new JButton("click me");/* Button组件,是awt的,而awt对中文的支持不太好,所以标题乱码,建议用Jbutton解决 */
//        设置按钮大小
        button.setSize(40, 10);
//        把按钮添加到窗体
        frame.add(button);

//        用适配器实现关闭窗体
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

//        对窗体按钮事件处理
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("do it again!!!");
            }
        });

//        调用一个方法,让窗体可见
//        frame.show();/*show方法已经过时不在使用*/
        frame.setVisible(true);

    }

 案例二:

  

public static void main(String[] args) {
//        1.创建窗体对象
        Frame frame = new Frame("数据转移");/* 可以直接写标题名字 new Frame("这里写标题"),也可以单独调用title方法设置 */

//        设置窗体属性(大小setSize、位置setLocation、颜色color、布局等)
        frame.setBounds(800, 300, 500, 500);

//        (窗体中组件的排列方式:流式布局FlowLayout、边界布局BorderLayout、网格布局GridLayout)
        frame.setLayout(new FlowLayout());

//        创建文本框
        TextField tf = new TextField(20);
        JButton button = new JButton("数据转移");
//        创建文本域
        TextArea ta = new TextArea(10, 40);

//        把按钮添加到窗体
        frame.add(tf);
        frame.add(button);
        frame.add(ta);

//        对按钮做事件
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
//                获取文本框的值
                if (tf.getText().trim().equals("")) {
                    System.out.println("未监测到输入数据,请输入!");
                }
                String tf_one = tf.getText().trim();
//                清空数据
                tf.setText("");
//                赋值给文本域
                ta.append(tf_one + "\r\n");/* 追加和换行 */
//                获取光标
                tf.requestFocus();

            }
        });
//        设置窗体关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
//        设置窗体可见
        frame.setVisible(true);
    }

 案例三:

 

public static void main(String[] args) {
//        1.创建窗体对象
        Frame frame = new Frame("更换背景色");/* 可以直接写标题名字 new Frame("这里写标题"),也可以单独调用title方法设置 */

//        设置窗体属性(大小setSize、位置setLocation、颜色color、布局等)
        frame.setBounds(800, 300, 500, 500);

//        (窗体中组件的排列方式:流式布局FlowLayout、边界布局BorderLayout、网格布局GridLayout)
        frame.setLayout(new FlowLayout());

//        创建四个按钮
        JButton redButton = new JButton("红色");
        JButton blackButton = new JButton("黑色");
        JButton greenButton = new JButton("绿色");
        JButton buleButton = new JButton("蓝色");

//        把按钮添加到窗体
        frame.add(redButton);
        frame.add(blackButton);
        frame.add(greenButton);
        frame.add(buleButton);

//        对redButton鼠标添加点击事件
        redButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                frame.setBackground(Color.RED);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                frame.setBackground(Color.WHITE);
            }
        });
//        对blackButton鼠标添加进入事件
        blackButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                frame.setBackground(Color.black);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                frame.setBackground(Color.WHITE);
            }
        });
//        对greenButton鼠标添加进入事件
        greenButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                frame.setBackground(Color.green);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                frame.setBackground(Color.WHITE);
            }
        });
//        对buleButton鼠标添加进入事件
        buleButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                frame.setBackground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                frame.setBackground(Color.WHITE);
            }
        });

//        设置窗体关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
//        设置窗体可见
        frame.setVisible(true);
    }

 

 复习完毕!

 

posted @ 2019-11-29 08:26  忧桑の民工  阅读(790)  评论(0编辑  收藏  举报