Java图形界面设计-切换按钮复选按钮及单选按钮

Java程序设计语言(一) 示例程序

P164 程序8.5

与书上程序不完全一样,匿名类使用 lambda

使用 jdk1.8.0_311

package tech.bugstar.practice.gui;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;

public class JFrame85 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("P164 程序8.5");
        //窗体左上角居中
        frame.setLocationRelativeTo(null);
        Container contentPane = frame.getContentPane();
        切换按钮复选按钮和单选按钮示例(contentPane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static void 切换按钮复选按钮和单选按钮示例(Container contentPane) {
        JCheckBox checkBox1 = new JCheckBox("复选按钮 1");
        JCheckBox checkBox2 = new JCheckBox("复选按钮 2");
        JCheckBox checkBox3 = new JCheckBox("复选按钮 3");
        JCheckBox checkBox4 = new JCheckBox("复选按钮 4");
        JCheckBox checkBox5 = new JCheckBox("复选按钮 5");
        JCheckBox checkBox6 = new JCheckBox("复选按钮 6");

        JRadioButton radioButton1 = new JRadioButton("单选按钮 1");
        JRadioButton radioButton2 = new JRadioButton("单选按钮 2");
        JRadioButton radioButton3 = new JRadioButton("单选按钮 3");
        JRadioButton radioButton4 = new JRadioButton("单选按钮 4");
        JRadioButton radioButton5 = new JRadioButton("单选按钮 5");
        JRadioButton radioButton6 = new JRadioButton("单选按钮 6");
        JTextArea textArea = new JTextArea();

        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        JPanel p4 = new JPanel();
        JPanel p5 = new JPanel();
        JPanel pa = new JPanel();
        JPanel pb = new JPanel();

        p1.add(checkBox1);
        p1.add(checkBox2);
        p1.add(checkBox3);
        Border etchedBorder = BorderFactory.createEtchedBorder();
        Border border = BorderFactory.createTitledBorder(etchedBorder, "复选按钮");
        p1.setBorder(border);

        p2.add(checkBox4);
        p2.add(checkBox5);
        p2.add(checkBox6);
        border = BorderFactory.createTitledBorder(etchedBorder, "复选按钮组");
        p2.setBorder(border);

        ButtonGroup group1 = new ButtonGroup();
        group1.add(checkBox4);
        group1.add(checkBox5);
        group1.add(checkBox6);

        p3.add(radioButton1);
        p3.add(radioButton2);
        p3.add(radioButton3);
        border = BorderFactory.createTitledBorder(etchedBorder, "单选按钮");
        p3.setBorder(border);

        p4.add(radioButton4);
        p4.add(radioButton5);
        p4.add(radioButton6);
        border = BorderFactory.createTitledBorder(etchedBorder, "单选按钮组");
        p4.setBorder(border);
        ButtonGroup group2 = new ButtonGroup();
        group2.add(radioButton4);
        group2.add(radioButton5);
        group2.add(radioButton6);

        JScrollPane scrollPane = new JScrollPane(textArea);
        p5.setLayout(new BorderLayout());
        p5.add(scrollPane);
        border = BorderFactory.createTitledBorder(etchedBorder, "输出");
        p5.setBorder(border);

        ItemListener itemListener = e->{
            JCheckBox checkBox = (JCheckBox) e.getSource();
            if(checkBox == checkBox1) textArea.append("\n" + (checkBox1.isSelected()?"选中":"取消选中") + " 复选按钮 1 ");
            else if(checkBox == checkBox2) textArea.append("\n" + (checkBox2.isSelected()?"选中":"取消选中") + " 复选按钮 2 ");
            else if(checkBox == checkBox3) textArea.append("\n" + (checkBox3.isSelected()?"选中":"取消选中") + " 复选按钮 3 ");
            else if(checkBox == checkBox4) textArea.append("\n" + (checkBox4.isSelected()?"选中":"取消选中") + " 复选按钮 4 ");
            else if(checkBox == checkBox5) textArea.append("\n" + (checkBox5.isSelected()?"选中":"取消选中") + " 复选按钮 5 ");
            else textArea.append("\n" + (checkBox6.isSelected()?"选中":"取消选中") + " 复选框按钮 6 ");
        };
        checkBox1.addItemListener(itemListener);
        checkBox2.addItemListener(itemListener);
        checkBox3.addItemListener(itemListener);
        checkBox4.addItemListener(itemListener);
        checkBox5.addItemListener(itemListener);
        checkBox6.addItemListener(itemListener);

        ActionListener actionListener = e->{
            JRadioButton radioButton = (JRadioButton) e.getSource();
            if(radioButton == radioButton1) textArea.append("\n" + (radioButton1.isSelected()?"选中":"取消选中") + " 单选按钮 1 ");
            else if(radioButton == radioButton2) textArea.append("\n" + (radioButton2.isSelected()?"选中":"取消选中") + " 单选按钮 2 ");
            else if(radioButton == radioButton3) textArea.append("\n" + (radioButton3.isSelected()?"选中":"取消选中") + " 单选按钮 3 ");
            else if(radioButton == radioButton4) textArea.append("\n" + (radioButton4.isSelected()?"选中":"取消选中") + " 单选按钮 4 ");
            else if(radioButton == radioButton5) textArea.append("\n" + (radioButton5.isSelected()?"选中":"取消选中") + " 单选按钮 5 ");
            else textArea.append("\n" + (radioButton6.isSelected()?"选中":"取消选中")+" 单选按钮 6 ");
        };
        radioButton1.addActionListener(actionListener);
        radioButton2.addActionListener(actionListener);
        radioButton3.addActionListener(actionListener);
        radioButton4.addActionListener(actionListener);
        radioButton5.addActionListener(actionListener);
        radioButton6.addActionListener(actionListener);

        pa.setLayout(new GridLayout(0,1));
        pa.add(p1);
        pa.add(p2);
        pb.setLayout(new GridLayout(0,1));
        pb.add(p3);
        pb.add(p4);
        contentPane.setLayout(new GridLayout(0,1));
        contentPane.add(pa);
        contentPane.add(pb);
        contentPane.add(p5);
    }
}

