GUI编程

GUI编程

组件

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

1、简介

ui的核心技术: 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.lesson01;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //Frame  GDK 看源码
        Frame frame = new Frame("我的第一个Java图像界面窗口");
        //需要设置可见性  w h
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //设置背景颜色
        frame.setBackground(new Color(224));
        //弹出的初始位置
        frame.setLocation(200, 200);
        //设置大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述

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

尝试回顾封装:

package com.lesson01;
import java.awt.*;
public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口  new
        MyFrame frame1 = new MyFrame(100,100,300,300,Color.CYAN);
        MyFrame frame2 = new MyFrame(400,100,300,300,Color.yellow);
        MyFrame frame3 = new MyFrame(100,400,300,300,Color.pink);
        MyFrame frame4 = new MyFrame(400,400,300,300,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));
        setBackground(color);
        setBounds(x,y,w,h);
        setVisible(true);
    }
}

在这里插入图片描述

2、面板Panel

解决了关闭问题

package com.lesson01;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Ponel 可以看成是一个空间,但是不能单独存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);
        //坐标
        frame.setBounds(200, 200, 500, 500);
        frame.setBackground(Color.pink);
        //panel设置坐标,相对于frame
        panel.setBounds(100, 100, 300, 300);
        panel.setBackground(Color.yellow);
        //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);
            }
        });
    }
}

在这里插入图片描述

2.3、布局管理器

  • 流式布局

    package com.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(FlowLayout.RIGHT));
            frame.setSize(300, 300);
            //把按钮添加上去
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
            frame.setVisible(true);
        }
    }
    

在这里插入图片描述

  • 东西南北中

    package com.lesson01;
    import java.awt.*;
    
    public class TestBorderLayout {
        public static void main(String[] args) {
            Frame frame = new Frame("TestBorderLayout");
            Button east = new Button("East");
            Button west = new Button("West");
            Button north = new Button("North");
            Button center = new Button("Center");
            frame.add(east,BorderLayout.EAST);
            frame.add(west,BorderLayout.WEST);
            frame.add(north,BorderLayout.NORTH);
            frame.add(center,BorderLayout.CENTER);
            frame.setSize(200,200);
            frame.setVisible(true);
        }
    }
    
    

在这里插入图片描述

  • 表格布局 Grid

    package com.lesson01;
    import java.awt.*;
    
    public class TestGrridLayout {
        public static void main(String[] args) {
            Frame frame = new Frame("TestGriderLayout");
            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(3,2));
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            frame.add(btn4);
            frame.add(btn5);
            frame.add(btn6);
            frame.pack();//Java函数
            frame.setVisible(true);
    
        }
    }
    
    

在这里插入图片描述
在这里插入图片描述
分析过程:

在这里插入图片描述

代码实现:

package com.lesson01;
import java.awt.*;

public class TestButton {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(600,600);
        frame.setLocation(300,300);
        frame.setBackground(Color.pink);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));
        //四个面板
        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,1));
        //上面
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p2-bth-1"));
        p2.add(new Button("p2-bth-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);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

总结:

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

2.4、事件监听

事件监听:当某个事情发生时候要做的事

package com.lesson01;
import java.awt.*;
import java.awt.event.*;

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("aaa");
    }
}

多个按钮共享一个事件

package com.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestAction02 {
    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) {
        //e.getActionCommand() 获得按钮的信息
        System.out.println("按钮被点击了:msg=>" + e.getActionCommand());
    }
}

2.5、输入框TextField事件监听

package com.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestText01 {
    public static void main(String[] args) {
        //启动
        new MyFrame();
    }
}
class MyFrame extends Frame {
    public MyFrame() {
        TextField textField = new TextField();
        add(textField);
        //监听这个文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter 就会触发这个输入框的
        textField.addActionListener(myActionListener2);
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class MyActionListener2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获得一些资源,返回的一个对象
        System.out.println(field.getText());//获得输入框的文本
        field.setText("");//null
    }
}

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

0pp原则:组合,大于继承!

class A extends B{

}
class A{
  public B b;
}

目前代码

package com.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TextCalc {
    public static void main(String[] args) {

        new Calculator();
    }
}
//计算器类
class Calculator extends Frame {
    public Calculator() {
        //三个文本框
        TextField field1 = new TextField(10);
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);

        //1 个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(field1, field2, field3));
        //1 个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);
        pack();
        setVisible(true);
    }
//监听器类
class MyCalculatorListener implements ActionListener {
    //获取三个变量
    private TextField field1, field2, field3;
    public MyCalculatorListener(TextField field1, TextField field2, TextField field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int f1 = Integer.parseInt(field1.getText());
        int f2 = Integer.parseInt(field2.getText());
        //2.将这个是加法运算后放到第三个框
        field3.setText("" + (f1 + f2));
        //清除前两个框
        field1.setText("");
        field2.setText("");
    }
}

