绊夏微凉

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

swing

​ swing是对awt的延伸,提供更多的功能。

基本

​ swing窗口颜色的设置以及放置组件,一般都先获取窗口的容器,再设置容器的颜色,将组件放到容器中。

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局
        container.setLayout(new FlowLayout());
        // 设置容器颜色
        container.setBackground(Color.BLACK);
        // 设置按钮
        JButton btn = new JButton("按钮");
        // 按钮放到容情中
        container.add(btn);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

窗口关闭:

​ awt:

addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

​ swing:

 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

文本框

文本框(JTextField)

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则组件会占满整个容器
        container.setLayout(new FlowLayout());
        // 设置文本框
        JTextField textField = new JTextField(20);
        // 文本框放入容器中
        container.add(textField);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

密码框

密码框(JPasswordField)

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则组件会占满整个容器
        container.setLayout(new FlowLayout());
        // 设置密码框
        JPasswordField passwordField = new JPasswordField(10);
        // 密码框放入面板中
        container.add(passwordField);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

文本域

文本域(JTextArea)

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置文本域
        JTextArea textArea = new JTextArea();
        // 文本域放入容器中
        container.add(textArea);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

按钮

按钮(JButton)

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则按钮会占满整个容器
        container.setLayout(new FlowLayout());
        // 设置按钮
        JButton btn = new JButton("按钮");
        // 按钮放入容器
        container.add(btn);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

单选按钮

单选按钮(JRadioButton):要把单选按钮放入同一个按钮组中才能实现单选的

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则组件会占满整个容器
        container.setLayout(new FlowLayout());
        // 设置单选按钮
        JRadioButton radioButton_1 = new JRadioButton("btn1");
        JRadioButton radioButton_2 = new JRadioButton("btn2");
        JRadioButton radioButton_3 = new JRadioButton("btn3");
        // 设置按钮组
        ButtonGroup group = new ButtonGroup();
        // 把单选按钮放到一个组中
        group.add(radioButton_1);
        group.add(radioButton_2);
        group.add(radioButton_3);
        // 按钮放入容器
        container.add(radioButton_1);
        container.add(radioButton_2);
        container.add(radioButton_3);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

多选按钮

多选按钮(JCheckBox)

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则组件会占满整个容器
        container.setLayout(new FlowLayout());
        // 设置复选按钮
        JCheckBox checkBox_1 = new JCheckBox("btn1");
        JCheckBox checkBox_2 = new JCheckBox("btn2");
        // 复选按钮放入容器中
        container.add(checkBox_1);
        container.add(checkBox_2);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

下拉列表

下拉列表(JComboBox):先定义下拉列表的内容,再作为构造参数,传入构造方法中,下拉列表的内容以数组的形式传入。

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则组件会占满整个容器
        container.setLayout(new FlowLayout());
        // 下拉列表内容
        String[] years = {"2021", "2020", "2019"};
        // 设置下拉列表
        JComboBox jComboBox = new JComboBox(years);
        // 下拉列表添加到容器中
        container.add(jComboBox);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

列表

列表(JList):先定义列表的内容,再作为构造参数,传入构造方法中,列表的内容以数组的形式传入。

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,否则组件会占满整个容器
        container.setLayout(new FlowLayout());
        // 列表展示的数据
        String[] years = {"2021", "2020", "2019"};
        // 设置列表
        JList list = new JList(years);
        // 将列表添加到面板中
        container.add(list);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

弹框

​ 弹框:JDialog

​ 弹框类似于窗口,弹框的关闭按钮默认实现。

