日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java radio 不可选_在Java Swing中取消选择RadioButtons

發布時間:2024/9/27 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java radio 不可选_在Java Swing中取消选择RadioButtons 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當顯示一組JRadioButton時,最初沒有選擇任何一個(除非您以編程方式強制執行)。我想要能夠將按鈕恢復到該狀態,即使在用戶已經選擇了一個之后,即,不應該選擇任何按鈕。

然而,使用通常的嫌疑不會提供所需的效果:調用’setSelected(false)’在每個按鈕不工作。有趣的是,當按鈕未放入ButtonGroup時,它會工作 – 不幸的是,后者是JRadioButtons要求是互斥的。

此外,使用javax.swing.ButtonGroup的setSelected(ButtonModel,boolean)方法不會做我想要的。

我把一個小程序來展示效果:兩個單選按鈕和一個JButton。單擊JButton應取消選擇單選按鈕,以使窗口看起來與它首次彈出時的完全一樣。

import java.awt.Container;

import java.awt.GridLayout;

import java.awt.event.*;

import javax.swing.*;

/**

* This class creates two radio buttons and a JButton. Initially, none

* of the radio buttons is selected. Clicking on the JButton should

* always return the radio buttons into that initial state, i.e.,

* should disable both radio buttons.

*/

public class RadioTest implements ActionListener {

/* create two radio buttons and a group */

private JRadioButton button1 = new JRadioButton("button1");

private JRadioButton button2 = new JRadioButton("button2");

private ButtonGroup group = new ButtonGroup();

/* clicking this button should unselect both button1 and button2 */

private JButton unselectRadio = new JButton("Unselect radio buttons.");

/* In the constructor, set up the group and event listening */

public RadioTest() {

/* put the radio buttons in a group so they become mutually

* exclusive -- without this, unselecting actually works! */

group.add(button1);

group.add(button2);

/* listen to clicks on 'unselectRadio' button */

unselectRadio.addActionListener(this);

}

/* called when 'unselectRadio' is clicked */

public void actionPerformed(ActionEvent e) {

/* variant1: disable both buttons directly.

* ...doesn't work */

button1.setSelected(false);

button2.setSelected(false);

/* variant2: disable the selection via the button group.

* ...doesn't work either */

group.setSelected(group.getSelection(), false);

}

/* Test: create a JFrame which displays the two radio buttons and

* the unselect-button */

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

RadioTest test = new RadioTest();

Container contentPane = frame.getContentPane();

contentPane.setLayout(new GridLayout(3,1));

contentPane.add(test.button1);

contentPane.add(test.button2);

contentPane.add(test.unselectRadio);

frame.setSize(400, 400);

frame.setVisible(true);

}

}

任何想法任何人?謝謝!

總結

以上是生活随笔為你收集整理的java radio 不可选_在Java Swing中取消选择RadioButtons的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。