day10-java GUI编程

GUI编程

  • 这是什么
  • 它怎么玩
  • 该如何去在平时运用

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘
  • 破解工具

一、简介

GUI的核心类:AWT Swing

  • 界面不美观
  • 需要jre环境

为什么我们要学习

  1. 可以写出自己心中想要的一些小工具
  2. 工作时候,也可能需要维护到swing界面,概率极小
  3. 了解MVC架构,了解监听

二、AWT

1. awt介绍

包含了很多的类和接口,抽象窗口工具类

元素:窗口、按钮、文本框

java.awt.*

组件:button 容器(windows, pannel)

window(frame,dialog)

pannel(applet)

2.组件和容器

创建窗口

new Frame

需要设置可见性

frame.setVisible

设置窗口大小

frame.setSize()

设置背景颜色

frame.setColor

弹出的初始位置

frame.setLocation

设置大小固定

frame.setResizeable

public class MyFrame extends Frame {
    public static int id;
        public MyFrame(int x, int y, int w, int h,Color color){
            super("这是第"+ (id++)+"个Frame");
            setVisible(true);
            setBackground(color);
            setBounds(x,y,w,h);
            setResizable(false);
        }
}
public class Demo01 {
    public static void main(String[] args) {

        MyFrame myFrame1 = new MyFrame(100, 100, 300, 300, Color.BLACK);
        MyFrame myFrame2 = new MyFrame(400, 100, 300, 300, Color.RED);
        MyFrame myFrame3 = new MyFrame(100, 400, 300, 300, Color.BLUE);
        MyFrame myFrame4 = new MyFrame(400, 400, 300, 300, Color.GREEN);

    }
}

3. Panel

可以看成一个空间,不能单独存在

布局的概念

设置布局

frame.setLayout

设置坐标

frame.setBounds

坐标相对于frame

panel.setBounds

frame.add

frame.setvisible

监听事件,监听窗口关闭事件

frame.addWindowListener

适配器模式

new WindowAdapter()

4. 布局管理器

  • 流式布局

    flowLayout

  • 东西南北中

    borderLayout

  • 表格布局

    Grid

    frame.pack()

正常的方式

先构思

分析过程

代码实现

public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(100,100,300,300);
        frame.setBackground(Color.BLACK);
        frame.setLayout(new GridLayout(2,1));
        frame.setVisible(true);

        //面板
        Panel p1 = new Panel();
        Panel p2 = new Panel();
        p1.setLayout(new BorderLayout());
        p2.setLayout(new GridLayout(2,1));

        p1.add(new Button("btn-east-1"),BorderLayout.EAST);
        p1.add(new Button("btn-west-1"),BorderLayout.WEST);

        p2.add(new Button("btn-cent-1"));
        p2.add(new Button("btn-cent-2"));

        p1.add(p2,BorderLayout.CENTER);

        //下部
        Panel p3 = new Panel();
        Panel p4 = new Panel();
        p3.setLayout(new BorderLayout());
        p4.setLayout(new GridLayout(2,2));

        p3.add(new Button("btn-east-2"),BorderLayout.EAST);
        p3.add(new Button("btn-west-2"),BorderLayout.WEST);

        for (int i = 0; i < 4; i++) {
            p4.add(new Button("btn"+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);
            }
        });

    }

image-20220421121716705

总结

  1. Frame是一个顶级窗口
  2. Panel 无法单独显示,必须添加到某个容器中
  3. 布局管理器
    • 流式
    • 东西南北中
    • 表格
  4. 大小、定位、背景颜色,监听,可见性

5.监听事件

当某个事情发生的时候干什么

按下按钮的时候触发一些事件

Button

因为需要,所以创建

两个按钮,实现同一个监听

可以显式的定义触发会返回的命令

如果不显式定义,则会返回默认的值

可以多个按钮只写一个监听类

6. 输入框TextField监听

获得一些资源,返回的一个对象

获得输入框的文本

按下回车就会触发这个输入框的事件

setEchoChar('*')

回车后清空

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

        windowsClose(new TestFrame());
    }

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

