JAVA GUI编程

GUI编程

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

1. 简介

GUI的核心技术:Swing,AWT

快淘汰了:

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

可以学习一下了解MVC架构,了解监听

2. AWT

2.1 AWT介绍

Abstract Windows Tools 抽象窗口工具

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

2.2 组件和容器

2.2.1 Frame
public static void main(String[] args) {
        Frame frame = new Frame("这是我的第一个Frame界面");
        //设置窗口大小
        frame.setSize(400,400);
        //设置窗口颜色
        frame.setBackground(Color.BLUE);
        //设置弹出的位置
        frame.setLocation(200,200);
        //设置是否可以调节窗口大小
        frame.setResizable(false);
        //需要设置可见性
        frame.setVisible(true);
    }

对界面进行封装

public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame f1 = new MyFrame(200, 200, 200, 200, Color.BLUE);
        MyFrame f2 = new MyFrame(200, 200, 400, 200, Color.RED);
        MyFrame f3 = new MyFrame(200, 200, 200, 400, Color.PINK);
        MyFrame f4 = new MyFrame(200, 200, 400, 400, new Color(50,244,10));
    }
}
//对界面操作进行封装
class MyFrame extends Frame {
    static int count;

    public MyFrame(int h,int w,int x,int y,Color color){
        super("Frame"+(++count));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);
    }
}
2.2.2 Panel面板(不能独立存在,嵌在Frame上的)
 public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        //设置布局setLayout
        frame.setLayout(null);
        //设置Frame背景和坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(45, 104, 163));
        //设置Panel背景和坐标
        panel.setBounds(0,0,400,400);//这个panel坐标是根据frame来的。panel不能独立存在
        panel.setBackground(new Color(34, 218, 218));
        //add
        frame.add(panel);
        frame.setVisible(true);

        //事件监听,监听窗口关闭事件System.exit(0);
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });//重写方法,如果括号里是new一个接口的话,需要重写所有的方法,new一个这个接口的实现类,就重写这一个就行了
    }

2.3 布局管理器

1. 流式布局
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.LEFT));
        frame.setSize(200,200);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}

/*
public FlowLayout(int align,int hgap,int vgap)
创建一个新的流布局管理器,具有指定的对齐方式以及指定的水平和垂直间隙。
对齐参数的值必须是以下之一:FlowLayout.LEFT、FlowLayout.RIGHT、FlowLayout.CENTER、FlowLayout.LEADING 或 FlowLayout.TRAILING。


参数:
align - 对齐值
hgap - 组件之间以及组件与 Container 的边之间的水平间隙
vgap - 组件之间以及组件与 Container 的边之间的垂直间隙
*/

2. 东西南北中布局
public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("1");
        Button button2 = new Button("2");
        Button button3 = new Button("3");
        Button button4 = new Button("4");
        Button button5 = new Button("5");
        frame.setSize(200,200);
        //开始布局
        frame.setLayout(new BorderLayout());//这好像有没有这一行都行啊。。。
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.NORTH);
        frame.add(button4,BorderLayout.SOUTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setVisible(true);
    }
3.表格布局
public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1 = new Button("1");
        Button button2 = new Button("2");
        Button button3 = new Button("3");
        Button button4 = new Button("4");
        Button button5 = new Button("5");
        Button button6 = new Button("6");
        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();//java的一个方法
        frame.setVisible(true);
    }

注意点:

  • Frame和Panel都可以用setLayout
  • Panel在构造方法里可以直接写new …什么布局
  • BorderLayout好像写不写setLayout都行
4. 测试
public static void main(String[] args) {
        //先建立上下整体布局
        Frame frame = new Frame();
        frame.setBounds(500,500,600,400);
        frame.setBackground(Color.blue);
        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, 2));

        //上边两个面板操作
        p1.add(new Button("1"),BorderLayout.WEST);
        p1.add(new Button("2"),BorderLayout.EAST);
        p2.add(new Button("3"));
        p2.add(new Button("4"));
        p1.add(p2,BorderLayout.CENTER);

        //下边两个面板操作
        p3.add(new Button("5"),BorderLayout.WEST);
        p3.add(new Button("6"),BorderLayout.EAST);
        for (int i = 0; i < 4; i++) {
            p4.add(new Button(String.valueOf(i+7)));

        }
        p3.add(p4,BorderLayout.CENTER);

        //都加上
        frame.add(p1);
        frame.add(p3);
    }
