• 【GUI】-- 09 JComboBox & JList、JTextField & JPasswordField & JTextArea


    GUI编程

    03 Swing

    3.6 列表

    1. 下拉框
    package com.duo.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class ComboBoxDemo01 extends JFrame {
        public ComboBoxDemo01() throws HeadlessException {
            Container contentPane = getContentPane();
    
            JComboBox<Object> objectJComboBox = new JComboBox<>();
            objectJComboBox.addItem(null);
            objectJComboBox.addItem("热映中");
            objectJComboBox.addItem("即将上映");
            objectJComboBox.addItem("已下架");
    
            objectJComboBox.setFont(new Font("宋体", Font.BOLD, 20));
    
            contentPane.add(objectJComboBox);
    
            setTitle("下拉列表测试窗口");
            setVisible(true);
            setSize(350, 100);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new ComboBoxDemo01();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    运行结果:

    图1

    1. 列表框
    package com.duo.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Vector;
    
    public class ComboBoxDemo02 extends JFrame {
        public ComboBoxDemo02() {
            Container contentPane = getContentPane();
    
            //先生成一个列表
            String[] contents = {"One", "Two", "Three"};
            //用于展示contents的列表框
            JList<Object> objectJList1 = new JList<>(contents);
    
            contentPane.add(objectJList1);
    
            setTitle("列表框测试窗口");
            setVisible(true);
            setSize(650, 500);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new ComboBoxDemo02();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    运行结果:

    图2

    如图,列表框JList用于展示列表中的参数量。

    列表的具体应用场景:

    • 下拉列表:选择地区,或一些单个的选项
    • 列表框:展示信息(可以自动动态扩容)

    3.7 文本框

    1. 文本框
    package com.duo.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TextDemo01 extends JFrame {
        public TextDemo01() {
            Container contentPane = getContentPane();
            contentPane.setLayout(null);  //设置为绝对布局
    
            JTextField jTextField = new JTextField("Hello", 20);
            jTextField.setBounds(20, 20, 100, 25);
            JTextField jTextField2 = new JTextField("world", 20);
            jTextField2.setBounds(20, 50, 100, 25);
    
            contentPane.add(jTextField);
            contentPane.add(jTextField2);
    
            setTitle("文本框测试窗口");
            setVisible(true);
            setSize(650, 500);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TextDemo01();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    运行结果:

    图3

    如上,此窗口面板设为了绝对布局。

    1. 密码框
    package com.duo.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TextDemo02 extends JFrame {
        public TextDemo02() {
            Container contentPane = getContentPane();
    
            JPasswordField jPasswordField = new JPasswordField();  //密码框文本域默认输入为小黑圆点
            //jPasswordField.setEchoChar('*');  //同样可通过语句来设置输入文本后的显示样式为'*'
            contentPane.add(jPasswordField);
    
            setTitle("文本框测试窗口");
            setVisible(true);
            setSize(650, 500);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TextDemo02();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    运行结果:

    图4

    1. 文本域
    package com.duo.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TextDemo03 extends JFrame {
    
        public TextDemo03() {
            Container contentPane = getContentPane();
            contentPane.setLayout(null);
    
            JTextArea jTextArea = new JTextArea("This is Schrodinger's channel.");
            jTextArea.setFont(new Font("Times New Roman", Font.PLAIN, 22));
            Dimension preferredSize = jTextArea.getPreferredSize();  //获得文本域的首选大小
            jTextArea.setBounds((650 - preferredSize.width) / 2,
                    (500 - preferredSize.height) / 2 - preferredSize.height / 2,
                    preferredSize.width, preferredSize.height);
    
            contentPane.add(jTextArea);
    
            setTitle("文本域测试窗口");
            setVisible(true);
            setSize(650, 500);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TextDemo03();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    运行结果:

    图5


  • 相关阅读:
    Docker常用命令
    Java Collections.list()方法具有什么功能呢?
    python mysql语句中有单引号执行的报错处理方式
    力扣1704
    代码质量与安全 | 想在发布竞赛中胜出?Sonar来帮你
    webpack定制化 加载与插件[css加载器、html插件、image打包配置、babel代码兼容、vue加载器及配置]
    温控采集器对接-java版-modbus4j
    bean的作用域、bean的生命周期、bean的后置处理器
    算法思想之回溯法
    nginx优先级、规则及重定向
  • 原文地址:https://blog.csdn.net/qq_51916086/article/details/134431117