欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GUI编程笔记

程序员文章站 2022-05-28 11:36:25
...

GUI编程

GUI简介

GUI(图形用户界面编程)的核心技术: Swing AWT

被淘汰的原因:

  • 因为界面不美观

  • 需要jre环境

AWT

GUI编程笔记

AWT介绍

  • 包含很多类和接口

  • 元素:窗口,按钮,文本框 …

  • java.awt 包使用

容器和组件

窗口 Frame

import java.awt.*;

public class TestFrame1 {
    public static void main(String[] args) {

        //实例化
        Frame frame = new Frame("我的第一个Frame!");

        //需要设置可见性
       frame.setVisible(true);

        //设置窗口的大小
        frame.setSize(400,400);

        //设置背景颜色 color
       frame.setBackground(Color.BLUE);

        //弹出的初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);

    }
}
import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
       
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.BLUE);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.YELLOW);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.GREEN);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.RED);

    }

}
class MyFrame extends Frame{
    //封装
    static  int id = 0;
    public MyFrame(int x, int y, int w, int h, Color color) {
        super("MyFrame + " + (++id));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);

    }
}

面板Panel

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Panel 可以看见是一个空间, 但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);

        //设置frame 坐标,宽高,颜色
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(136, 65, 198));

        //设置panel 坐标,宽高,颜色
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(139, 237, 73));

        //把panel 添加到frame 中
        frame.add(panel);

        ////设置可见性
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件  System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

布局管理器

  • 流式布局
import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //组件 - 按钮
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置流式布局
        //frame.setLayout(new FlowLayout()); //居中对齐
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左对齐
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT)); //右对齐

        //设置大小
        frame.setSize(200,200);

        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        //显示
        frame.setVisible(true);

    }
}
  • 东西南北中
import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {

        Frame frame = new Frame();

        //组件 - 按钮
        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");

        //把按钮添加到容器中
        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        //设置大小
        frame.setSize(200,200);

        //显示
        frame.setVisible(true);

    }
}

  • 表格布局

    import java.awt.*;
    
    public class TestGridLayout {
        public static void main(String[] args) {
            Frame frame = new Frame();
    
            //组件 - 按钮
            Button btn1 = new Button("btn1");
            Button btn2 = new Button("btn2");
            Button btn3 = new Button("btn3");
            Button btn4 = new Button("btn4");
            Button btn5 = new Button("btn5");
            Button btn6 = new Button("btn6");
    
            //设置布局
            frame.setLayout(new GridLayout(2,3)); //3行2列
    
            //设置大小
            //frame.setSize(200,200);
    
            //把按钮添加到容器
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            frame.add(btn4);
            frame.add(btn5);
            frame.add(btn6);
    
            frame.pack(); //Java 函数:自动填充
    
            //显示
            frame.setVisible(true);
    
        }
    }
    

    布局练习

    效果和思路:

GUI编程笔记

实习代码:

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestLayout {
    public static void main(String[] args) {

        //总 Frame
        Frame frame = new Frame();

        //设置窗口大小,位置,颜色
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.BLACK);

        //使用表格布局分成:2行1列
        frame.setLayout(new GridLayout(2,1));

        //显示
        frame.setVisible(true);

        //4个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,1));

        //上面
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2,BorderLayout.CENTER);


        //下面
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p3.add(new Button("West-2"),BorderLayout.WEST);

        //下面4个
        for (int i = 0; i < 4 ; i++) {
            p4.add(new Button("for-" + i));

        }

        p3.add(p4,BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);

        //监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}


总结

  1. Frame 是一个*窗口

  2. Panel 是一个面板,无法单独显示,必须加到容器中

  3. 布局管理器

    1. 流式

    2. 东西南北中

    3. 表格

事件监听

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestActionEvent {
    public static void main(String[] args) {

        Frame frame = new Frame();
        Button button = new Button("btn1");

        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();


        windowClose(frame);  //关闭窗口
        frame.setVisible(true);

    }


    private static void windowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}


//事件监听
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("我是按钮");
    }
}


输入框监听

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText01 {
    public static void main(String[] args) {
        //启动
        new MyFrame1();
    }
}

class MyFrame1 extends Frame{
    public MyFrame1() {
        TextField textField = new TextField();
        add(textField);

        //监听文本框输入的文字
        MyListener myListener = new MyListener();
        //按下enter,就会触发这个输入框的事件
        textField.addActionListener(myListener);

        //设置替换编码
        textField.setEchoChar('*');

        pack();
        setVisible(true);
    }
}

class MyListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();  //获得资源,返回一个对象
        System.out.println(field.getText()); //获得输入框的文本
        field.setText("");
    }
}

画笔

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {

        new MyPaint().loadFrame();
    }
}


class MyPaint extends Frame{

    public void loadFrame() {
        setBounds(200,200,600,500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画笔,需要颜色,画笔可以画画
        g.setColor(Color.red);
        //g.drawOval(100,100,100,100); //画空心圆
        g.fillOval(100,100,100,100); //画实心圆

        g.setColor(Color.blue);
        g.fillRect(150,200,200,200);  //画矩形

    }
}

鼠标监听

GUI编程笔记

实现用鼠标画画:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.security.cert.PolicyNode;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

//自己类
class MyFrame extends Frame{
    //画画需要画笔 ,需要监听鼠标当前的位置,需要集合存储这个点
    ArrayList points;