class TestFrame extends Frame{
    public TestFrame(){
        setBounds(100,100,200,200);
        setBackground(Color.BLACK);
        setVisible(true);


        TextField textField = new TextField();
        textField.setEchoChar('*');
        add(textField);
        pack();
        MyActionListener myActionListener = new MyActionListener();
        textField.addActionListener(myActionListener);
    }

}


class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();
        System.out.println(textField.getText());
        textField.setText("");
    }
}

7. 简易计算器

组合,大于继承

内部类

基本类+监听类

完全改造为面向对象

内部类最大的好处就是畅通无阻的去访问外部类

组合类写法

class Calculator extends Frame{

    public TextField t1,t2,t3;
    private Label label;
    private Button button;

    public void loadCalc(){
        //三个文本
         t1 = new TextField(10);
         t2 = new TextField(10);
         t3 = new TextField(20);

        //一个标签
         label = new Label("+");

        //一个按钮
        button = new Button("=");
        button.addActionListener(new MyCalculatorLisitener(this));

        //布局
        setLayout(new FlowLayout());
        add(t1);
        add(label);
        add(t2);
        add(button);
        add(t3);
        pack();
        setVisible(true);
    }


}

class MyCalculatorLisitener implements ActionListener{

    private Calculator calculator = null;
    public MyCalculatorLisitener(Calculator calculator){
        this.calculator = calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String n1 = calculator.t1.getText();
        String n2 = calculator.t2.getText();
        if (!n1.isEmpty() && !n2.isEmpty()) {
            int sum = Integer.valueOf(n1) + Integer.valueOf(n2);
            calculator.t3.setText(String.valueOf(sum));
        }
        calculator.t1.setText("");
        calculator.t2.setText("");
    }
}

内部类写法

class Calculator extends Frame{

    private TextField t1,t2,t3;
    private Label label;
    private Button button;

    public void loadCalc(){
        //三个文本
         t1 = new TextField(10);
         t2 = new TextField(10);
         t3 = new TextField(20);

        //一个标签
         label = new Label("+");

        //一个按钮
        button = new Button("=");
        button.addActionListener(new MyCalculatorLisitener());

        //布局
        setLayout(new FlowLayout());
        add(t1);
        add(label);
        add(t2);
        add(button);
        add(t3);
        pack();
        setVisible(true);
    }

    class MyCalculatorLisitener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String n1 = t1.getText();
            String n2 = t2.getText();
            if (!n1.isEmpty() && !n2.isEmpty()) {
                int sum = Integer.valueOf(n1) + Integer.valueOf(n2);
                t3.setText(String.valueOf(sum));
            }
            t1.setText("");
            t2.setText("");
        }
    }

}

8、 画笔paint

重载paint

画笔要有颜色,可以画画

画笔用完将他还原原来的颜色

9、鼠标监听

实现鼠标画画

画画需要画笔,需要监听鼠标当前的位置 ,需要集合来存储这个点

鼠标监听器,针对窗口

每次点击鼠标都要重画一遍

repaint()刷新

class MyPaint extends Frame {

    private ArrayList points;
    public MyPaint(String title){
        super(title);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.RED);
//        g.drawRect(50,50,100,100);
//        g.drawOval(100,100,100,100);
//        g.fillOval(200,200,100,100);
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.fillOval(point.x,point.y,10,10);
        }
        g.setColor(Color.black);

    }

    public void loadStart(){

        points = new ArrayList();
        setBounds(100,100,600,400);

        addMouseListener(new MyMouseListener());
        setVisible(true);
    }

    class MyMouseListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            Point point = new Point(e.getX(), e.getY());
            points.add(point);
            //重新画
            repaint();
        }
    }
}

10、 窗口监听

setVisiable(false)隐藏窗口

窗口关闭

窗口激活

11 、键盘监听

KeyAdapter

getKeyCode

class MyKeyFrame extends Frame{

    public MyKeyFrame(){
        setBounds(100,100,200,200);
        setVisible(true);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println(e.getKeyCode());
                if (e.getKeyCode() == KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }
            }
        });
    }

}

三、swing

1、窗口

JFrame

JLabel

