GUI编程基础一篇

GUI编程

组件:

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

1、简介

Gui的核心技术:Swing AWT

为什么没有流行

  1. 因为界面不美观。
  2. 需要jre环境。比较大。

为什么要学习

  1. 写出自己心中像要的一些工具。
  2. 工作的时候,也可以需要维护到swing界面,很少。
  3. 了解MVC架构,和监听器。

2、AWT

2.1、Awt介绍

  1. 包含了很多的类和接口!GUI: 图形用户界面编程。
  2. 元素:窗口,文本框,按钮。
  3. java.awt 包中。

在这里插入图片描述

2.2、组件和容器

1、Frame

package com.yanxun.book;

import java.awt.*;

public class TestFrame {
    public static void main(String[] args) {
        //Frame
        //窗口标题
        Frame frame = new Frame("我的第一个Java图形界面窗口");
        //设置窗口可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //设置背景颜色
        frame.setBackground(new Color(131, 188, 212));
        //窗口弹出位置
        frame.setLocation(200, 200);//显示的左上角为0,0
        //将窗口固定不能拉伸, false窗口不可可拉升,true窗口可拉升
        frame.setResizable(false);
    }
}

效果图

在这里插入图片描述

问题:

  • 窗口无法关闭!
  • panel代码中已经解决关闭问题

尝试回顾封装:

效果图:

在这里插入图片描述

package com.yanxun.book;

import java.awt.*;

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


}

class MyFrame extends Frame {
    static int id = 0;//可能出现多个窗口,一个计数器

    public MyFrame(int x, int y, int w, int h, Color color){
        super("MyFrame + " + (++id));
        setBackground(color);
        setBounds(x, y, w, h);
        setVisible(true);
    }
}

2、面板Panel

解决了关闭问题

程序运行效果图

在这里插入图片描述

package com.yanxun.book;

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

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

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

        //坐标
        frame.setBounds(300, 300, 500, 500);
        frame.setBackground(new Color(40, 161, 35));

        //设置panel坐标,相对于frame,此时frame的左上角坐标为(0,0)
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(179, 68, 68));

        //frame.add(panel)
        frame.add(panel);

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

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

3、布局管理

  • 流式布局

    • package com.yanxun.book;
      
      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();
      
              //组件按钮
              Button button1 = new Button("button1");
              Button button2 = new Button("button2");
              Button button3 = new Button("button3");
      
              //设置流式布局
              //默认为剧中对齐
              //frame.setLayout(new FlowLayout());
              //向右对齐
              //frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
              //默认向左对齐
              frame.setLayout(new FlowLayout(FlowLayout.LEFT));
              frame.setSize(200, 200);
      
              //把按钮添加上去
              frame.add(button1);
              frame.add(button2);
              frame.add(button3);
      
              frame.setVisible(true);
              frame.addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
          }
      }
      
  • 东西南北中

    • 效果图

      • 在这里插入图片描述
    • 代码实现:

      • package com.yanxun.book;
        
        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();
        
                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.setVisible(true);
        
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });
            }
        }
        
        
  • 表格布局

    • package com.yanxun.book;
      
      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");
      
              Button bnt1 = new Button("bnt1");
              Button bnt2 = new Button("bnt2");
              Button bnt3 = new Button("bnt3");
              Button bnt4 = new Button("bnt4");
              Button bnt5 = new Button("bnt5");
              Button center = new Button("btn6");
      
              frame.setLayout(new GridLayout(3, 2));
              frame.add(bnt1);
              frame.add(bnt2);
              frame.add(bnt3);
              frame.add(bnt4);
              frame.add(bnt5);
              frame.add(center);
      
              frame.pack();//自动布局。
              frame.setVisible(true);
              frame.addWindowListener(new WindowAdapter() {
                  @Override
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
          }
      }
      
      
    • 课堂练习

      • 在这里插入图片描述
        在这里插入图片描述

效果图

