Java GUI编程汇总

文章目录

  • AWT
    • 组件
    • 容器 Frame
    • 面板 Panel
    • 三种布局模式
    • 事件监听
    • 文本框监听
    • 画图类 paint(g)
    • 鼠标监听
    • 窗口监听
    • 键盘监听
  • Swing
    • 容器 JFrame
    • 面板 JPanel
    • 带滚动条的面板 JScrollPanel
    • 标签 JLabel
    • 按钮 JButton
    • 单选框 JRadioButton
    • 多选框 JCheckBox
    • 下拉框 JComboBox
    • 列表框 JList
    • 弹窗 JDialog
    • 文本框 JTextFiled

AWT Swing

AWT

组件

Java GUI编程汇总_第1张图片

容器 Frame

package com.Swing;

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

public class TestMoreFrame {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 500, 500, Color.blue);
        MyFrame myFrame2 = new MyFrame(600, 600, 500, 500, Color.YELLOW);
    }
}
//继承Frame类 拥有全部属性
class MyFrame extends Frame{
    static int id;
    public MyFrame(int x,int y,int w,int h,Color color){
        super("Frame"+(++id));
        //设置可见性
        setVisible(true);
        //设置大小
        setSize(w,h);
        //设置位置
        setLocation(x,y);
        //setBounds(x,y,w,h); 一次性完成设置大小及位置
        //设置背景颜色
        setBackground(color);
        //设置不可调整大小
        setResizable(true);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

面板 Panel

package com.Swing;

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

public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame("TestPanel");
        frame.setVisible(true);

        Panel panel = new Panel();

        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.add(panel);

        frame.setSize(400,400);
        frame.setBackground(Color.black);
        panel.setSize(500,500);
        panel.setBackground(Color.cyan);


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

三种布局模式

package com.Swing;

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.setLayout(new FlowLayout(FlowLayout.CENTER));

        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.setVisible(true);
        frame.setSize(500,500);

        //AWT 设置退出 (监听器)
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
}
//东西南北中布局
class TestBorderLayout{
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");

        frame.setLayout(new BorderLayout());

        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.add(button1,BorderLayout.CENTER);
        frame.add(button2,BorderLayout.EAST);
        frame.add(button3,BorderLayout.WEST);
        frame.add(button4,BorderLayout.SOUTH);
        frame.add(button5,BorderLayout.NORTH);


        frame.setVisible(true);
        frame.setSize(500,500);

        //AWT 设置退出 (监听器)
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }

}
//表格布局
class TestGridLayout{
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");

        frame.setLayout(new GridLayout(3,2));//行 列

        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.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);


        frame.setVisible(true);
        frame.setSize(500,500);

        //frame.pack();

        //AWT 设置退出 (监听器)
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }

}

事件监听

以简易计算器为例
输入数字 按下= 自动消除加数
Java GUI编程汇总_第2张图片

package com.Swing;

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 TestCalculate {
    public static void main(String[] args) {
        new MyFrame2().loading();
    }
}
class MyFrame2 extends Frame{
    TextField num1,num2,res;
    public void loading(){
        setVisible(true);
        setBounds(300,300,400,500);
        //pack();

        num1 = new TextField(10);
        num2 = new TextField(10);
        res = new TextField(10);

        Label label = new Label("+");
        Button button = new Button("=");

        setLayout(new FlowLayout());

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

        button.addActionListener(new Calculate(this));

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

    }
}
class Calculate implements ActionListener {

    MyFrame2 myFrame2;
    public Calculate(MyFrame2 myFrame2){
        this.myFrame2 = myFrame2;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(myFrame2.num1.getText());
        int n2 = Integer.parseInt(myFrame2.num2.getText());

        myFrame2.res.setText(""+(n1+n2));

        myFrame2.num1.setText("");
        myFrame2.num2.setText("");
    }
}

文本框监听

以输入密码为例
回车输入 并清空栏
Java GUI编程汇总_第3张图片

package com.Swing;

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 TestTextField {
    public static void main(String[] args) {
        new MyFrame1();
    }
}
class MyFrame1 extends Frame{
    public MyFrame1(){
        setVisible(true);
        setBounds(400,400,500,500);

        //文本框TextField 文本域TextArea
        TextField textField = new TextField();
        add(textField,BorderLayout.CENTER);

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

        ActionListener actionListener;
        actionListener = ((ActionEvent e) -> {
            e.getActionCommand();
        });

        //创建监听事件
        TextListener textListener = new TextListener();
        textField.addActionListener(textListener);

        //关闭
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class TextListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取监听信息强转为文本框
        TextField field =(TextField) e.getSource();
        //通过获取文本框内文本来获取输入
        System.out.println(field.getText());
        //设置文本框内容为“” (清空)
        field.setText("");

    }
}

画图类 paint(g)

固定画
Java GUI编程汇总_第4张图片

package com.Swing;

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

public class TestPaint {
    public static void main(String[] args) {
        new Paint().loading();
    }
}
class Paint extends Frame{
    public void loading(){
        setVisible(true);
        setBounds(300,300,500,500);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.cyan);
        g.drawOval(100,100,100,100);
    }
}

鼠标点击画
Java GUI编程汇总_第5张图片

package com.Swing;

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 TestMouthPaint {
    public static void main(String[] args) {
        new MouthPaint("画画");
    }
}
class MouthPaint extends Frame{
    ArrayList points;
    public MouthPaint(String name){
        super(name);
        setVisible(true);
        setBounds(300,300,500,500);

        //存放鼠标的点
        points = new ArrayList();
        //鼠标监听器 针对窗口 在窗口点
        addMouseListener(new MyMouseListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


    @Override
    public void paint(Graphics g) {
        //画画 监听鼠标事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point next =(Point) iterator.next();
            g.setColor(Color.cyan);
            g.fillOval(next.x,next.y,20,20);
        }
    }
    public void addPoint(Point point){
        points.add(point);
    }
    class MyMouseListener extends MouseAdapter{
        //鼠标点击
        @Override
        public void mousePressed(MouseEvent e) {
            MouthPaint frame = (MouthPaint) e.getSource();
            //点击的时候产生一个点
            frame.addPoint(new Point(e.getX(),e.getY()));
            //每次点击鼠标 重画一遍所有的
            frame.repaint();
        }
    }
}

鼠标监听

见上画画

窗口监听

关闭时输出
在这里插入图片描述

package com.Swing;

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

public class TestWindowsListener {
    public static void main(String[] args) {
        new Listener().loading();
    }
}
class Listener extends Frame{
    public void loading(){
        setVisible(true);
        setBounds(100,100,500,500);

        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");
                setTitle("我又出现了");
            }

        });
    }
}

键盘监听

按下哪个键返回他的KeyCode值
Java GUI编程汇总_第6张图片

package com.Swing;

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 Key().loading();
    }
}
class Key extends Frame{
    public void loading(){
        setVisible(true);
        setBounds(100,100,300,300);

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获取当前键盘下是哪个键 KeyCode值
                int keyCode = e.getKeyCode();

                System.out.println(keyCode);

                //判断如果是 上键 输出
                if(keyCode == KeyEvent.VK_UP){
                    System.out.println("上键");
                }

            }
        });

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

Swing

容器 JFrame

面板 JPanel

带滚动条的面板 JScrollPanel

标签 JLabel

  • 普通标签
  • 图片标签
  • 图像标签

按钮 JButton

  • 普通按钮
  • 带图片的按钮

单选框 JRadioButton

多选框 JCheckBox

下拉框 JComboBox

列表框 JList

弹窗 JDialog

文本框 JTextFiled

你可能感兴趣的:(java,jvm,开发语言)