5. 总结
  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须要加到某个容器中
  3. 布局管理器
    • 流式布局
    • 东南西北中布局
    • 表格布局
  4. 大小、定位、背景颜色、可见性、监听

2.4 事件监听

当某个事情发生的时候,干什么

示例一:

public class Test1 {
    public static void main(String[] args) {
        //按下按钮,触发事件
        Frame frame = new Frame();
        Button button = new Button();
        //因为addActionListener需要的参数是ActionListener,所以需要构造一个
        //直接用lamda表达式new一个接口也可以
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        //设置事件关闭按钮监听
        windowClose(frame);
        //设置可见度
        frame.add(button);
        frame.setVisible(true);
    }


    //关闭按钮事件监听写成一个方法,参数写frame,因为是窗口的关闭按钮
    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");
    }
}

示例二:多个按钮共享一个事件

public class Test2 {
    public static void main(String[] args) {
        Frame frame = new Frame("这是一个frame");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //将两个按钮加到frame上
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        //按钮事件操作
        //可以显示的定义触发会返回的命令,如果不设置这个,就使用默认值(label)
        button1.setActionCommand("我改了命令");
        MyMonitor myMonitor = new MyMonitor();
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        //添加关闭窗口事件并设置大小和可见度
        windowClose(frame);
        frame.setSize(300,300);
        frame.pack();
        frame.setVisible(true);

    }
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//monitor监视的意思
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        //可以根据事件监听的不同ActionCommand来实现不同的按钮操作,就不用一下建一堆类了
        if("我改了命令".equals(e.getActionCommand())){
            System.out.println("上边那个按钮");
        }else{
            System.out.println("下边那个按钮");
        }
    }
}

2.5 简易计算器+进阶版

初版

public class CalculatorFirst {
    public static void main(String[] args) {
        new MyCalculator();
    }
}

//简易计算器类
class MyCalculator extends Frame {
    public MyCalculator()  {
        //先建立整体布局
        TextField field1 = new TextField(10);
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        Label label = new Label("+");
        //Frame frame = new Frame("简易计算器");
        Button button = new Button("=");
        setLayout(new FlowLayout());
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);

        //给button添加事件监听
        button.addActionListener(new MyCalculatorFirst(field1,field2,field3));

        windowClose(this);
        pack();
        setVisible(true);
    }
    //关闭按钮事件监听
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//事件监听类
class MyCalculatorFirst implements ActionListener{
    //命名变量
    TextField field1,field2,field3;
    public MyCalculatorFirst(TextField field1,TextField field2,TextField field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取两个数
        int n1 = Integer.parseInt(field1.getText());
        int n2 = Integer.parseInt(field2.getText());
        //将两个数相加添加到最后一个框
        field3.setText("" +(n1+n2));
        //清除前边两个数
        field1.setText("");
        field2.setText("");
    }
}

完全面向对象的情况

public class CalculatorSecond {
    public static void main(String[] args) {
        new MyCalculator2();
    }
}

//简易计算器类
class MyCalculator2 extends Frame {
    TextField field1,field2,field3;
    public MyCalculator2()  {
        //先建立整体布局
        field1 = new TextField(10);
        field2 = new TextField(10);
        field3 = new TextField(20);
        Label label = new Label("+");
        //Frame frame = new Frame("简易计算器");
        Button button = new Button("=");
        setLayout(new FlowLayout());
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);

        //给button添加事件监听
        button.addActionListener(new MyCalculatorSecond(this));