代码

  • package com.yanxun.book;
    
    import javax.swing.border.Border;
    import java.awt.*;
    
    /**
     * 1.frame
     * 2.4个面板
     */
    public class TestCourse {
        public static void main(String[] args) {
            Frame frame = new Frame();
            frame.setSize(400, 400);
            frame.setLocation(300, 400);
            frame.setBackground(Color.black);
            frame.setVisible(true);
            frame.setLayout(new GridLayout(2, 1));
    
    
    
    
            //4个面板
            //上部分的面板
            Panel p1 = new Panel(new BorderLayout());
            //放在p1面板中间的面板
            Panel p2 = new Panel(new GridLayout(2, 1));
            //下部分的面板
            Panel p3 = new Panel(new BorderLayout());
            //放在p3中间的面板
            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-bnt-1"));
            p2.add(new Button("p2-bnt-2"));
    
            p1.add(p2, BorderLayout.CENTER);
    
            p3.add(new Button("East-2"), BorderLayout.EAST);
            p3.add(new Button("West-2"), BorderLayout.WEST);
    
            for(int i = 0; i < 4; ++ i) {
                p4.add(new Button("for-" + i));
            }
    
            p3.add(p4, BorderLayout.CENTER);
    
            frame.add(p1);
            frame.add(p3);
        }
    }
    
    

一定要先分析

4、事件监听

多个按钮共享一个事件

package com.yanxun.book;

import javax.management.monitor.Monitor;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.add(button1, BorderLayout.NORTH);
        frame.add(button2, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
    }
}

class MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击:" +  e.getActionCommand());
        if(e.getActionCommand().equals("start")){

        }else {

        }
    }
}

5、输入框TextField监听

package com.yanxun.test;

import org.w3c.dom.Text;

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 TestText {
    public static void main(String[] args) {
        //启动
        MyFrame myFrame = new MyFrame();
        myFrame.setSize(500, 500);
        windowClose(myFrame);

    }
    public static void windowClose(MyFrame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyFrame extends Frame {
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下回车就回触发输入框的事件
        textField.addActionListener(myActionListener2);

        //设置编码,显示像密码一样
        textField.setEchoChar('*');

        setVisible(true);
        pack();
    }

}



class MyActionListener2 implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获得一些资源
        String text = field.getText();//获得输入框中的文本
        System.out.println(text);
        field.setText("");//null
    }
}

6、简单计算器,组合+内部类回顾复习!

oop原则:组合,大于继承!

//组合
class A extends B {
    
}

class A {
    public B b;
    
}

完全改造成面向对象写法

  • package com.yanxun.test;
    
    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) {
            Calculator calculator = new Calculator();
            calculator.loadFrame();
            windowClose(calculator);
    
        }
        public static void windowClose(Calculator calculator){
            calculator.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    //计算器类
    class Calculator extends Frame {
        TextField num1, num2, num3;
    
        public void loadFrame(){
    
    
            setTitle("计算器");
            //三个文本框
            num1 = new TextField(10);
            num2 = new TextField(10);
            num3 = new TextField(20);
            //1个按钮
            Button button = new Button("=");
            button.addActionListener(new MyCalculatorListener(this));
            //一个标签
            Label label = new Label("+");
            //布局
            setLayout(new FlowLayout());//流式布局
            add(num1);
            add(label);
            add(num2);
            add(button);
            add(num3);
            pack();
            setVisible(true);
        }
    }
    
    //监听器类
    class MyCalculatorListener implements ActionListener {
        Calculator calculator = null;
        public MyCalculatorListener(Calculator calculator){
            this.calculator = calculator;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            //获得加数
            int n1 = Integer.parseInt(this.calculator.num1.getText());
            int n2 = Integer.parseInt(this.calculator.num2.getText());
    
            //2.讲这个值+法运算后,放到第三个框框。
            this.calculator.num3.setText("" + (n1 + n2));
            //清除前两个框
            this.calculator.num1.setText("");
            this.calculator.num2.setText("");
        }
        //获取三个变量
    }
    
  • 效果图

    • 在这里插入图片描述

内部类:

  • 更好的包装
package com.yanxun.test;

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) {
        Calculator calculator = new Calculator();
        calculator.loadFrame();
        windowClose(calculator);

    }
    //关闭事件监听
    public static void windowClose(Calculator calculator){
        calculator.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//计算器类
class Calculator extends Frame {
    TextField num1, num2, num3;

    public void loadFrame(){


        setTitle("计算器");
        //三个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener());
        //一个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());//流式布局
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    //这是一个内部类
    private //监听器类
    class MyCalculatorListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //获得加数,将字符串转化成整形
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            //2.讲这个值+法运算后,放到第三个框框。
            num3.setText("" + (n1 + n2));
            //清除前两个框
            num1.setText("");
            num2.setText("");
        }
        //获取三个变量
    }
}

