Java——GUI编程


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

1、简介

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

不流行:界面不美观,需要jre环境。

学习GUI:可以写一些小工具,维护swing的界面,了解MVC架构,了解监听。

2、AWT(抽象的窗口工具)

AWT简介

1.包含了很多类和接口
2.元素:窗口,按钮,文本框
3.java.awt
在这里插入图片描述

1、Frame的使用:

import java.awt.*;

public class Solution {
    public static void main(String[] args) {
        //Frame.JDK  看源码
        Frame frame = new Frame("我的第一个Java图形界面窗口");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //设置背景颜色
        frame.setBackground(new Color(104, 42, 42));
        //弹出初始位置
        frame.setLocation(200,400);
        //设置窗口大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述
问题:窗口无法关闭

设置多个窗口:

import java.awt.*;

public class Solution {
    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.pink);
        MyFrame myFrame4= new MyFrame(300, 300, 200, 200, Color.pink);
    }
}

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);
    }
}

在这里插入图片描述

2、面板 panel

在容器里边,不能单独存在,需要放在frame上。

import java.awt.*;

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

        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);
        frame.setBounds(300, 300, 400, 400);
        //frame.setBackground(new Color(239, 217, 7, 119));

        //panel设置坐标,相对于Frame
        panel.setBounds(50, 50, 200, 200);
        panel.setBackground(new Color(199, 20, 20));

        frame.add(panel);
        frame.setVisible(true);
    }
}

在这里插入图片描述
监听事件关闭:退出

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

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

        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);
        frame.setBounds(300, 300, 400, 400);
        //frame.setBackground(new Color(239, 217, 7, 119));

        //panel设置坐标,相对于Frame
        panel.setBounds(50, 50, 200, 200);
        panel.setBackground(new Color(199, 20, 20));

        frame.add(panel);
        frame.setVisible(true);
        //关闭事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

3、布局管理器

3.1 流式布局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Solution {
    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.setBounds(200, 200, 300, 300);
        //把按钮添加进去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}

在这里插入图片描述

3.2 东西南北中

在这里插入图片描述

package demopacket;

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

public class Solution {
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        //设置为东西南北中布局
        frame.setLayout(new BorderLayout());
        frame.setBounds(200, 200, 300, 300);
        //把按钮添加进去
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}

在这里插入图片描述

3.3 表格布局
package demopacket;

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

public class Solution {
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        //设置为表格布局
        frame.setLayout(new GridLayout(3, 2));
        frame.setBounds(200, 200, 300, 300);
        //把按钮添加进去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        //frame.pack();//java函数:作用 自动布局,选择一个最好的
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}

在这里插入图片描述

4、事件监听

当事情发生的时候,需要干什么?

package demopacket;

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 Solution {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("button");
        button.addActionListener(new MyAction());
        frame.setLayout(new BorderLayout());
        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("000");
    }
}

按下会打印000。
在这里插入图片描述

5、文本框(TextField)的监听

package demopacket;

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

public class Solution {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }
}

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

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

        //设置替换编码,隐藏输入内容
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField source =(TextField) e.getSource();  //获得资源,返回一个对象
        System.out.println(source.getText());
        source.setText(""); //null,enter后清空文本内容
    }
}

6、制作简易计算器

package demopacket;

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

public class Solution {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame() {
        TextField textField1 = new TextField(10);
        TextField textField2 = new TextField(10);
        TextField textField3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        button.addActionListener(new MyActionListener(textField1, textField2, textField3));
        setLayout(new FlowLayout());
        add(textField1);
        add(label);
        add(textField2);
        add(button);
        add(textField3);
        setVisible(true);
        pack();
    }
}

class MyActionListener implements ActionListener {
    private TextField textField1, textField2, textField3;

    public MyActionListener(TextField textField1, TextField textField2, TextField textField3) {
        this.textField1 = textField1;
        this.textField2 = textField2;
        this.textField3 = textField3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = Integer.parseInt(textField1.getText());
        int b = Integer.parseInt(textField2.getText());
        textField3.setText("" + (a + b));
        textField1.setText("");
        textField2.setText("");
    }
}

