Java GUI

Posted 风影旋新月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java GUI相关的知识,希望对你有一定的参考价值。

1. GUI

Graphical user interface(图形用户接口):用图形的方式,来显示计算机操作的界面,这样更加直观

CLI:command line User interface(命令行用户接口):常用的Dos命令行操作,需要记住一些常用的命令,操作不直观

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

javax.swing:在AWT的基础上,建立的一套图形界面系统,提供了很多的组件,而且完全由java实现,增加了移植性,属于轻量级控件

2. Frame

public void setTitle(String s):设置窗体的标题

public void setSize(width, height):设置窗体的大小

public void setLocation(x, y):设置窗体的区域

public void setBounds(x, y, width, height):同时设置窗体的大小和所在区域

public void setVisible(true):设置窗体是否可见

public class GUIDemo {
    public static void main(String[] args){
        Frame f = new Frame();
        // Frame f = new Frame("窗口标题");
        
        f.setTitle("frame");    // 设置窗体标题
        f.setSize(400, 300);    // 单位:像素,设置窗体的大小
        /* 等同于:
         * Dimension d = new Dimension(400,300);
         * f.setSize(400, 300);
         */
        f.setLocation(400,200);    // 设置窗体所在的区域
        /* 等同于
         * Point p = new Point(400, 200);
         * f.setLocation(p);
         */
        
        /*
         * f.setBounds(x, y, width, height);
         * 等同于:
         * f.setLocation(x, y);
         * f.setSize(width, height);
         */
        
        f.setVisible(true);
    }
}

 

3. 事件监听机制

事件源:事件发生的地方

事件:要发生的事情

事件处理:针对发生的事情做出的处理方案

事件监听:把事件源和事件联系起来

public class GUIDemo {
    public static void main(String[] args){
        Frame f = new Frame();
        // Frame f = new Frame("窗口标题");
        
        f.setTitle("frame");    // 设置窗体标题
        f.setSize(400, 300);    // 单位:像素,设置窗体的大小
        /* 等同于:
         * Dimension d = new Dimension(400,300);
         * f.setSize(400, 300);
         */
        f.setLocation(400,200);    // 设置窗体所在的区域
        /* 等同于
         * Point p = new Point(400, 200);
         * f.setLocation(p);
         */
        
        /*
         * f.setBounds(x, y, width, height);
         * 等同于:
         * f.setLocation(x, y);
         * f.setSize(width, height);
         */
        f.addWindowListener(new WindowListener(){

            @Override
            public void windowOpened(WindowEvent e) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void windowIconified(WindowEvent e) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void windowActivated(WindowEvent e) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                // TODO Auto-generated method stub
                
            }
            
        });
        f.setVisible(true);
    }
}

 

4. 适配器设计模式

接口的方法比较多,也得把其他的实现也提供了,即使是空实现

解决方案:接口(方法比较多)------>适配器(实现接口,空实现)------->实现类(用哪个重写哪个)

针对上例的改进版:

f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

 例:文本框的数据转移到文本域

public class FrameDemo {
    public static void main(String[] args){
        Frame f = new Frame("数据转移");
        f.setBounds(400, 200, 400, 300);
        f.setLayout(new FlowLayout());
        
        final TextField tf = new TextField(20);
        Button bu = new Button("heoo");
        final TextArea ta = new TextArea(10, 40);
        
        f.add(tf);
        f.add(bu);
        f.add(ta);
        
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        
        bu.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String tf_str = tf.getText().trim();
                tf.setText("");
                // ta.setText(tf_str);;
                ta.append(tf_str + "\\r\\n");
                tf.requestFocus();   // 获取光标
            }
            
        });
        
        f.setVisible(true);
    }
}

效果图:

 例:颜色变换

public class ColorTranverce {
    public static void main(String[] args) {
        final Frame f = new Frame();
        f.setBounds(400, 200, 400, 300);
        f.setLayout(new FlowLayout());

        Button redButton = new Button("red");

        /*
         * 按钮的动作实现更改背景色 redButton.addActionListener(new ActionListener(){
         * 
         * @Override public void actionPerformed(ActionEvent e) { // TODO
         * Auto-generated method stub f.setBackground(Color.red); }
         * 
         * });
         */

        // 对按钮田间鼠标点击事件
        redButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                f.setBackground(Color.red);
            }
            
            @Override
            public void mouseEntered(MouseEvent e){
                f.setBackground(Color.gray);
            }
        });

        f.add(redButton);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        f.setVisible(true);
    }
}

文本框只能输入数字和字符案例

public class NumbericDemo {
    public static void main(String[] args){
        Frame f = new Frame("number");
        f.setBounds(400, 200, 400, 300);
        f.setLayout(new FlowLayout());
        Label label = new Label("please input your qq number");
        TextField tf = new TextField(40);
        
        f.add(label);
        f.add(tf);
        
        tf.addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e){
                char ch = e.getKeyChar();
                if(!(ch >= \'0\' && ch <= \'9\')){
                    e.consume();    // 事件取消
                }
            }
        });
        
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        
        f.setVisible(true);
    }
}

 

5. 菜单组件

5.1 单级菜单

public class MenuDemo {
    public static void main(String[] args){
        Frame f = new Frame("单极菜单");
        f.setBounds(400, 200, 400, 300);
        f.setLayout(new FlowLayout());
        
        MenuBar mb = new MenuBar();
        Menu m = new Menu("file");
        MenuItem mi = new MenuItem("exit");
        
        m.add(mi);
        mb.add(m);
        
        f.setMenuBar(mb);
        
        mi.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
            
        });
        
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        
        f.setVisible(true);
    }
}

5.2 多级菜单

public class MenuDemo {
    public static void main(String[] args){
        final Frame f = new Frame("title");        // 匿名内部类调用的变量必须使用final修饰
        f.setBounds(400, 200, 400, 300);
        f.setLayout(new FlowLayout());
        
        MenuBar mb = new MenuBar();
        Menu m1 = new Menu("file");
        Menu m2 = new Menu("change title");
        
        final MenuItem mi1 = new MenuItem("t");
        final MenuItem mi2 = new MenuItem("g");
        MenuItem mi3 = new MenuItem("return titile");
        MenuItem mi4 = new MenuItem("open notepad");
        MenuItem mi5 = new MenuItem("exit");
        
        final String name = f.getTitle();
        
        m2.add(mi1);
        m2.add(mi2);
        m2.add(mi3);
        
        m1.add(m2);
        m1.add(mi4);
        m1.add(mi5);
        
        mb.add(m1);
        
        f.setMenuBar(mb);
        
        mi1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                f.setTitle(mi1.getLabel());
            }
            
        });
        
        mi2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                f.setTitle(mi2.getLabel());
            }
            
        });
        
        mi3.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                f.setTitle(name);
            }
            
        });
        
        mi4.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                Runtime rt = Runtime.getRuntime();
                try {
                    rt.exec("notepad");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            
        });
        
        mi5.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }
            
        });
        
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        
        f.setVisible(true);
    }
}

效果图:

 

以上是关于Java GUI的主要内容,如果未能解决你的问题,请参考以下文章

java代码在片段活动中不起作用

java 代码片段【JAVA】

# Java 常用代码片段

# Java 常用代码片段

创建片段而不从 java 代码实例化它

如何重构这个 Java 代码片段