GUI编程

GUI编程

组件

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

1.简介

GUI的核心技术:Swing AWT

GUI不流行的原因

  1. 因为界面不美观
  2. 需要jre环境

为什么要学GUI?

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

2.AWT

2.1AWT介绍

  1. 包含了很多的类和接口
  2. 有许多元素:窗口、按钮、文本框
  3. java.awt包

组件:Component

  • button

  • TextArea

  • Label

  • 容器Container

    • 窗口 Window

      • Frame
      • Dialog
    • 面板 Panel

      • Applet

2.2组件和容器

  1. Frame
package com.lvcong.lesson01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //Frame 看源码
        Frame frame = new Frame("我的第一个Java图像界面窗口");
        //要想窗口显示出来,需要设置可见性w,h
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //设置背景颜色color
        frame.setBackground(new Color(1, 150, 255));
        //设置窗口弹出的初始位置
        frame.setLocation(822, 157);
        //设置窗口大小固定
        frame.setResizable(false);
    }

}

image-20210707180318474

问题:发现窗口关闭不掉,停止Java程序!

尝试回顾封装:

package com.lvcong.lesson01;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.red);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.blue);
        MyFrame myFrame3 = new MyFrame(500, 100, 200, 200, Color.black);
    }
}

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

image-20210707182231654

  1. Panel面板

解决了关闭事件

package com.lvcong.lesson01;

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

//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(5, 7, 12));
        //panel设置坐标,相对于frame
        panel.setBounds(51, 51, 157, 157);
        panel.setBackground(new Color(157, 199, 197));
        //将面板添加到窗口
        frame.add(panel);
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件 system.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

image-20210707195820491

3.布局管理器

  • 流式布局
package com.lvcong.lesson01;

import java.awt.*;

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.LEFT));
        frame.setSize(200, 200);
        //把按钮添加到窗口
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);

    }
}
  • 东南西北中
package com.lvcong.lesson01;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout");
        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.setSize(520, 520);
        frame.setVisible(true);

    }
}
  • 表格布局
package com.lvcong.lesson01;

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        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");
        Button button6 = new Button("button6");
        frame.setLayout(new GridLayout(3, 2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.pack();
        frame.setVisible(true);


    }
}

练习

package com.lvcong.lesson01;

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

public class Demo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(520, 520);
        frame.setLocation(300, 300);
        frame.setBackground(Color.red);
        frame.setLayout(new GridLayout(2, 1));
        //4个面板
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2, 1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2, 2));

        panel1.add(new Button("East-1"), BorderLayout.EAST);
        panel1.add(new Button("West-1"), BorderLayout.WEST);
        panel2.add(new Button("panel2-btn-1"));
        panel2.add(new Button("panel2-btn-2"));
        panel1.add(panel2, BorderLayout.CENTER);

        panel3.add(new Button("East-2"), BorderLayout.EAST);
        panel3.add(new Button("West-2"), BorderLayout.WEST);
//        panel4.add(new Button("panel4-btn-1"));
//        panel4.add(new Button("panel4-btn-2"));
//        panel4.add(new Button("panel4-btn-3"));
//        panel4.add(new Button("panel4-btn-4"));
        for (int i = 0; i < 4; i++) {
            panel4.add(new Button("four" + i));
        }
        panel3.add(panel4, BorderLayout.CENTER);
        frame.add(panel1);
        frame.add(panel3);

        frame.setVisible(true);
    }
}

总结

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

4.事件监听

package com.lvcong.lesson01;

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();
        //因为addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);//关闭窗口
        frame.setVisible(true);
    }

    //关闭窗口的事件
    private static void windowClose(Frame frame) {
        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("我爱郑爽");
    }
}

多个按钮共享一个事件

package com.lvcong.lesson01;

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

public class TestActionTwo {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始 停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
        //可以多个按钮只写一个监听类
        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("按钮被点击了:msg-->" + e.getActionCommand());

    }
}

输入框监听事件

package com.lvcong.lesson01;

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

public class TestText01 {
    public static void main(String[] args) {
        //启动!
        new MyFrame02();
    }
}

class MyFrame02 extends Frame {
    public MyFrame02() {
        TextField textField = new TextField();
        //因为是继承了Frame
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener02 myActionListener02 = new MyActionListener02();
        //按下enter,触发事件
        textField.addActionListener(myActionListener02);
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();

    }
}

class MyActionListener02 implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();//获得一些资源,返回一个Object
        System.out.println(textField.getText());//获得输入框的文本
        textField.setText("");//null
    }
}

简易计算器,组合+内部类回顾复习

oop原则:组合大于继承

class A extends B{
    
}
class A{
    public B b;
}

普通写法-面向过程

package com.lvcong.lesson01;

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

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}

