radioButton不显示所选状态

时间:2012-01-24 19:00:33

标签: java swing selected jradiobutton

使用以下代码段,正在实现开/关功能 但是,VISUAL不会将radioButton显示为"选择"。
我正在寻找显示所选视觉的帮助/方向 在暂停和清除组合之前(见底部的听众)

提前谢谢你 王牌

//
// 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 */

1 个答案:

答案 0 :(得分:4)

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

bgAdmin.clearSelection();

不确定您要实现的目标,但不要使用Thread.sleep()阻止事件调度线程(EDT)。阅读更多here