首页 > 编程语言 >Java图形界面设计-切换按钮复选按钮及单选按钮

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

时间:2023-03-17 22:34:38浏览次数:35  
标签:图形界面 复选 add 单选 选中 按钮 new

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);

标签:图形界面,复选,add,单选,选中,按钮,new
From: https://www.cnblogs.com/hsbt2333/p/17228451.html

相关文章

  • 前端如何实现一个可拖拽的按钮 转
    chatgpt的答案  https://chat.forchange.cn/   //获取按钮元素varbtn=document.getElementById("dragBtn");//定义变量,用于记录鼠标在按钮上的偏移量var......
  • Jetbrains Rider New UI找回工具栏上的前进后退按钮
    更新到最新版本并启用了NewUI之后,前进后退按钮没了,此时我们在设置的MenusandToobars->MainToobarLeft点添加MainMenu->Navigate->Back|Forward即可。......
  • wpf自定义控件库(二)——伪3D按钮
    1、以学习wpf为目的,同时也为了增加控件代码的复用性,开始建立自己的自定义控件库;2、目前主要是根据项目需求去增加,完善控件库。希望之后能一步步扩展更多更丰富的控件;3、......
  • EAS添加审核/反审核按钮功能
    一、在按钮添加完成后,按钮功能是没有生效的,需要编写代码进行相关处理。当然这是在本人的开发环境下面。二、这个时候,就需要用到按钮的name属性了。所以添加按钮时,name属性......
  • 排序算法 之 (简单选择排序)
    10.5、简单选择排序这个算法的思想很简单,每次选择从没有排序的元素中选择最小(大)元素放到到前(后)面简单选择排序是不稳定的简单选择排序代码实现#include<stdio.h>#i......
  • 安卓菜单选项 Popup_Menu / Context_Menu / Option_Menu
    Option_Menu右上角的菜单optionmenu.xml这个页面没啥用<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.......
  • 在VMware虚拟机上安装CentOS 7,以及CentOS 7图形界面的安装
    一、安装VM虚拟机1.1下载虚拟机软件,官方地址位:http://www.VMware.com1.2下载完VM虚拟机后进行安装(由于已经安装完成,就不进行图片描述)下载完成后,双击打开.exe文件,首先......
  • Android中点击按钮获取星级评分条的评分
    场景效果 注:关注公众号霸道的程序猿获取编程相关电子书、教程推送与免费下载。实现将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添......
  • [CSS] 胶囊按钮与border-radius
    胶囊按钮高度固定以前在写这种形状的按钮的时候,我一般是写了一个固定高度,然后border-radius设置为高度的一半:.pill-button{width:auto;min-width:128px;......
  • 简单选择排序
    背景爱炒股票短线的人,总是喜欢不断的买进卖出,想通过价差来实现盈利。但通常这种频繁操作的人,即使失误不多,也会因为操作的手续费和印花税过高而获得很少。还有一种做股票......