7、画笔

package com.yanxun.test;

import java.awt.*;

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

class MyFrame1 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.GREEN);
        g.fillRect(150, 200, 200, 200);

        //repaint();

        //画笔用完还原成最初的颜色
    }
}

效果图

在这里插入图片描述

8、鼠标监听

在这里插入图片描述

package com.yanxun.lesson3;

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

public class TestMouseListener {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame("画图");
        windowClose(myFrame);
    }
    public static void windowClose(MyFrame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyFrame extends Frame {

    //画画要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
    ArrayList points;
    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 400, 300);
        //存鼠标点击的点
        points = new ArrayList<>();
        setVisible(true);
        //鼠标监听器正对这个窗口
        this.addMouseListener(new MyMouseListener());
    }

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


    public class MyMouseListener extends MouseAdapter {
        //鼠标事件有,按压,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            //拿到窗口句柄
            MyFrame myFrame = (MyFrame) e.getSource();
            //点击的时候界面上有个点
            myFrame.addPaint(new Point(e.getX(), e.getY()));

            //每次点击鼠标重新画一次
            repaint();
        }
    }
}

上边代码大致过程

9、窗口监听

package com.yanxun.lesson3;

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) {
                setTitle("激活成功");
                System.out.println("windowActivated");
            }


        });
    }

    class MyWindowListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);//隐藏窗口,
            //System.exit(0);//正常关闭
        }
    }

}

10、键盘监听

package com.yanxun.lesson3;

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(1, 2, 300, 400);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键位,当前的码
                int keyCode = e.getKeyCode();//直接使用静态属性,查看源代码
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按了上键");
                }
                //根据按件做对应的操作
                
            }
        });
    }

}

输出效果图

在这里插入图片描述

3、Swing

3.1、窗口

package com.yanxun.test2;

import javax.swing.*;

public class JFrameDemo {
    //初始化
    public void init(){
        JFrame jframe = new JFrame("这是一个JFrame窗口");
        jframe.setVisible(true);
        jframe.setBounds(100, 100, 200, 200);
        //设置文字 JLabel
        JLabel label = new JLabel("欢迎学习java");

        jframe.add(label);

        //关闭事件
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();

    }
}

package com.yanxun.test2;

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

public class JFrameDemo2 extends JFrame {
    public static void main(String[] args) {
        new MyJFrame2().init();
    }

}
class MyJFrame2 extends JFrame {
    public void init() {
        this.setBounds(10, 10, 200, 300);
        this.setVisible(true);

        JLabel label = new JLabel("欢迎学习java");

        this.add(label);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //获得容器
        Container container = this.getContentPane();
        container.setBackground(Color.cyan);

        //关闭窗口事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

3.2、弹窗

package com.yanxun.test2;

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 MyDialogDemo();
            }
        });
        container.add(jButton);
    }

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

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

        Container container = this.getContentPane();

        container.setLayout(null);

        container.add(new JLabel("学习java走上人生巅峰"));
    }
}


3.3、标签

label:

new Lable("内容");
new JLable("内容");

图标ICON

package com.yanxun.test2;

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