public class Demo11 {
    public static void main(String[] args) {
        MyJFrame myJFrame =  new MyJFrame("paint");
        myJFrame.init();
        myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class MyJFrame extends JFrame{

    private JLabel label;

    public MyJFrame(String title){
        super(title);
    }

    public void init(){
        setBounds(100,100,300,300);
        setBackground(Color.BLUE);
        label = new JLabel("标签");
        add(label);

        setVisible(true);
    }
}

弹窗的窗口

容器

布局

按钮

弹窗JDialog

默认就有关闭事件

class MyJFrame extends JFrame{

    private JLabel label;

    private JButton button;

    public MyJFrame(String title){
        super(title);
    }

    public void init(){
        setBounds(100,100,300,300);
        setBackground(Color.BLUE);
        label = new JLabel("标签");
        button = new JButton("打开弹框");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按钮按下");
                new MyDialog();
            }
        });
        setLayout(new FlowLayout());
        add(label);
        add(button);
        setVisible(true);
    }

    class MyDialog extends JDialog{
        public MyDialog() {
            this.setBounds(200,200,100,100);
            this.setBackground(Color.BLUE);
            this.setVisible(true);

            Container container = this.getContentPane();
            container.setLayout(new FlowLayout());
            container.add(new JLabel("弹框"));
        }
    }
}

2、标签

3、图标

图标需要实现类,是接口

获取图片的地址

ImageIcon.class.getResource();

label 可以放图标和图片

class MyImageIcon extends JFrame{

    public void init(){
        setBounds(100,100,300,300);
        setLayout(new FlowLayout());
        URL url = MyImageIcon.class.getResource("test.png");

        JLabel jLabel = new JLabel();
        jLabel.setIcon(new ImageIcon(url));
        add(jLabel);

        setVisible(true);
    }
}

4、面板

JScrollPanel

文本域

JTextArea

class MyJFrame extends JFrame{

    public void init(){
        setBounds(100,100,400,400);
        setBackground(Color.BLUE);
        setLayout(new FlowLayout());

//        add(new JTextArea(10,20));
        JTextArea jTextArea = new JTextArea(20, 20);
        JScrollPane jScrollPane = new JScrollPane(jTextArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        add(jScrollPane);


        pack();
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

5、按钮

将图片变成图标

把图标放在按钮上

setToolTipText

单选按钮

JRadioButton

分组中选择一个

ButtonGroup

复选按钮

JCheckBox

class TestJFrame extends JFrame{

    public void init(){
        setBounds(100,100,400,400);
        setLayout(new FlowLayout());

        URL resource = TestJFrame.class.getResource("test.png");

        ImageIcon imageIcon = new ImageIcon(resource);
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("点我呀");
        add(jButton);

        JRadioButton btn1 = new JRadioButton("btn1");
        JRadioButton btn2 = new JRadioButton("btn2");
        JRadioButton btn3 = new JRadioButton("btn3");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(btn1);
        buttonGroup.add(btn2);
        buttonGroup.add(btn3);

        add(btn1);
        add(btn2);
        add(btn3);

        JCheckBox btn11 = new JCheckBox("btn1");
        JCheckBox btn12 = new JCheckBox("btn2");

        add(btn11);
        add(btn12);

        JToggleButton open = new JToggleButton("open",true);
        add(open);


        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

6、 列表

下拉框

JComboBox

addItem

列表框

Vector

JList

选择地区

class TestJFrame extends JFrame{

    public void init(){
        setBounds(100,100,400,400);
        setLayout(new FlowLayout());

        Container contentPane = this.getContentPane();
       
        JComboBox<Object> objectJComboBox = new JComboBox<>();
        objectJComboBox.addItem("");
        objectJComboBox.addItem("星期一");
        objectJComboBox.addItem("星期二");
        objectJComboBox.addItem("星期三");
        objectJComboBox.addItem("星期四");
        objectJComboBox.addItem("星期五");


        Vector<String> strings = new Vector<>();
        JList<String> stringJList = new JList<>(strings);
        strings.add("aaa");
        strings.add("bbb");
        strings.add("acccaa");
        strings.add("adddaa");
        strings.add("aadddeeea");

        contentPane.add(stringJList);
        contentPane.add(objectJComboBox);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

7、文本框

文本框 ,密码框,文本域

JTextField

JPasswordField

JTextArea

posted @ 2022-04-23 17:43  LukeZhang_sz  阅读(37)  评论(0)    收藏  举报