7、画笔

package demopacket;

import java.awt.*;

public class Solution {
    public static void main(String[] args) {
        MyPrint myPrint = new MyPrint();
        myPrint.loadFrame();
    }
}

class MyPrint extends Frame {

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

    //画笔
    @Override
    public void paint(Graphics g) {
        //画笔需要有颜色,画笔可以画画
        g.setColor(Color.blue);
        g.fillOval(100, 100, 100, 100);
    }
}

8、鼠标监听

package demopacket;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class Solution {
    public static void main(String[] args) {
        MyPrint myPrint = new MyPrint("画图");
    }
}

class MyPrint extends Frame {

    ArrayList points;

    public MyPrint(String title) {
        super(title);
        setBounds(200, 200, 300, 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) {
            MyPrint source = (MyPrint) e.getSource();
            source.addPaint(new Point(e.getX(), e.getY()));
            source.repaint();
        }
    }

}

在这里插入图片描述

9、窗口监听

package demopacket;

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

public class Solution {
    public static void main(String[] args) {
        WindowFrame windowFrame = new WindowFrame();
    }

    static class WindowFrame extends Frame {
        public WindowFrame() {
            setBounds(100, 100, 200, 200);
            setBackground(Color.blue);
            setVisible(true);
            //addWindowListener(new MyWindowListener());
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("你点击了X");
                    System.exit(0);
                }
            });
        }
        /*class MyWindowListener extends WindowAdapter {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }*/
    }
}

10、键盘监听

package demopacket;

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

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

    static class KeyFrame extends Frame {
        public KeyFrame() {
            setBounds(100, 100, 200, 200);
            setBackground(Color.blue);
            setVisible(true);
            this.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    System.out.println(e.getKeyCode());
                }
            });
        } 
    }
}

3、Swing

1、窗口,面板

package demopacket;

import javax.swing.*;

public class Solution {

    public static void init() {
        JFrame jFrame = new JFrame("这是一个窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100, 100, 200, 200);
        JLabel jLabel = new JLabel("你好!!!");
        jFrame.add(jLabel);
        jLabel.setHorizontalAlignment(0);//居中
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭事件
    }

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

2、弹窗

package demopacket;

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

public class Solution extends JFrame {

    public Solution() {
        this.setVisible(true);
        this.setBounds(100, 100, 200, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭
        //JFrame 放东西需要一个容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);
        JButton jButton = new JButton("点击弹出一个对话框");
        jButton.setBounds(30, 30, 100, 50);
        //点击这个按钮的时候弹出一个弹窗
        container.add(jButton);
        jButton.addActionListener(new ActionListener() {//监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                MyDialog myDialog = new MyDialog();
            }
        });
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
    }

}

class MyDialog extends JDialog {
    public MyDialog() {
        this.setVisible(true);
        this.setBounds(200, 200, 200, 200);
        Container container = this.getContentPane();//JFrame 放东西需要一个容器
        container.setLayout(null);
        JLabel jLabel = new JLabel("你好!!!");
        jLabel.setBounds(50, 50, 150, 150);
        container.add(jLabel);
    }
}

在这里插入图片描述

3、标签

package demopacket;

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

public class Solution extends JFrame implements Icon {

    private int width;
    private int height;

    public Solution() {

    }

    public void init() {
        Solution solution = new Solution(15, 15);
        //图标可以放在标签上,按钮上
        JLabel jLabel = new JLabel("hello", solution, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public Solution(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public static void main(String[] args) {
        new Solution().init();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x, y, width, height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }
}

4、面板

package demopacket;

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

public class Solution extends JFrame {

    public Solution() {
        Container container = this.getContentPane();
        JTextArea textArea = new JTextArea(20, 30);
        textArea.setText("你好!!!");
        //scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        this.setVisible(true);
        this.setBounds(100, 100, 200, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

5、按钮

6、列表

7、文本框

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值