Java学习笔记之GUI编程

简介

请添加图片描述

AWT

介绍

请添加图片描述

组件和容器

Frame

import java.awt.*;

public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个图形界面窗口");
        frame.setVisible(true);
        frame.setSize(300,100);
        frame.setBackground(new Color(11, 199, 239 , 255));
        //弹出的初始位置,默认(0,0)
        frame.setLocation(100,100);
        //设置大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        new myFrame(100,100,200,200,Color.BLUE);
        new myFrame(500,500,100,100,Color.GREEN);
    }
}

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

在这里插入图片描述

布局管理器

流布局

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

//流布局
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("FlowLayout");
        frame.setBounds(200,200,500,500);
        frame.setVisible(true);
        //默认中间
        frame.setLayout(new FlowLayout());
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

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

在这里插入图片描述

边界布局

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

//边框布局
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");
        frame.setSize(300,300);
        frame.setVisible(true);

        Button east = new Button("east");
        Button west = new Button("west");
        Button north = new Button("north");
        Button south = new Button("south");
        Button center = new Button("center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(center,BorderLayout.CENTER);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

表格布局

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

//表格布局
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("testGridLayout");
        frame.setBounds(300,300,300,300);
        frame.setVisible(true);
        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(5,5));
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);
        //自动布局
        frame.pack();

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

在这里插入图片描述

练习

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

public class PracticeDemo {
    public static void main(String[] args) {
        Frame frame = new Frame("practiceDemo");
        frame.setBounds(300,300,500,500);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));
        frame.setBackground(Color.GREEN);

        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, 2));

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

        p1.add(p2);
        frame.add(p1);

        p3.add(new Button("West-2"),BorderLayout.WEST);
        p3.add(new Button("East-2"),BorderLayout.EAST);
        p4.add(new Button("p4-btn-1"));
        p4.add(new Button("p4-btn-2"));
        p4.add(new Button("p4-btn-3"));
        p4.add(new Button("p4-btn-4"));
        p3.add(p4);
        frame.add(p3);


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

在这里插入图片描述

事件监听

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();
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.setBounds(300,300,300,300);
        frame.setVisible(true);
        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        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("aaa");
    }
}

在这里插入图片描述

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 TestActionEvent2 {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始-结束
        Frame frame = new Frame();
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        button1.setActionCommand("button1-start");
        button2.setActionCommand("button2-stop");
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.setLayout(new FlowLayout());
        frame.add(button1);
        frame.add(button2);

        frame.setBounds(300,300,300,300);
        frame.setVisible(true);
        frame.pack();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyMonitor implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了,msg:"+e.getActionCommand());
    }
}

在这里插入图片描述

输入框TextField监听

import java.awt.*;
import java.awt.event.*;

public class TestTextField {
    public static void main(String[] args) {
        new MyFrame2();
    }
}

class MyFrame2 extends Frame {
    public MyFrame2() throws HeadlessException {
        setBounds(300,300,500,500);
        setVisible(true);
//        pack();
//        new TextArea();文本域
        TextField textField = new TextField();//文本框
        add(textField);

        //设置替换编码
        textField.setEchoChar('*');
        //监听这个文本框中的内容
        MyActionListener2 myActionListener2 = new MyActionListener2();
        textField.addActionListener(myActionListener2);

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

class MyActionListener2 implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();//返回了一个对象Object
        System.out.println(textField.getText());
        textField.setText("");
    }
}

在这里插入图片描述

简易计算器

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 TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}

/**
 * 计算器类
 */
class Calculator extends Frame {
    TextField num1;
    TextField num2;
    TextField num3;
    Button button;
    Label label;

    public Calculator(){
        load();
    }

    public void load(){
        num1 = new TextField(20);
        num2 = new TextField(20);
        num3 = new TextField(20);

        button = new Button("=");
        button.addActionListener(new MyCalculatorListener(this));

        label = new Label("+");

        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);

        addWindowListener(new WindowClose());
    }
}

/**
 * 监听器类
 */