    public MyFrame(String title)  {
        super(title);
        setBounds(200,200,400,300);

        //存鼠标点击的点
        points = new ArrayList<>();

        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()) {
            Point point = (Point) iterator.next();
            g.setColor(Color.blue);
            g.fillOval(point.x,point.y,10,10);
        }
        
    }

    //添加一个点到界面上
    public void addPaint(Point point) {
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter {
        //鼠标 按下, 弹起, 按住不放

        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();

            //这个我们点击的时候,就会在界面上产生一个点
            //这个点就是鼠标的点
            frame.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标都需要话一遍
            frame.repaint(); //刷新

        }
    }

}

窗口监听

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();

    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        //addWindowListener(new MyWindowListener());

        this.addWindowListener(
            //匿名内部类
            new WindowAdapter() {
                //关闭窗口
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("windowClosing");
                    System.exit(0);
                }
                //**窗口
                @Override
                public void windowActivated(WindowEvent e) {
                    System.out.println("windowActivated");
                }
            }
        );

    }
}

键盘监听

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}


class KeyFrame extends Frame{
    public KeyFrame() {
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {

            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘下的键是哪个,当前的码
                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按下了上键");
                }
            }
        });
    }
}

Swing

窗口 JFrame

import javax.swing.*;
import java.awt.*;

public class JFrameDemo extends JFrame{
    public static void main(String[] args) {
        //创建一个窗口
        new JFrameDemo().init();
    }

    //init() 初始化
    public void init() {

        this.setVisible(true);
        this.setBounds(100,100,200,200);

        //设置文字 JLabel
        JLabel jLabel = new JLabel("我是JLabel");

        this.add(jLabel);

        //让文本标签居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.blue);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

弹窗 Dialog

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialogDemo extends JFrame{

    public DialogDemo(){
        this.setVisible(true);
        this.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 放东西 容器
        Container container = this.getContentPane();

        //绝对布局
        container.setLayout(null);

        //按钮
        JButton jButton = new JButton("点击弹出对话框");
        jButton.setBounds(30,30,200,50);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });

        container.add(jButton);

    }

    public static void main(String[] args) {

        new DialogDemo();
    }
}

//弹窗窗口
class MyDialog extends JDialog{
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);
        //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(null);
        
    }
}

面板 JPanel

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();

        container.setLayout(new GridLayout(2,1,10,10));

        JPanel jPanel = new JPanel(new GridLayout(1,3));

        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));

        container.add(jPanel);

        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }


    public static void main(String[] args) {
        new JPanelDemo();
    }
}

按钮

  • 单选框
import javax.swing.*;
import java.awt.*;

public class JButtonDemo extends JFrame {

    public JButtonDemo() {
        Container container = this.getContentPane();

        //单选框
        JRadioButton btn1 = new JRadioButton("btn1");
        JRadioButton btn2 = new JRadioButton("btn2");
        JRadioButton btn3 = new JRadioButton("btn3");

        //由于单选框只能选择一个,把他分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(btn1);
        group.add(btn2);
        group.add(btn3);

        container.add(btn1,BorderLayout.CENTER);
        container.add(btn2,BorderLayout.NORTH);
        container.add(btn3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
    }

    public static void main(String[] args) {

        new JButtonDemo();
    }
}

  • 多选按钮
import javax.swing.*;
import java.awt.*;

public class JButtonDemo extends JFrame {

    public JButtonDemo() {
        Container container = this.getContentPane();


        //多选框
        JCheckBox checkBox1 = new JCheckBox("box1");
        JCheckBox checkBox2 = new JCheckBox("box2");

        container.add(checkBox1,BorderLayout.SOUTH);
        container.add(checkBox2,BorderLayout.NORTH);


        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        new JButtonDemo();
    }
}

列表

  • 下拉框
package test;

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame {

    public Test() {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();

        status.addItem(null);
        status.addItem("1");
        status.addItem("2");
        status.addItem("3");

        container.add(status);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        new Test();
    }
}

文本框

  • 文本框

    package test;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Test extends JFrame {
    
        public Test() {
            Container container = this.getContentPane();
    
            JComboBox status = new JComboBox();
    
            status.addItem(null);
            status.addItem("1");
            status.addItem("2");
            status.addItem("3");
    
            container.add(status);
    
            this.setVisible(true);
            this.setSize(500,350);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
        }
    
        public static void main(String[] args) {
    
            new Test();
        }
    }
    
    
  • 密码框

package test;

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame {

    public Test() {
        Container container = this.getContentPane();


        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');

        container.add(jPasswordField);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        new Test();
    }
}

  • 文本域
package test;

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame {

    public Test() {
        Container container = this.getContentPane();


        //文本域
        JTextArea textArea = new JTextArea();
        textArea.setText("大杰");

        //Scroll 面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        
        container.add(scrollPane);

        this.setVisible(true);
        this.setSize(500,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        new Test();
    }
}

相关标签: gui