• 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);
        }
    }
  • 相关阅读:
    [附源码]java毕业设计儿童资源教育网站
    2022系统架构师考试---冲刺文档整理
    动态跳过测试用例
    C# Microsoft.Office.Interop.Word设置Word页脚之添加当前页数
    【操作系统】1.3.1 操作系统的运行机制
    形态学算法应用之重建开操作的python实现——数字图像处理
    【Java进阶篇】第二章 Java数组(下篇) 二维数组
    Python解释器与Python编辑器的详细下载与安装过程
    最好用的Linux系统磁盘数据修复工具合集!
    python绘制动图,保存gif:(matplotlib.animation.FuncAnimation)
  • 原文地址:https://blog.csdn.net/yujiyai/article/details/133248033