        windowClose(this);
        pack();
        setVisible(true);
    }
    //关闭按钮事件监听
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}
//事件监听类
class MyCalculatorSecond implements ActionListener {
    //命名变量
    MyCalculator2 myCalculator2;
    public MyCalculatorSecond(MyCalculator2 myCalculator2) {
        this.myCalculator2 = myCalculator2;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取两个数
        int n1 = Integer.parseInt(myCalculator2.field1.getText());
        int n2 = Integer.parseInt(myCalculator2.field2.getText());
        //将两个数相加添加到最后一个框
        myCalculator2.field3.setText("" +(n1+n2));
        //清除前边两个数
        myCalculator2.field1.setText("");
        myCalculator2.field2.setText("");
    }
}

内部类的情况

public class CalculatorFinal {
    public static void main(String[] args) {
        new MyCalculator3().load();
    }
}

//简易计算器类
class MyCalculator3 extends Frame {
    TextField field1,field2,field3;
    public void load()  {
        //先建立整体布局
        field1 = new TextField(10);
        field2 = new TextField(10);
        field3 = new TextField(20);
        Label label = new Label("+");
        //Frame frame = new Frame("简易计算器");
        Button button = new Button("=");
        setLayout(new FlowLayout());
        add(field1);
        add(label);
        add(field2);
        add(button);
        add(field3);

        //给button添加事件监听
        button.addActionListener(new MyCalculatorFinal());

        windowClose(this);
        pack();
        setVisible(true);
    }
    //关闭按钮事件监听
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //事件监听类(内部类),内部类的好处就是可以随意使用外部类的属性方法
    class MyCalculatorFinal implements ActionListener {
        //命名变量
//        MyCalculator3 myCalculator3;
//        public MyCalculatorFinal(MyCalculator3 myCalculator3) {
//            this.myCalculator3 = myCalculator3;
//        }

        @Override
        public void actionPerformed(ActionEvent e) {
            //获取两个数
            int n1 = Integer.parseInt(field1.getText());
            int n2 = Integer.parseInt(field2.getText());
            //将两个数相加添加到最后一个框
            field3.setText("" +(n1+n2));
            //清除前边两个数
            field1.setText("");
            field2.setText("");
        }
    }

}

2.6 画笔Paint

public class TestMyPaint {
    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) {
        //super.paint(g);
        //画一个圆
        g.setColor(Color.BLUE);
        g.drawOval(100,100,100,100);
        //画一个失心圆
        g.setColor(Color.RED);
        g.fillRect(150,200,200,200);
    }
}

2.7 鼠标监听

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A7mAE4XX-1660291918738)(C:\Users\10357\AppData\Roaming\Typora\typora-user-images\image-20220810144654699.png)]

  • paint()方法重写后会自动调用,不用手动调用
  • 鼠标监听是获取鼠标点击点的坐标,这里用一个list把每个point记录下来
  • paint()方法是利用迭代器遍历list里边的所有点的位置,然后画出来(画一个小实心圆)
  • paint()方法只会执行一次,所以这时候在鼠标监听的每一次都repaint()一下,也就是重新遍历list集合,刷新界面(视觉上看就是点上去了,其实是电脑又重新画了一遍)
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画画");
    }
}

class MyFrame extends Frame {
    //属性
    ArrayList points;
    //方法
    public MyFrame(String string){
        super(string);
        setBounds(200,200,600,400);
        //points用来存放鼠标点击的点
        points = new ArrayList<Point>();
        //鼠标监听
        addMouseListener(new MyMouseListener());
        //关闭窗口监听
        windowClose(this);
        setVisible(true);
    }

    //重写paint()方法,利用迭代器把list里边的点都画出来
    @Override
    public void paint(Graphics g) {
        Iterator it = points.iterator();
        while (it.hasNext()){
            Point point = (Point) it.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);

        }
    }

    //关闭窗口监听
    private void windowClose (Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //内部类鼠标监听
    private class MyMouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            //把鼠标监听到的点存在list集合里边
            points.add(new Point(e.getX(),e.getY()));
            //每次监听完都要重新画一次
            repaint();
        }
    }
}

2.8 键盘监听

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