程序启动界面

image

选择复选按钮1

image

全选复选按钮123

image

先选复选按钮4再选复选按钮5

image

选择单选按钮1

image

全选单选按钮123

image

先选单选按钮6再选单选按钮5

image

说明

具有两种状态的按钮不仅可以注册 ActionEvent 事件监听程序,还可以注册 ItemEvent 事件监听程序。ItemListener 接口当按钮的状态发生改变时将会调用该类方法。

ItemListener, ActionListener 的区别在该程序中就是 ItemListener 会监听按钮组里面取消选中的操作,而 ActionListener不会。

ActionEvent 和 ItemEvent 都提供了 getSource() 方法,但返回对象是 Object,需要强转才能使用,ItemEvent 还有个 getItem()方法,返回值和用法和 getSource() 一致。

isSelected() 返回 true 或者 false,表名按钮是否选中

JCheckBox 加入按钮组之后只能单选,JRadioButton 如果不加入按钮组也可以多选,但是通常用 JCheckBox 表示可多选的选择项(不加入按钮组),而用 JRadioButton 表示只能单选的选择项(加入按钮组),因此 JCheckBox 称为复选按钮,而 JRadioButton 被称为单选按钮

其他说明

//设置边框
Border etchedBorder = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(etchedBorder, "复选按钮");
jPanel.setBorder(border);

//加入按钮组
ButtonGroup group1 = new ButtonGroup();
group1.add(checkBox4);
group1.add(checkBox5);
group1.add(checkBox6);

//文本框
JTextArea textArea = new JTextArea();
//带滚动条面板
JScrollPane scrollPane = new JScrollPane(textArea);

posted @ 2023-03-17 22:26  快乐在角落里  阅读(173)  评论(0编辑  收藏  举报