zoukankan      html  css  js  c++  java
  • Java:GUI-Swing(下)

    学习资料

    b站狂神说:https://www.bilibili.com/video/BV1DJ411B75F

    按钮

    单选按钮

    package com.zy7y.gui.swing;
    
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * @ProjectName: JavaSE
     * @PackageName: com.zy7y.gui.swing
     * @Author: zy7y
     * @Date: 2020/8/16 下午2:47
     * @Description: 按钮
     */
    public class TestJButton extends JFrame {
        public TestJButton(){
            setTitle("单选按钮");
            Container container = getContentPane();
            setBounds(500,500,500,500);
    
            // 单选按钮, 单选按钮只能选择一个,需要用到分组
            JRadioButton jRadioButton = new JRadioButton("男");
            JRadioButton jRadioButton1 = new JRadioButton("女");
    
            ButtonGroup buttonGroup = new ButtonGroup();
            buttonGroup.add(jRadioButton);
            buttonGroup.add(jRadioButton1);
    
            container.add(jRadioButton, BorderLayout.WEST);
            container.add(jRadioButton1, BorderLayout.EAST);
    
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
        }
    
        public static void main(String[] args) {
            new TestJButton();
        }
    }
    

    多选按钮

    Snipaste_2020-08-16_15-01-12.png

    package com.zy7y.gui.swing;
    
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * @ProjectName: JavaSE
     * @PackageName: com.zy7y.gui.swing
     * @Author: zy7y
     * @Date: 2020/8/16 下午2:47
     * @Description: 按钮
     */
    public class TestJButton extends JFrame {
        public TestJButton(){
            setTitle("多选按钮");
            Container container = getContentPane();
            setBounds(500,500,500,500);
    
            // 多选按钮
            Checkbox checkbox = new Checkbox("多选项");
            Checkbox checkbox1 = new Checkbox("多选项1");
            Checkbox checkbox2 = new Checkbox("多选项2");
          
            container.add(checkbox, BorderLayout.WEST);
            container.add(checkbox1, BorderLayout.EAST);
            container.add(checkbox2, BorderLayout.CENTER);
    
            setVisible(true);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
        }
    
        public static void main(String[] args) {
            new TestJButton();
        }
    }
    
    

    列表

    下拉列表选择框

    Snipaste_2020-08-16_15-21-29.png

    package com.zy7y.gui.swing;
    
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * @ProjectName: JavaSE
     * @PackageName: com.zy7y.gui.swing
     * @Author: zy7y
     * @Date: 2020/8/16 下午3:10
     * @Description: 下拉框
     */
    public class TestJComBox extends JFrame {
        public TestJComBox(){
            setTitle("下拉列表-JComboBox");
            setVisible(true);
            setBounds(300,100,300,300);
            Container container = getContentPane();
            JComboBox jComboBox = new JComboBox();
            jComboBox.addItem("男");
            jComboBox.addItem("女");
    
            container.add(jComboBox);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestJComBox();
        }
    }
    

    列表数据展示

    Snipaste_2020-08-16_15-27-39.png

    package com.zy7y.gui.swing;
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Vector;
    
    /**
     * @ProjectName: JavaSE
     * @PackageName: com.zy7y.gui.swing
     * @Author: zy7y
     * @Date: 2020/8/16 下午3:10
     * @Description: 下拉框
     */
    public class TestJComBox extends JFrame {
        public TestJComBox(String title){
            super(title);
            setVisible(true);
            setBounds(300,100,300,300);
            Container container = getContentPane();
    
            // 线程安全的数组
            Vector vector = new Vector();
            JList jList = new JList(vector);
    
            // 给数组添加东西
            vector.add("Java");
            vector.add("Python");
            vector.add("Golang");
    
            container.add(jList);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestJComBox("列表数据展示");
        }
    }
    

    文本框

    Snipaste_2020-08-16_15-40-47.png

    package com.zy7y.gui.swing;
    
    import javax.swing.*;
    import java.awt.*;
    
    /**
     * @ProjectName: JavaSE
     * @PackageName: com.zy7y.gui.swing
     * @Author: zy7y
     * @Date: 2020/8/16 下午3:29
     * @Description: 文本框、密码框、文本域
     */
    public class TestText extends JFrame{
        public TestText(){
            setTitle("文本框、密码框");
            setVisible(true);
            setBounds(300,100,300,300);
            Container container = getContentPane();
    
            // 20个字符长度的文本框,有个默认值 "文本框"
            JTextField jTextField = new JTextField("文本框",20);
            container.add(jTextField, BorderLayout.AFTER_LAST_LINE);
    
            // 密码框,16长度
            JPasswordField jPasswordField = new JPasswordField(16);
            container.add(jPasswordField, BorderLayout.AFTER_LINE_ENDS);
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new TestText();
        }
    }
    
    作者:zy7y
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    xpath和lxml库
    pipenv和autoenv
    正则表达式
    requests库
    Photoshop学习笔记
    Firebug使用笔记
    JavaScript中的声明提升(Hoisting )
    JavaScript中的继承机制
    JavaScript中的instanceof原理详解
    JSP使用JSON传递数据,注意避免中文乱码
  • 原文地址:https://www.cnblogs.com/zy7y/p/13512774.html
Copyright © 2011-2022 走看看