class MyCalculatorListener implements ActionListener {

    private Calculator calculator;

    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = Integer.parseInt(calculator.num1.getText());
        int b = Integer.parseInt(calculator.num2.getText());

        calculator.num3.setText(""+(a+b));
    }
}

class WindowClose extends WindowAdapter {
    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

在这里插入图片描述

画笔

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame {
    public void loadFrame() {
        setBounds(300,300,800,500);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.ORANGE);
        g.drawOval(100,100,200,200);
        g.fillOval(300,300,100,100);
        g.setColor(Color.red);
        g.fill3DRect(400,10,100,100,false);
        
    }
}

在这里插入图片描述

鼠标监听

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

//鼠标监听事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("mouseListener");
    }
}

class MyFrame extends Frame {
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    private ArrayList points;
    public MyFrame(String title) {
        super(title);
        setBounds(300,300,500,500);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //针对这个窗口需要鼠标监听器
        addMouseListener(new MyMouseListener());
        //存鼠标点击的点
        points = new ArrayList<>();
    }

    @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 myFrame = (MyFrame) e.getSource();
            //在我们点击的时候,就会在界面上产生一个点,这个点就是鼠标的点
            myFrame.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标都需要重新画一遍
            myFrame.repaint();
        }
    }
}

在这里插入图片描述

窗口监听

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