// 点击按钮,弹出弹框
public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,不然按钮会占满整个窗口
        container.setLayout(new FlowLayout());
        // 设置按钮
        JButton btn = new JButton("窗口按钮");
        // 按钮放置容器
        container.add(btn);
        // 按钮绑定事件监听
        btn.addActionListener(new MyActionListener());

        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

    class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // 创建弹框
            JDialog dialog = new JDialog();
            // 设置弹框大小以及坐标
            dialog.setBounds(3, 3, 100, 100);
            // 设置弹框可见
            dialog.setVisible(true);
            // 获取弹框容器
            Container container = dialog.getContentPane();
            // 设置容器布局,否则按钮占满整个窗口
            container.setLayout(new FlowLayout());
            // 设置按钮
            JButton btn = new JButton("弹框按钮");
            // 按钮放置弹框容易
            container.add(btn);
        }
    }
}

标签

​ 标签包括JLabel,Icon,ImageIcon

JLabel:

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,不然按钮会占满整个窗口
        container.setLayout(new FlowLayout());
        // 设置标签
        JLabel label = new JLabel("标签");
        // 容器添加标签
        container.add(label);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

Icon:

​ Icon不能直接添加到容器中,可以添加到JLabel等组件中,自定义Icon要实现Icon接口

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,不然按钮会占满整个窗口
        container.setLayout(new FlowLayout());
        // 设置图标的大小
        Icon icon = new MyIcon(10, 10);
        // 设置标签,把图标放到标签中
        JLabel label = new JLabel(icon);
        // 设置按钮,把图标放到按钮中
        JButton button = new JButton("按钮", icon);
        // 容器添加标签
        container.add(label);
        // 容器添加按钮
        container.add(button);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

    class MyIcon implements Icon {
        // 长度
        private int width;
        // 宽度
        private int height;

        public MyIcon() {

        }
        public MyIcon(int width, int height) {
            this.width = width;
            this.height = height;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            // 画个实心圆
            g.fillOval(x, y, this.width, this.width);
        }

        @Override
        public int getIconWidth() {
            return this.width;
        }

        @Override
        public int getIconHeight() {
            return this.width;
        }
    }
}

ImageIcon:是icon的扩展,直接获取图片,也不能直接添加到容器中,得添加到JLabel等组件中。

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置容器布局,不然按钮会占满整个窗口
        container.setLayout(new FlowLayout());
        // 获取图片地址, 图片放在同一个包下,类.class.getResource,类为最外围public修饰的类
        URL url = Application.class.getResource("2.jpg");
        // 图标添加图片地址
        ImageIcon imageIcon = new ImageIcon(url);
        // 图标添加到标签中
        JLabel label = new JLabel(imageIcon);
        JButton btn = new JButton("按钮", imageIcon);
        container.add(btn);
        // 容器添加标签
        container.add(label);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }
}

面板

面板:一般面板(JPanel)和滚动面板(JScrollPane)

JPanel

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        container.setLayout(new GridLayout(2, 1));
        // 设置面板
        JPanel panel_1 = new JPanel(new GridLayout(2, 1));
        JPanel panel_2 = new JPanel(new GridLayout(1, 2));
        // 设置按钮
        JButton btn_1 = new JButton("btn_1");
        JButton btn_2 = new JButton("btn_2");
        JButton btn_3 = new JButton("btn_3");
        JButton btn_4 = new JButton("btn_4");
        // 将按钮添加到面板中
        panel_1.add(btn_1);
        panel_1.add(btn_2);
        panel_2.add(btn_3);
        panel_2.add(btn_4);
        // 将面板添加到容器中
        container.add(panel_1);
        container.add(panel_2);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}

JScrollPane:组件是作为构造参数传进去的,不是使用add,不然组件会不显示

public class Application {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends JFrame {

    public MyFrame (){
        init();
    }

    public void init() {
        // 设置窗体的位置以及大小
        setBounds(200, 200, 500, 600);
        // 获取容器
        Container container = getContentPane();
        // 设置文本域
        JTextArea textArea = new JTextArea();
        // 设置滚动面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        // 容器添加滚动面板
        container.add(scrollPane);
        // 关闭窗口
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体可见
        setVisible(true);
    }

}
posted on 2021-04-15 11:34  绊夏微凉  阅读(85)  评论(0)    收藏  举报