//图标是一个接口,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;

    public IconDemo() {
    }

    public IconDemo(int width, int height) {
        this.width = width;
        this.height = height;
    }
    public void init() {
        IconDemo iconDemo = new IconDemo(15, 15);
        //图标放在标签上,也可以放在按钮上
        JLabel label = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
        setSize(200, 200);
        Container container = getContentPane();
        container.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    @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;
    }

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

图片ICON

package com.yanxun.test2;

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

public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("LC11.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);

        label.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100, 100, 200, 200);
    }

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

3.4、面板

下面代码的效果图

在这里插入图片描述

package com.yanxun.test5;

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

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

        container.setLayout(new GridLayout(2, 1, 10, 10));//后面参数是间距
		//定义了四个面板,并且都用表格布局
        JPanel panel1 = new JPanel(new GridLayout(1, 3));
        JPanel panel2 = new JPanel(new GridLayout(1, 2));
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        JPanel panel4 = new JPanel(new GridLayout(3, 2));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));

        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

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


    }

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

JScrollPanel(有滚动条)

package com.yanxun.test5;

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

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

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习Java");

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

        this.setVisible(true);
        this.setBounds(100, 100, 300,350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

3.5、按钮

  • 图片按钮

    • package com.yanxun.test5;
      
      import javax.swing.*;
      import java.awt.*;
      import java.net.URL;
      
      public class JButtonDemo01 extends JFrame {
          public JButtonDemo01(){
              Container container = this.getContentPane();
              URL resource = JButtonDemo01.class.getResource("LC11.jpg");
      
              Icon icon = new ImageIcon(resource);
      
              //把这个图标放在按钮上
              JButton jButton = new JButton();
              jButton.setIcon(icon);
              jButton.setToolTipText("鼠标放到按钮上的提示");
      
              //add
              container.add(jButton);
      
              this.setVisible(true);
              this.setSize(500, 300);
              this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          }
          public static void main(String[] args) {
              new JButtonDemo01();
          }
      }
      
      
      
      
  • 单选按钮

    • 效果图

    • 在这里插入图片描述

    • package com.yanxun.test5;
      
      import javax.swing.*;
      import java.awt.*;
      import java.net.URL;
      
      public class JButtonDemo02 extends JFrame {
          public JButtonDemo02() {
              Container container = this.getContentPane();
              //将图片变为图标
              URL resource = JButtonDemo02.class.getResource("LC11.jpg");
              Icon icon = new ImageIcon(resource);
      
              //单选框
              JRadioButton radioButton1 = new JRadioButton("JRadioButton-1");
              JRadioButton radioButton2 = new JRadioButton("JRadioButton-2");
              JRadioButton radioButton3 = new JRadioButton("JRadioButton-3");
      
              //由于单选框只能选择一个,所以必须分组,也就是一组中只能选一个
              ButtonGroup group = new ButtonGroup();
              group.add(radioButton1);
              group.add(radioButton2);
              group.add(radioButton3);
      
              container.add(radioButton1, BorderLayout.CENTER);
              container.add(radioButton2, BorderLayout.NORTH);
              container.add(radioButton3, BorderLayout.SOUTH);
      
              this.setVisible(true);
              this.setSize(500, 300);
              this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      
          }
      
          public static void main(String[] args) {
              new JButtonDemo02();
          }
      }
      
      
  • 复选按钮

    • 效果图

    • 在这里插入图片描述

    • 代码:

      • package com.yanxun.test5;
        
        import javax.swing.*;
        import java.awt.*;
        import java.net.URL;
        
        public class JButtonDemo02 extends JFrame {
            public JButtonDemo02() {
                Container container = this.getContentPane();
                //将图片变为图标
                URL resource = JButtonDemo02.class.getResource("LC11.jpg");
                Icon icon = new ImageIcon(resource);
        
                //多选框
                JCheckBox checkBox1 = new JCheckBox("checkBox1");
                JCheckBox checkBox2 = new JCheckBox("checkBox2");
                JCheckBox checkBox3 = new JCheckBox("checkBox3");
        
                container.add(checkBox1, BorderLayout.NORTH);
                container.add(checkBox2, BorderLayout.CENTER);
                container.add(checkBox3, BorderLayout.SOUTH);
        
                this.setVisible(true);
                this.setSize(500, 300);
                this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        
            }
        
            public static void main(String[] args) {
                new JButtonDemo02();
            }
        }
        
        

        应用场景

        • 选择国家,或者一些单个选项

3.6、列表

  • 下拉框

    • 效果图

      • 在这里插入图片描述
    • 代码

      • package com.yanxun.test6;
        
        import javax.swing.*;
        import java.awt.*;
        
        public class TestComboxDemo1 extends JFrame {
            public TestComboxDemo1() {
                Container container = this.getContentPane();
        
                JComboBox box = new JComboBox();
        
                box.addItem("上映");
                box.addItem("下架");
                box.addItem(null);
        
                container.add(box);
        
                this.setVisible(true);
                this.setSize(500, 300);
                this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            }
        
            public static void main(String[] args) {
                new TestComboxDemo1();
            }
        }
        
        
  • 列表框

    • 效果图

      • 在这里插入图片描述
    • 代码

      • package com.yanxun.test6;
        
        import javax.swing.*;
        import java.awt.*;
        import java.util.Vector;
        
        public class TestComboxDemo2 extends JFrame {
            public TestComboxDemo2(){
                Container container = this.getContentPane();
        
                //生成列表内容
                //String[] contents = {"1", "2", "3"};
                Vector contents = new Vector();
                contents.add("1");
                contents.add("2");
                contents.add("3");
                //列表中需要放入的内容
                JList jList = new JList(contents);
        
                container.add(jList);
        
                this.setVisible(true);
                this.setSize(500, 200);
                this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            }
        
            public static void main(String[] args) {
                new TestComboxDemo2();
            }
        }
        
        

3.7、文本框

  • 文本框

    效果图

在这里插入图片描述

package com.yanxun.Test7;

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

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

        //声明了一个文本框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("world", 20);

        //监听事件
        textField1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JTextField source = (JTextField) e.getSource();//获取文本框对象
                String text = source.getText();
                System.out.println(text);
                source.setText("");
            }
        });
        container.add(textField1, BorderLayout.NORTH);
        container.add(textField2, BorderLayout.SOUTH);


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

    public static void main(String[] args) {
        new TestTextDemo1();
    }
}
  • 密码框

    • 效果图

      • 在这里插入图片描述
    • 代码

      • package com.yanxun.Test7;
        
        import javax.swing.*;
        import java.awt.*;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        
        public class TextDemo3 extends JFrame {
            public TextDemo3() {
                Container container = this.getContentPane();
        
                //声明一个密码框
                JPasswordField passwordField = new JPasswordField();//*****
                passwordField.setEchoChar('*');
        
                container.add(passwordField);
        
        
        
                this.setVisible(true);
                this.setSize(500, 350);
                this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            }
        
            public static void main(String[] args) {
                new TextDemo3();
            }
        }
        
        
  • 文本域

    • 效果图

    • 在这里插入图片描述

    • package com.yanxun.test5;
      
      import javax.swing.*;
      import java.awt.*;
      
      public class JScrollDemo extends JFrame {
          public JScrollDemo() {
              Container container = this.getContentPane();
      
              //文本域
              JTextArea textArea = new JTextArea(20, 50);
              textArea.setText("欢迎学习Java");
      
              //Scroll面板
              JScrollPane scrollPane = new JScrollPane(textArea);
              container.add(scrollPane);
      
              this.setVisible(true);
              this.setBounds(100, 100, 300,350);
              this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      
          }
      
          public static void main(String[] args) {
              new JScrollDemo();
          }
      }
      
      

完结撒花,估计没人能看到这里。## 标题

追求吾之所爱
原文地址:https://www.cnblogs.com/rstz/p/14390985.html