public class TestWindowListener {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
/**
*关闭窗口
*/
class WindowFrame extends Frame{
    public  WindowFrame() {
        setBounds(200,200,300,300);
        setBackground(Color.red);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

键盘监听

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

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

class KeyFrame extends Frame {
    public KeyFrame(){
        setBounds(200,200,300,300);
        setVisible(true);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if(keyCode==KeyEvent.VK_UP) {
                    System.out.println("你按下了上键");
                }
            }
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

Swing

窗口、面板

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

public class TestJFrame {
    public void init() {
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setBounds(300,300,300,300);
        frame.setVisible(true);
        //无法正常显示颜色,需要容器实例化,
        //frame.setBackground(Color.red);
        //设置文字
        JLabel label = new JLabel("Hello world");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        frame.add(label);
        //容器实例化
        Container container = frame.getContentPane();
        container.setBackground(Color.red);
        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

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

在这里插入图片描述

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

public class TestJPanel extends JFrame {
    public TestJPanel() {
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));
        JPanel panel1 = new JPanel(new GridLayout(1,3));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("2"));
        panel1.add(new JButton("3"));

        container.add(panel1);
        this.setVisible(true);
        this.setSize(200,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

弹窗

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

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        setVisible(true);
        setBounds(300,300,300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame放东西需要是一个容器
        Container container = getContentPane();
        //绝对布局
        container.setLayout(null);
        JButton button = new JButton("弹窗");
        button.setBounds(100,100,100,100);
        //点击这个按钮的时候,弹出一个弹窗
        button.addActionListener(new ActionListener() { //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });

        container.add(button);
    }
    public static void main(String[] args) {
        new DialogDemo();
    }
}

//弹窗的窗口

class MyDialog extends JDialog {
    public MyDialog() {
        setVisible(true);
        this.setBounds(100,100,100,100);
        Container container = getContentPane();
        container.setLayout(null);
        container.add(new JLabel("Hello world"));
        //默认有关闭事件
    }
}

在这里插入图片描述

Icon标签

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

public class TestICON extends JFrame implements Icon {
    public static void main(String[] args) {
        new TestICON().init();
    }

    private int width;
    private int heigth;

    public TestICON() {}

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

    @Override
    public int getIconWidth() {
        return 0;
    }

    @Override
    public int getIconHeight() {
        return 0;
    }

    public void init() {
        TestICON icon = new TestICON(15, 15);
        JLabel label = new JLabel("testICON", icon, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        setBounds(200,200,300,300);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

在这里插入图片描述

ImageIcon标签

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

public class TestImageIcon extends JFrame {
    public TestImageIcon(){
        JLabel label = new JLabel("ImageIcon");
        URL url = TestImageIcon.class.getResource("check-circle.png");
        ImageIcon icon = new ImageIcon(url);
        label.setIcon(icon);
        label.setSize(100,100);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);

        setVisible(true);
        setBounds(300,300,1000,1000);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

文本域JScroll面板

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

public class TestJScroll extends JFrame {
    public TestJScroll() {
        Container container = getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(40, 50);
        textArea.setText("Hello world");
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        
        this.setVisible(true);
        this.setSize(300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

在这里插入图片描述

按钮

普通按钮

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

public class TestJButton extends JFrame {
    public static void main(String[] args) {
        new TestJButton();
    }

    public TestJButton() {
        Container container = getContentPane();
        URL url = TestJButton.class.getResource("check-circle.png");
        ImageIcon icon = new ImageIcon(url);
        JButton button = new JButton();
        button.setIcon(icon);
        button.setBounds(50,50,50,50);
        button.setToolTipText("图标按钮");

        this.setVisible(true);
        this.setSize(1000,1000);
        this.setLayout(new GridLayout(2,2));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        container.add(button);
    }
}

在这里插入图片描述

单选框

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

public class TestRadioButton extends JFrame {
    public static void main(String[] args) {
        new TestRadioButton();
    }

    public TestRadioButton() {
        Container container = getContentPane();

        //单选框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton3");

        //由于单选框只能选一个,所以要进行分组,每组只能选一个,不分组则每个
        ButtonGroup group1 = new ButtonGroup();
        group1.add(radioButton1);
        group1.add(radioButton2);

        ButtonGroup group2 = new ButtonGroup();
        group2.add(radioButton3);


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

        container.add(radioButton1,BorderLayout.NORTH);
        container.add(radioButton2,BorderLayout.CENTER);
        container.add(radioButton3,BorderLayout.SOUTH);
    }
}

在这里插入图片描述

复选框

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

/**
 * 复选框
 */
public class TestJCheckBox extends JFrame{
    public static void main(String[] args) {
        new TestJCheckBox();
    }

    public TestJCheckBox() {
        Container container = getContentPane();

        JCheckBox checkbox01 = new JCheckBox("checkbox01");
        JCheckBox checkbox02 = new JCheckBox("checkbox02");

        container.add(checkbox01);
        container.add(checkbox02);

        this.setVisible(true);
        this.setSize(1000,1000);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
    }
}

在这里插入图片描述

列表

下拉框

public class TestJComboBox extends JFrame {
    public static void main(String[] args) {
        new TestJComboBox();
    }

    public TestJComboBox() {
        Container container = getContentPane();

        JPanel panel = new JPanel();
        container.add(panel);

        JComboBox jComboBox = new JComboBox();
        jComboBox.addItem(null);
        jComboBox.addItem("正在热映");
        jComboBox.addItem("已下架");
        jComboBox.addItem("即将上映");

        panel.add(jComboBox);

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

在这里插入图片描述

列表框

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

public class TestJList extends JFrame {
    public static void main(String[] args) {
        new TestJList();
    }

    public TestJList() {
        Container container = getContentPane();

        String[] contents = {"aaa","男","20"};
        JList jList = new JList(contents);
        container.add(jList);

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

在这里插入图片描述

文本框

文本框

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

public class TestJTextField extends JFrame {
    public static void main(String[] args) {
        new TestJTextField();
    }

    public TestJTextField() {
        this.setVisible(true);
        this.setSize(300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();

        JTextField textField1 = new JTextField("Hello world");
        JTextField textField2 = new JTextField("你好,世界!", 30);

        container.add(textField1);
        container.add(textField2);

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

在这里插入图片描述

密码框

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

public class TestPasswordField extends JFrame {
    public static void main(String[] args) {
        new TestPasswordField();
    }

    public TestPasswordField() {
        this.setVisible(true);
        this.setSize(300,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();

        JPasswordField passwordField = new JPasswordField();
//        passwordField.setEchoChar('*');
        container.add(passwordField);
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿炳的旅程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值