单选按钮不显示选定状态

发布于 2024-12-29 03:49:46 字数 5869 浏览 6 评论 0原文

通过下面的代码片段,就实现了开关机的功能。
但是,视觉效果不会将单选按钮显示为“已选择”。
我正在寻找显示所选视觉效果的帮助/方向
在暂停和清除组合之前(请参阅底部的听众)

提前致谢
高手

//
// The idea is to have a combo box for device selection
// and radio buttons to select on/off for the device
// the buttons are NOT available until a combo selection is made
// The button should show selected before the combo box is reset
//

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JTabbedPane;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;

public class itsGUI extends JFrame 
{
private static final long serialVersionUID = 1L;
static itsGUI       yeahItsGUI;

// Main panel
JPanel              jpMain;
JTabbedPane         jtpMain;
GridBagLayout       gblMain = new GridBagLayout();
GridBagConstraints  gbcMain = new GridBagConstraints();

// Administrator tab
JPanel              jpAdmin;
GridBagLayout       gblAdmin = new GridBagLayout();
GridBagConstraints  gbcAdmin = new GridBagConstraints();
ButtonGroup         bgAdmin;
JComboBox           jcbDevice;
JRadioButton        jrbOn;
JRadioButton        jrbOff;

// local
int                 DeviceIndex  = 0;
String              DeviceName = null;


public static void main( String args[] ) 
{
   try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
   catch ( ClassNotFoundException e ) {}
   catch ( InstantiationException e ) {}
   catch ( IllegalAccessException e ) {} 
   catch ( UnsupportedLookAndFeelException e ) {}

   yeahItsGUI = new itsGUI();
   } /* main */


public itsGUI() 
{
   super( "Button Group" );

   jpMain = new JPanel();
   jpMain.setLayout( gblMain );
   jpMain.setPreferredSize(new Dimension(200, 100));

   jtpMain = new JTabbedPane( );

   jpAdmin = new JPanel();
   bgAdmin = new ButtonGroup();
   jpAdmin.setLayout( gblAdmin );

   String[] dataList = { "Choose a device", "Lamp", "Radio", "Toaster" } ;
   jcbDevice = new JComboBox( dataList );
   jcbDevice.addActionListener(listenerCombo); 
   gbcAdmin.gridx = 1;
   gbcAdmin.gridy = 1;
   gbcAdmin.gridwidth = 6;
   gbcAdmin.gridheight = 3;
   gbcAdmin.fill = GridBagConstraints.BOTH;
   gbcAdmin.weightx = 1;
   gbcAdmin.weighty = 0;
   gbcAdmin.anchor = GridBagConstraints.NORTH;
   gblAdmin.setConstraints( jcbDevice, gbcAdmin );
   jpAdmin.add( jcbDevice );

   jrbOn = new JRadioButton( "ON" );
   jrbOn.setActionCommand("ON");         
   jrbOn.addActionListener(listenerRadio); 
   jrbOn.setEnabled(false);     // start radioButton DISABLED
   bgAdmin.add( jrbOn );
   gbcAdmin.gridx = 8;
   gbcAdmin.gridy = 2;
   gbcAdmin.gridwidth = 3;
   gbcAdmin.gridheight = 1;
   gbcAdmin.fill = GridBagConstraints.BOTH;
   gbcAdmin.weightx = 1;
   gbcAdmin.weighty = 0;
   gbcAdmin.anchor = GridBagConstraints.NORTH;
   gblAdmin.setConstraints( jrbOn, gbcAdmin );
   jpAdmin.add( jrbOn );

   jrbOff = new JRadioButton( "OFF" );
   jrbOff.setActionCommand("OFF");         
   jrbOff.addActionListener(listenerRadio); 
   jrbOff.setEnabled(false);        // start radioButton DISABLED
   bgAdmin.add( jrbOff );
   gbcAdmin.gridx = 13;
   gbcAdmin.gridy = 2;
   gbcAdmin.gridwidth = 1;
   gbcAdmin.gridheight = 1;
   gbcAdmin.fill = GridBagConstraints.BOTH;
   gbcAdmin.weightx = 1;
   gbcAdmin.weighty = 0;
   gbcAdmin.anchor = GridBagConstraints.NORTH;
   gblAdmin.setConstraints( jrbOff, gbcAdmin );
   jpAdmin.add( jrbOff );

   jtpMain.addTab("Controller", jpAdmin);

   gbcMain.gridx = 0;
   gbcMain.gridy = 0;
   gbcMain.gridwidth = 20;
   gbcMain.gridheight = 5;
   gbcMain.fill = GridBagConstraints.BOTH;
   gbcMain.weightx = 1;
   gbcMain.weighty = 1;
   gbcMain.anchor = GridBagConstraints.NORTH;
   gblMain.setConstraints( jtpMain, gbcMain );
   jpMain.add( jtpMain );

   setDefaultCloseOperation( EXIT_ON_CLOSE );

   setContentPane( jpMain );
   pack();
   setVisible( true );
   } /* itsGUI */



ActionListener listenerCombo = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JComboBox combo = (JComboBox) e.getSource();
    DeviceIndex = combo.getSelectedIndex();
    if (DeviceIndex > 0) {
        // ENABLE the radioButtons
        DeviceName = (String)combo.getSelectedItem();
        jrbOn.setEnabled(true);     
        jrbOff.setEnabled(true);
        }
    else {
        // DISABLE the radioButtons
        DeviceName = null ;
        jrbOn.setEnabled(false);    
        jrbOff.setEnabled(false);
        }
    }
};