//计算器类
class Calculator extends Frame {
    public Calculator() {
        //3个文本框
        TextField num1 = new TextField(10);//字符数
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        //button添加监听事件
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));
        //1个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }
}

//监听器类
class MyCalculatorListener implements ActionListener {
    //获取三个变量
    private TextField num1, num2, num3;

    public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        //将这个值+法运算后,放到第三个框
        num3.setText("" + (n1 + n2));
        //清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

完全改造为面向对象写法

package com.lvcong.lesson01;

import com.sun.prism.shader.FillCircle_RadialGradient_PAD_AlphaTest_Loader;

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

//简易计算器
public class TestCalc1 {
    public static void main(String[] args) {
        new Calculator1().loadFrame();
    }
}

//计算器类
class Calculator1 extends Frame {
    //属性
    TextField num1, num2, num3;

    //方法
    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener1(this));
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }
}

//监听器类
class MyCalculatorListener1 implements ActionListener {
    //获取计算器这个对象,在一个类中组合另外一个类
    Calculator1 calculator1 = null;

    public MyCalculatorListener1(Calculator1 calculator1) {
        this.calculator1 = calculator1;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//    1.获得加数和被加数
//    2.将这个值加法运算后,放到第三个框
//    3.清除前两个框
        int n1 = Integer.parseInt(calculator1.num1.getText());
        int n2 = Integer.parseInt(calculator1.num2.getText());
        calculator1.num3.setText("" + (n1 + n2));
        calculator1.num1.setText("");
        calculator1.num2.setText("");
    }
}

内部类

  • 更好的包装
package com.lvcong.lesson01;

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

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

//计算器类
class Calculator2 extends Frame {
    //属性
    TextField num1, num2, num3;

    //方法
    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener2());
        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);

    }

    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法
    private class MyCalculatorListener2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
//    1.获得加数和被加数
//    2.将这个值加法运算后,放到第三个框
//    3.清除前两个框
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText("" + (n1 + n2));
            num1.setText("");
            num2.setText("");
        }
    }
}

画笔paint

package com.lvcong.lesson02;

import java.awt.*;

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

class MyPaint extends Frame {
    public void loadFrame() {
        setBounds(200, 200, 800, 800);
        setVisible(true);
    }
    //画笔

    @Override
    public void paint(Graphics g) {
        //画笔需要颜色,可以用来画画
        g.setColor(Color.BLUE);
        g.drawOval(100,100,200,200);
        //养成习惯,画笔用完,将它还原到最初的颜色
    }
}

鼠标监听

目的:想要实现鼠标画画

image-20210708115725579

package com.lvcong.lesson02;

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

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

//自己的类
class MyFrame extends Frame {
    //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这些点
    ArrayList points;

    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 500, 500);
        //存鼠标点击的点
        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) {
            MyFrame myFrame = (MyFrame) e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点
            //这个点就是鼠标的点
            myFrame.addPaint(new Point(e.getX(), e.getY()));
            //每次点击鼠标都需要重新画一遍
            myFrame.repaint();//刷新

        }
    }
}

窗口监听

package com.lvcong.lesson02;

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.exit(0);
                    }

                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame)e.getSource();
                        source.setTitle("被激活了");
                    }
                }
        );
    }

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

键盘监听

package com.lvcong.lesson02;

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

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBackground(Color.BLUE);
        setBounds(300,300,500,500);
        setVisible(true);
        this.addKeyListener(
                new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                        //获得键盘下的键是哪一个
                        int keyCode = e.getKeyCode();//不需要去记录这个数字,直接使用静态属性VK_XXX
                        if(keyCode == KeyEvent.VK_UP){
                            System.out.println("你按下了上键");

                        }
                    }
                }
        );
    }
}

Swing

package com.lvcong.lesson03;

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

public class JFrameDemo {
    //init();初始化
    public void init(){
        JFrame jFrame = new JFrame("这是一个JFrame窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(300,300,500,500);
        jFrame.setBackground(Color.BLUE);
        //设置文字Jlabel
        JLabel jLabel = new JLabel("我爱郑爽");
        jFrame.add(jLabel);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

标签居中

package com.lvcong.lesson03;

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

public class JFramDemo02 {
    public static void main(String[] args) {
        new MyJFrame2().init();
    }
}
class MyJFrame2 extends JFrame{
    public void init(){
        this.setBounds(100,100,500,500);
        this.setVisible(true);
        JLabel jLabel = new JLabel("645213");
        this.add(jLabel);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.BLUE);
        //关闭事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }
}

弹窗

package com.lvcong.lesson03;

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);
        container.add(jButton);
        //点击这个按钮的时候,弹出一个弹窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {//监听器
                //弹窗
                new MyDialogDemo();
            }
        });
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
    public MyDialogDemo(){
        this.setVisible(true);
        this.setBounds(100,200,330,440);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("4444444465464656646"));
    }
}