完全改造为面向对象代码

package com.lesson01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//简易计算器
public class TextCalc {
    public static void main(String[] args) {

        new Calculator().loadFrame();
    }
}
//计算器类
class Calculator extends Frame {
    //属性
    TextField f1, f2, f3;
    //方法
    public void loadFrame() {
        f1 = new TextField(10);
        f2 = new TextField(10);
        f3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener(this));

        //布局
        setLayout(new FlowLayout());
        add(f1);
        add(label);
        add(f2);
        add(button);
        add(f3);
        pack();
        setVisible(true);
    }
}
//监听器类
class MyCalculatorListener implements ActionListener {
    //获取计算器这个对象,在一个类中组合另外一个类
    Calculator calculator = null;
    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        //2.将这个是加法运算后放到第三个框
        //3.清除前两个框
        int n1 = Integer.parseInt(calculator.f1.getText());
        int n2 = Integer.parseInt(calculator.f2.getText());
        calculator.f3.setText("" + (n1 + n2));
        calculator.f1.setText("");
        calculator.f2.setText("");
    }
}

内部类:

  • 更好的包装

    package com.lesson01;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    //简易计算器
    public class TextCalc {
        public static void main(String[] args) {
    
            new Calculator().loadFrame();
        }
    }
    //计算器类
    class Calculator extends Frame {
        //属性
        TextField f1, f2, f3;
        //方法
        public void loadFrame() {
            f1 = new TextField(10);
            f2 = new TextField(10);
            f3 = new TextField(20);
            Button button = new Button("=");
            Label label = new Label("+");
    
            button.addActionListener(new MyCalculatorListener());
    
            //布局
            setLayout(new FlowLayout());
            add(f1);
            add(label);
            add(f2);
            add(button);
            add(f3);
            pack();
            setVisible(true);
        }
        //监听器类
        //内部类最大的好处,就是可以畅通无阻的访问外部的属性和方法
       private class MyCalculatorListener implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.获得加数和被加数
                //2.将这个是加法运算后放到第三个框
                //3.清除前两个框
                int n1 = Integer.parseInt(f1.getText());
                int n2 = Integer.parseInt(f2.getText());
                f3.setText("" + (n1 + n2));
                f1.setText("");
                f2.setText("");
            }
        }
    }
    

2.7、画笔

package com.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, 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);
        //画笔用完将它还原到最初的颜色
    }
}

2.8、鼠标监听

目的:实现鼠标画画!

在这里插入图片描述

package com.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, 400, 300);
        //存鼠标点击的点
        points = new ArrayList<>();
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()) {
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x, point.y, 10, 10);
        }
    }
    //添加一个点到界面上
    public void addPaint(Point point) {
        points.add(point);
    }
    //适配器模式
    private class MyMouseListener extends MouseAdapter {
        //鼠标按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            //这里我们点击的时候就会在屏幕上产生一个点
            //这个点就是鼠标的点
            frame.addPaint(new Point(e.getX(), e.getY()));
            //每次点击鼠标度需要重新画一遍
            frame.repaint();//刷新
        }
    }
}

2.9、窗口监听

package com.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 windowOpened(WindowEvent e) {
                        System.out.println("windowOpened");
                    }
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                        System.exit(0);

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

                        System.out.println("windowActivated");
                    }
                }
        );
    }
}

2.10键盘监听

package com.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() {
        setBounds(1, 2, 300, 400);
        setVisible(true);
        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.lesson04;
import javax.swing.*;
import java.awt.*;

public class JFrameDemo {
    //init();初始化
    public void init() {
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100, 100, 200, 200);
        jf.setBackground(Color.BLUE);
        //设置文字  JLable
        JLabel label = new JLabel("这是一个lable");
        jf.add(label);
        //容器实例化
        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

标签居中

package com.lesson04;

import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJframe02().init();
    }
}
class MyJframe02 extends JFrame {
    public void init() {
        this.setBounds(100, 100, 300, 300);
        this.setVisible(true);
        JLabel label = new JLabel("这是一个lable");
        this.add(label);
        //让文本标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.BLUE);
    }
}

3.2、弹窗

JDialog 用来被弹出,默认有关闭事件

package com.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DiialogDemo extends JFrame {
    public DiialogDemo() {
        this.setVisible(true);
        this.setSize(700, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //JFrame  放东西,容器
        Container container = this.getContentPane();
        container.setLayout(null);
        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(100, 100, 300, 100);
        //点击这个按钮,弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDilogDemo();
            }
        });
        container.add(button);
    }
    public static void main(String[] args) {
        new DiialogDemo();
    }
}
//弹窗窗口
class MyDilogDemo extends JDialog {
    public MyDilogDemo() {
        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("欢迎来到动物世界"));
    }
}

3.3、 标签

label

new Label("xxx");

图标Icon

package com.lesson04;
import javax.swing.*;
import java.awt.*;