ActionListener listenerRadio = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
    AbstractButton aButton = (AbstractButton) actionEvent.getSource(); 
    if (aButton.getText().equals("ON")) {
        // turn on radioButton selection ON indicator
        // jrbOn.setSelected(true); 
        bgAdmin.setSelected(jrbOn.getModel(), true);
        System.out.println("Turning ON the " + DeviceName);
        } /* on */
    else if (aButton.getText().equals("OFF")) {
        // turn on radioButton selection OFF indicator
        // jrbOff.setSelected(true);            
        bgAdmin.setSelected(jrbOff.getModel(), true);
        System.out.println("Turning OFF the " + DeviceName);
        } /* off */
    try { Thread.sleep(1000L); } catch (InterruptedException e) {}  // wait a second
    bgAdmin.clearSelection();       // clear selection indicator of both radioButtons
    jrbOn.setEnabled(false);        // DISABLE the radioButtons
    jrbOff.setEnabled(false);       
    jcbDevice.setSelectedIndex(0);  // Clear the device selection
    }
  };
} /* class */

with the following snippet, the function of on/off is being achieved.
however, the VISUAL does NOT display the radioButton as "selected".
i am looking for help/direction in displaying the selected visual
BEFORE the pause and clearing the combo (see listener at bottom)

thanks in advance
ace

//
// The idea is to have a combo box for device selection
// and radio buttons to select on/off for the device
// the buttons are NOT available until a combo selection is made
// The button should show selected before the combo box is reset
//

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JTabbedPane;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;

public class itsGUI extends JFrame 
{
private static final long serialVersionUID = 1L;
static itsGUI       yeahItsGUI;

// Main panel
JPanel              jpMain;
JTabbedPane         jtpMain;
GridBagLayout       gblMain = new GridBagLayout();
GridBagConstraints  gbcMain = new GridBagConstraints();

// Administrator tab
JPanel              jpAdmin;
GridBagLayout       gblAdmin = new GridBagLayout();
GridBagConstraints  gbcAdmin = new GridBagConstraints();
ButtonGroup         bgAdmin;
JComboBox           jcbDevice;
JRadioButton        jrbOn;
JRadioButton        jrbOff;

// local
int                 DeviceIndex  = 0;
String              DeviceName = null;


public static void main( String args[] ) 
{
   try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      }
   catch ( ClassNotFoundException e ) {}
   catch ( InstantiationException e ) {}
   catch ( IllegalAccessException e ) {} 
   catch ( UnsupportedLookAndFeelException e ) {}

   yeahItsGUI = new itsGUI();
   } /* main */