标签

label

new JLabel("xxx");

图标

package com.lvcong.lesson03;

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

//图标,需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
    public static void main(String[] args) {
        new IconDemo().init();
    }

    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(100, 100);
        //图标放在标签,也可以放在按钮上
        JLabel jLabel = new JLabel("SHENYANG", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        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;
    }
}

图片

package com.lvcong.lesson03;

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

public class IconImageDemo extends JFrame {
    public IconImageDemo() {
        //获取图片的地址
        JLabel jLabel = new JLabel("zs");
        URL url = IconImageDemo.class.getResource("zs.jpeg");
        ImageIcon imageIcon = new ImageIcon(url);//创建一个图标
        jLabel.setIcon(imageIcon);//将图标加到标签上
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(200, 200, 520, 520);

    }

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

面板

JPanel

package com.lvcong.lesson03;

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

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2, 1, 10, 10));//后面参数的意思是间距
        JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
        JPanel jPanel2 = new JPanel(new GridLayout(2, 2));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        jPanel2.add(new JButton("3"));
        container.add(jPanel1);
        container.add(jPanel2);
        this.setVisible(true);
        this.setBounds(200, 200, 500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

JScrollPanel

package com.lvcong.lesson03;

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

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

    public JScrollDemo() {
        Container container = this.getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("郑爽");
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);

        this.setVisible(true);
        this.setBounds(200, 200, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
}

按钮

  • 图片按钮
package com.lvcong.Lesson04;

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

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

    public JButtonDemo() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL url = JButtonDemo.class.getResource("zs.jpeg");
        Icon icon = new ImageIcon(url);
        //把这个图标放到按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        //add
        container.add(button);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }
}
  • 单选按钮
package com.lvcong.Lesson04;

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

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

    public JButtonDemo02() {
        Container container = this.getContentPane();
        //将一个图片变为图标
        URL url = JButtonDemo.class.getResource("zs.jpeg");
        Icon icon = new ImageIcon(url);
        //单选框
        JRadioButton radioButton1 = new JRadioButton("radioButton1");
        JRadioButton radioButton2 = new JRadioButton("radioButton2");
        JRadioButton radioButton3 = new JRadioButton("radioButton3");

        //由于单选框只能选一个,所以需要分组,一个组里只能选一个
        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, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 多选按钮
package com.lvcong.Lesson04;

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

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

    public JButtonDemo03() {
        Container container = this.getContentPane();
        //多选框
        JCheckBox checkBox1 = new JCheckBox("checkBox1");
        JCheckBox checkBox2 = new JCheckBox("checkBox2");

        container.add(checkBox1, BorderLayout.NORTH);
        container.add(checkBox2, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

列表

  • 下拉框
package com.lvcong.Lesson04;

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

public class TestBoxDemo extends JFrame {


    public TestBoxDemo() {
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正版");
        status.addItem("微光");
        status.addItem("无所畏惧");
        container.add(status);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

    }
}
  • 列表框
package com.lvcong.Lesson04;

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

public class TextBoxDemo02 extends JFrame {


    public TextBoxDemo02() {
        Container container = this.getContentPane();

        //生成列表的内容
//        String[] str = {"1", "2", "3"};
//        //列表中需要放入内容(静态)
//        JList jList = new JList(str);
//        container.add(jList);

        //动态
        Vector vector = new Vector();
        JList jList1 = new JList(vector);
        vector.add("111");
        vector.add("222");
        vector.add("333");
        container.add(jList1);


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

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

    }
}

应用场景

  • 下拉框:选择地区,或者一些单个选项
  • 列表,展示信息,一般是动态扩容!

文本框

  • 文本框
package com.lvcong.Lesson04;

import sun.net.NetworkServer;

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

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

    public TestTextDemo() {
        Container container = this.getContentPane();
        //文本框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("world", 20);
        container.add(textField1, BorderLayout.NORTH);
        container.add(textField2, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(157, 197, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 密码框
package com.lvcong.Lesson04;

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

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

    public TestTextDemo2() {
        Container container = this.getContentPane();
        //密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('·');
        container.add(passwordField);

        this.setVisible(true);
        this.setBounds(157, 197, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
  • 文本域
package com.lvcong.Lesson04;

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

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

    public TestTextDemo3() {
        Container container = this.getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("郑爽");
        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);
        this.setVisible(true);
        this.setBounds(157, 197, 520, 520);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
posted @ 2021-07-09 17:50  独爱郑爽  阅读(61)  评论(0)    收藏  举报
Live2D
返回顶端