//图标需要实现Fream类继承
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);
        Container container = getContentPane();
        container.add(label);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new IconDemo().init();
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {

        g.fillOval(x, y, width, height);
    }
    @Override
    public int getIconWidth() {

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



图片Icon

package com.lesson04;
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("b8.png");
        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、 面板

JPanel

package com.lesson05;
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 panel = new JPanel(new GridLayout(2, 1));
        JPanel panel1 = new JPanel(new GridLayout(3, 1));
        JPanel panel2 = new JPanel(new GridLayout(1, 3));
        JPanel panel3 = new JPanel(new GridLayout(2, 2));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel1.add(new JButton("2"));
        panel1.add(new JButton("2"));
        panel1.add(new JButton("2"));
        panel2.add(new JButton("3"));
        panel2.add(new JButton("3"));
        panel2.add(new JButton("3"));
        panel3.add(new JButton("4"));
        panel3.add(new JButton("4"));
        panel3.add(new JButton("4"));
        panel3.add(new JButton("4"));
        container.add(panel);
        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        this.setVisible(true);
        this.setSize(500, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JpanelDemo();
    }
}

JscrollPanel

package com.lesson05;
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("欢迎来到我的世界");
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JscrollDemo();
    }

}

3.5 、按钮

图盘按钮

package com.lesson05;
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("b8.png");
        Icon icon = new ImageIcon(resource);
        //将图标放置到按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        //add
        container.add(button);
        this.setVisible(true);
        this.setSize(500, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

  • 单选按钮

    package com.lesson05;
    
    import javax.swing.*;
    import java.awt.*;
    public class JButtonDemo02 extends JFrame {
        public JButtonDemo02() {
            Container container = this.getContentPane();
            JRadioButton radioButton = new JRadioButton("01");
            JRadioButton radioButton1 = new JRadioButton("02");
            JRadioButton radioButton2 = new JRadioButton("03");
            ButtonGroup group = new ButtonGroup();
            group.add(radioButton);
            group.add(radioButton1);
            group.add(radioButton2);
            container.add(radioButton, BorderLayout.CENTER);
            container.add(radioButton1, BorderLayout.NORTH);
            container.add(radioButton2, 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.lesson05;
    import javax.swing.*;
    import java.awt.*;
    
    public class JButtonDemo03 extends JFrame {
        public JButtonDemo03() {
            Container container = this.getContentPane();
            JCheckBox checkBox = new JCheckBox("111");
            JCheckBox checkBox1 = new JCheckBox("222");
            JCheckBox checkBox2 = new JCheckBox("333");
            container.add(checkBox,BorderLayout.NORTH);
            container.add(checkBox1,BorderLayout.CENTER);
            container.add(checkBox2,BorderLayout.SOUTH);
            this.setVisible(true);
            this.setSize(500, 300);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new JButtonDemo03();
        }
    }
    
    

3.6、列表

  • 下拉框

    package com.lesson06;
    import javax.swing.*;
    import java.awt.*;
    
    public class TextComboboxDemo01 extends JFrame {
        public TextComboboxDemo01() {
            Container container = this.getContentPane();
            JComboBox box = new JComboBox();
            box.addItem(null);
            box.addItem("山西");
            box.addItem("湖南");
            box.addItem("上海");
            container.add(box);
            this.setVisible(true);
            this.setSize(500, 300);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new TextComboboxDemo01();
        }
    }
    
    
  • 列表框

package com.lesson06;

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

public class TextComboboxDemo02 extends JFrame {
    public TextComboboxDemo02() {
        Container container = this.getContentPane();
        //生成列表内容
        //String[] contents={"用户1","用户2","用户3"};
        Vector contents = new Vector();
        //列表中需要放入内容
        JList list = new JList(contents);
        contents.add("用户1");
        contents.add("用户2");
        contents.add("用户3");
        container.add(list);
        this.setVisible(true);
        this.setSize(500, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TextComboboxDemo02();
    }
}

应用场景

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

3.7、文本框

  • 文本框

    package com.lesson06;
    import javax.swing.*;
    import java.awt.*;
    public class TestTextDemo01 extends JFrame {
        public TestTextDemo01() {
            Container container = this.getContentPane();
    
            JTextField field = new JTextField("Hello");
            JTextField field2 = new JTextField("World", 20);
    
            container.add(field, BorderLayout.NORTH);
            container.add(field2, BorderLayout.SOUTH);
            this.setVisible(true);
            this.setSize(500, 300);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new TestTextDemo01();
        }
    }
    
    
  • 密码框

    package com.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestTextDemo02 extends JFrame {
        public TestTextDemo02() {
            Container container = this.getContentPane();
            //面板
            JPasswordField field = new JPasswordField();
            field.setEchoChar('*');
    
            container.add(field);
            this.setVisible(true);
            this.setSize(500, 300);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestTextDemo02();
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值