public itsGUI() 
{
   super( "Button Group" );

   jpMain = new JPanel();
   jpMain.setLayout( gblMain );
   jpMain.setPreferredSize(new Dimension(200, 100));

   jtpMain = new JTabbedPane( );

   jpAdmin = new JPanel();
   bgAdmin = new ButtonGroup();
   jpAdmin.setLayout( gblAdmin );

   String[] dataList = { "Choose a device", "Lamp", "Radio", "Toaster" } ;
   jcbDevice = new JComboBox( dataList );
   jcbDevice.addActionListener(listenerCombo); 
   gbcAdmin.gridx = 1;
   gbcAdmin.gridy = 1;
   gbcAdmin.gridwidth = 6;
   gbcAdmin.gridheight = 3;
   gbcAdmin.fill = GridBagConstraints.BOTH;
   gbcAdmin.weightx = 1;
   gbcAdmin.weighty = 0;
   gbcAdmin.anchor = GridBagConstraints.NORTH;
   gblAdmin.setConstraints( jcbDevice, gbcAdmin );
   jpAdmin.add( jcbDevice );

   jrbOn = new JRadioButton( "ON" );
   jrbOn.setActionCommand("ON");         
   jrbOn.addActionListener(listenerRadio); 
   jrbOn.setEnabled(false);     // start radioButton DISABLED
   bgAdmin.add( jrbOn );
   gbcAdmin.gridx = 8;
   gbcAdmin.gridy = 2;
   gbcAdmin.gridwidth = 3;
   gbcAdmin.gridheight = 1;
   gbcAdmin.fill = GridBagConstraints.BOTH;
   gbcAdmin.weightx = 1;
   gbcAdmin.weighty = 0;
   gbcAdmin.anchor = GridBagConstraints.NORTH;
   gblAdmin.setConstraints( jrbOn, gbcAdmin );
   jpAdmin.add( jrbOn );

   jrbOff = new JRadioButton( "OFF" );
   jrbOff.setActionCommand("OFF");         
   jrbOff.addActionListener(listenerRadio); 
   jrbOff.setEnabled(false);        // start radioButton DISABLED
   bgAdmin.add( jrbOff );
   gbcAdmin.gridx = 13;
   gbcAdmin.gridy = 2;
   gbcAdmin.gridwidth = 1;
   gbcAdmin.gridheight = 1;
   gbcAdmin.fill = GridBagConstraints.BOTH;
   gbcAdmin.weightx = 1;
   gbcAdmin.weighty = 0;
   gbcAdmin.anchor = GridBagConstraints.NORTH;
   gblAdmin.setConstraints( jrbOff, gbcAdmin );
   jpAdmin.add( jrbOff );

   jtpMain.addTab("Controller", jpAdmin);

   gbcMain.gridx = 0;
   gbcMain.gridy = 0;
   gbcMain.gridwidth = 20;
   gbcMain.gridheight = 5;
   gbcMain.fill = GridBagConstraints.BOTH;
   gbcMain.weightx = 1;
   gbcMain.weighty = 1;
   gbcMain.anchor = GridBagConstraints.NORTH;
   gblMain.setConstraints( jtpMain, gbcMain );
   jpMain.add( jtpMain );

   setDefaultCloseOperation( EXIT_ON_CLOSE );

   setContentPane( jpMain );
   pack();
   setVisible( true );
   } /* itsGUI */



ActionListener listenerCombo = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JComboBox combo = (JComboBox) e.getSource();
    DeviceIndex = combo.getSelectedIndex();
    if (DeviceIndex > 0) {
        // ENABLE the radioButtons
        DeviceName = (String)combo.getSelectedItem();
        jrbOn.setEnabled(true);     
        jrbOff.setEnabled(true);
        }
    else {
        // DISABLE the radioButtons
        DeviceName = null ;
        jrbOn.setEnabled(false);    
        jrbOff.setEnabled(false);
        }
    }
};



ActionListener listenerRadio = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent) {
    AbstractButton aButton = (AbstractButton) actionEvent.getSource(); 
    if (aButton.getText().equals("ON")) {
        // turn on radioButton selection ON indicator
        // jrbOn.setSelected(true); 
        bgAdmin.setSelected(jrbOn.getModel(), true);
        System.out.println("Turning ON the " + DeviceName);
        } /* on */
    else if (aButton.getText().equals("OFF")) {
        // turn on radioButton selection OFF indicator
        // jrbOff.setSelected(true);            
        bgAdmin.setSelected(jrbOff.getModel(), true);
        System.out.println("Turning OFF the " + DeviceName);
        } /* off */
    try { Thread.sleep(1000L); } catch (InterruptedException e) {}  // wait a second
    bgAdmin.clearSelection();       // clear selection indicator of both radioButtons
    jrbOn.setEnabled(false);        // DISABLE the radioButtons
    jrbOff.setEnabled(false);       
    jcbDevice.setSelectedIndex(0);  // Clear the device selection
    }
  };
} /* class */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2025-01-05 03:49:46

您可以使用以下行自行清除选择:

bgAdmin.clearSelection();

不确定您想要实现什么,但不要使用 Thread.sleep() 阻止事件调度线程 (EDT)。请在此处了解更多信息。

You clear the selection yourself with this line:

bgAdmin.clearSelection();

Not sure what you trying to achieve but don't block Event Dispatch Thread (EDT) with Thread.sleep(). Read more here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文