class KeyFrame extends Frame {
    public KeyFrame(){
        setBackground(Color.blue);
        setVisible(true);
        setBounds(200,200,600,400);
        //键盘监听
        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 JFrame窗口

public class Demo01 {
    public static void main(String[] args) {
        new MyJFrame01().init();
    }
}
class MyJFrame01 extends JFrame{
    public void init(){
        this.setBounds(10,10,400,500);
        this.setVisible(true);
        this.setBackground(Color.blue);
        JLabel label = new JLabel("欢迎");
        this.add(label);
        //让文本居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.yellow);
        //关闭事件操作
       // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//都可以
    }
}

/*
Java窗口是指JFrame或者Frame
其次,窗口背景颜色是指直接调用JFrame或者Frame的setBackground(Color color)方法设置后显示出来的颜色。
其实,J在你直接调用这个方法后,你的确设置了背景颜色,而你看到的却不是直接的JFrame或者Frame,而是JFrame.getContentPane().
而JFrame上的contentPane默认是Color.WHITE的,所以,无论你对JFrame或者Frame怎么设置背景颜色,你看到的都只是contentPane.

最后,讲解决办法:
办法A:在完成初始化,调用getContentPane()方法得到一个contentPane容器,然后将其设置为不可见,即setVisible(false)。这样,你就可以看到JFrame的庐山真面貌啦!
核心代码this.getContentPane().setVisible(false);//得到contentPane容器,设置为不可见
方法B:将contentPane的颜色设置为你想要的颜色,而不是对JFrame本身设置,
核心代码:this.getContentPane().setBackground(Color.red);//设置contentPane为红色
将核心代码替换方法A核心代码即可实现
方法C:为JFrame添加一个Panel或者JLabel等其他组件,设置其颜色为你想要的颜色,然后将其覆盖JFrame窗口即可
*/

好像可以理解成JFrame上面覆盖了一个白底,所有的操作都是在白底上面进行的,想要换一下设置颜色得先获得容器

如果设置容器不可见,那就只剩下一个蓝底别的什么都没有了

3.2 JDialog弹窗

public class TestJDialog extends JFrame {
    public TestJDialog() {
        //设置整体尺寸和可见度
        setBounds(200,200,600,400);
        setVisible(true);
        //实例化容器并设置自由布局
        Container container = this.getContentPane();
        container.setLayout(null);
        //添加Button并添加事件监听
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(180,120,200,50);//这个是一个相对坐标
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJDialog();
            }
        });

        container.add(button);
        //关闭事件监听
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestJDialog();
    }
}
//这里不用事件监听类了,上边已经用了lambda表达式
class MyJDialog extends JDialog{
    public MyJDialog() {
        //设置整体尺寸和可见度
        setBounds(300,220,400,400);//因为这个弹窗没有加到顶级弹窗上,所以这个是个绝对坐标
        setVisible(true);
        //实例化容器并设置自由布局
        Container container = getContentPane();
        
        container.setBackground(Color.red);
        //添加标签
        JLabel jLabel = new JLabel("这是弹窗里的标签");
        jLabel.setBackground(Color.green);
        //container.setLayout(null);
        //jLabel.setBounds(150,150,200,100);  
        jLabel.setHorizontalAlignment(SwingConstants.LEFT);
        container.add(jLabel);
        //弹窗就不用再加关闭事件监听了,因为它自己就带着
    }
}
container.setLayout(null);
jLabel.setHorizontalAlignment(SwingConstants.LEFT);

不能这么写,setLayout(null)是绝对布局,也就是用xy定位,只能用setBounds输入xy坐标

要么

setLayout(null)后输入坐标

要么

直接用写好的定位

3.3 标签

label

new Label("sadasd a");

Icon图标,需要实现接口,并且继承JFrame

public class TestIcon extends JFrame implements Icon {
    //属性
    private int width,height;
    //构造器
    public TestIcon(){}
    public TestIcon(int width, int height) {
        this.width = width;
        this.height = height;
    }

