• Swing02


    一、按钮

    • 图片按钮

    public class TestJButtonIcon extends JFrame {
        public static void main(String[] args) {
            new TestJButtonIcon();
        }
    ​
        public TestJButtonIcon()  {
            Container contentPane = getContentPane();
            URL url = TestJButtonIcon.class.getResource("迦南.png");
            ImageIcon imageIcon = new ImageIcon(url);
            JButton jButton = new JButton(imageIcon);
            jButton.setToolTipText("图片按钮");
            contentPane.add(jButton);
    ​
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
        }
    }

    • 单选框

    public class TestRadioButton extends JFrame {
        public static void main(String[] args) {
            new TestRadioButton();
        }
    ​
        public TestRadioButton() {
            Container contentPane = getContentPane();
            //单选框
            JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
            JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
            JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");
    ​
            //由于单选框只能选择一个,一组中只能选择一个
            ButtonGroup buttonGroup = new ButtonGroup();
            buttonGroup.add(jRadioButton01);
            buttonGroup.add(jRadioButton02);
            buttonGroup.add(jRadioButton03);
    ​
            contentPane.add(jRadioButton01,BorderLayout.CENTER);
            contentPane.add(jRadioButton02,BorderLayout.NORTH);
            contentPane.add(jRadioButton03,BorderLayout.SOUTH);
    ​
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
    ​
        }
    }

    • 多选框

    public class TestCheckBox extends JFrame {
        public static void main(String[] args) {
            new TestCheckBox();
        }
    ​
        public TestCheckBox() {
            Container contentPane = getContentPane();
    ​
            JCheckBox jCheckBox1 = new JCheckBox("JCheckBox1");
            JCheckBox jCheckBox2 = new JCheckBox("JCheckBox2");
            JCheckBox jCheckBox3 = new JCheckBox("JCheckBox3");
    ​
            contentPane.add(jCheckBox1,BorderLayout.SOUTH);
            contentPane.add(jCheckBox2,BorderLayout.CENTER);
            contentPane.add(jCheckBox3,BorderLayout.NORTH);
    ​
    ​
    ​
    ​
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
            this.setVisible(true);
        }
    }

    二、列表

    • 下拉框

    public class TestComboBox extends JFrame {
        public static void main(String[] args) {
            new TestComboBox();
        }
        public TestComboBox(){
            Container contentPane = getContentPane();
    ​
            JComboBox status = new JComboBox<>();
    ​
            status.addItem("null");
            status.addItem("选项一");
            status.addItem("选项二");
            status.addItem("选项三");
    ​
            contentPane.add(status);
    ​
    ​
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
            this.setVisible(true);
        }
    }
    • 列表框

    public class TestJList extends JFrame {
        public static void main(String[] args) {
        new TestJList();
        }
        public TestJList(){
            Container contentPane = getContentPane();
            //String[] array ={"1","2","3"};
            Vector array = new Vector<>();
            JList list = new JList<>(array);
            array.add("zhangsan");
            array.add("lisi");
            array.add("wangwu");
    ​
            contentPane.add(list);
    ​
    ​
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
        }
    }
    • 应用场景

      • 选择地区或者一些单个选项

      • 列表,展示一些信息,一般是动态扩容


    三、文本框

    • 文本框

    public class TestJTextField extends JFrame {
        public static void main(String[] args) {
            new TestJTextField();
        }
        public TestJTextField(){
            Container contentPane = getContentPane();
            //this.setLayOut(null);
            JTextField hello = new JTextField("hello");
            JTextField world = new JTextField("world", 20);
    ​
            contentPane.add(hello,BorderLayout.NORTH);
            contentPane.add(world,BorderLayout.SOUTH);
    ​
            this.setSize(500,500);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    }
    • 密码框

    public class TestPassword extends JFrame {
        public static void main(String[] args) {
            new TestPassword();
        }
    ​
        public TestPassword()  {
            Container contentPane = getContentPane();
    ​
            JPasswordField jPasswordField = new JPasswordField();
            jPasswordField.setEchoChar('*');
    ​
            contentPane.add(jPasswordField);
    ​
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            this.setSize(500,500);
            this.setVisible(true);
        }
    }

    • 文本域

    public class JScrollDemo extends JFrame {
        public static void main(String[] args) {
            new JScrollDemo();
        }
        public JScrollDemo(){
            Container contentPane = getContentPane();
    ​
            //文本域
            JTextArea jTextArea = new JTextArea(20, 50);
            jTextArea.setText("刘铁锤真的棒");
    ​
            //Scroll面板
            JScrollPane jScrollPane = new JScrollPane(jTextArea);
            contentPane.add(jScrollPane);
    ​
            setVisible(true);
            setBounds(100,100,300,350);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    }
  • 相关阅读:
    已有项目与git建立连接、老项目搭建git管理
    若依分离版——配置多数据源(mysql和oracle),实现一个方法操作多个数据源
    原型,原型链的理解
    RocketMQ源码(8)—Producer发送消息源码(2)—单向、同步、异步发送消息【一万字】
    学期伊始,来填两年前的flag,这是我的笔记博客网站!
    Move 合约漏洞,Move 合约中最常见的 10 种 Bug
    “大图模型”亮相外滩大会,蚂蚁开创大模型与图计算融合研究
    关于XXLJOB集群模式下调度失败的问题
    高效、优雅的对象copy之MapStruct入门到精通,实战踩坑版
    Redis专题----2
  • 原文地址:https://blog.csdn.net/yujiyai/article/details/133248033