    //初始化
    public void init(){
        TestIcon icon = new TestIcon(50, 50);
        //图标放在标签上,也可以放在按钮上面
        JLabel label = new JLabel("icon", icon, SwingConstants.CENTER);

        //容器
        Container container = getContentPane();
        container.add(label);

        setBounds(200,200,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        new TestIcon().init();
    }
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,width,height);
    }
    @Override
    public int getIconWidth() {
        return width;
    }
    @Override
    public int getIconHeight() {
        return height;
    }
}

ImageIcon 图片

public class TestImageIcon extends JFrame{
    public TestImageIcon(){
        URL url = TestImageIcon.class.getResource("tx.png");//返回这个类所在地址目录下的这个文件的url地址
        ImageIcon imageIcon = new ImageIcon(url);
        JLabel label = new JLabel("ImageIcon");
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //容器
        Container container = getContentPane();
        container.add(label);

        setBounds(200,200,800,800);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestImageIcon();
    }
}

3.4 面板

  1. JPanel

  2. JScrollPane 可滚动面板

文本域TextAera+JScrollPane

public class TestJScrollDemo extends JFrame {
    public TestJScrollDemo() {
        Container container = getContentPane();
        //文本域
        TextArea textArea = new TextArea("欢迎来到德莱联盟");
        //Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        //布局操作
        setVisible(true);
        setBounds(200,200,600,600);
         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

3.5 按钮

图片按钮

public class TestJButtonDemo01 extends JFrame {
    public TestJButtonDemo01() {
        Container container = getContentPane();
        URL resource = TestJButtonDemo01.class.getResource("tx.png");
        Icon icon = new ImageIcon(resource);
        //把图篇放到按钮上
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        //鼠标放上去的时候给个提示
        jButton.setToolTipText("图片按钮");
        container.add(jButton);

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

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

单选框

public class TestJButtonDemo02 extends JFrame {
    public TestJButtonDemo02() {
        Container container = getContentPane();
        //单选框
        JRadioButton jRadioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton jRadioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton jRadioButton3 = new JRadioButton("JRadioButton3");
        //添加分组
        ButtonGroup group = new ButtonGroup();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        group.add(jRadioButton3);
        container.add(jRadioButton1,BorderLayout.NORTH);
        container.add(jRadioButton2,BorderLayout.CENTER);
        container.add(jRadioButton3,BorderLayout.SOUTH);

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

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

多选框

public class TestJButtonDemo03 extends JFrame {
    public TestJButtonDemo03() {
        Container container = getContentPane();
        //多选框
        JCheckBox jCheckBox01 = new JCheckBox("01");
        JCheckBox jCheckBox02 = new JCheckBox("02");
        container.add(jCheckBox01,BorderLayout.NORTH);
        container.add(jCheckBox02,BorderLayout.SOUTH);

        setVisible(true);
        //setSize(600,600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

3.6 列表

  • 下拉框
public class TestComboBox extends JFrame {
    public TestComboBox() {
        Container container = getContentPane();
        //下拉框
        JComboBox<String> status = new JComboBox<>();
        status.addItem(null);
        status.addItem("没上映");
        status.addItem("上映中");
        status.addItem("已下线");

        JPanel panel = new JPanel();
        panel.add(status);
        panel.setBackground(Color.red);
        container.add(panel);


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

    public static void main(String[] args) {
        new TestComboBox();
    }
}
  • 列表框
public class TestComboBoxDemo02 extends JFrame {
    public TestComboBoxDemo02() {
        Container container = getContentPane();
        //列表框JList
        String[] contents = {"1","2","3"};
        JList<String> list = new JList<>(contents);
        container.add(list);

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

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

3.7 文本框

  • 文本框
  • 密码框
public class TestTextDemo extends JFrame {
    public TestTextDemo() {
        Container container = getContentPane();
        //密码框
        JPasswordField PasswordField = new JPasswordField();
        PasswordField.setEchoChar('*');
        container.add(PasswordField);
        
        setVisible(true);
        setSize(600,600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestTextDemo();
    }
}
  • 文本域(面板那里有)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值