我有一个关于Toggled按钮的基本问题:以下代码确实生成了所需的结果,但是,似乎布尔值应该相反。代码是否以编程方式正确?
public class ToggleButton extends javax.swing.JFrame {
public ToggleButton() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
one = new javax.swing.JRadioButton();
all = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
one.setText("one channel");
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oneActionPerformed(evt);
}
});
all.setText("all channel");
all.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
allActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(all)
.addComponent(one))
.addContainerGap(122, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(one)
.addGap(18, 18, 18)
.addComponent(all)
.addContainerGap(29, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void allActionPerformed(java.awt.event.ActionEvent evt) {
if("all".equals(evt.getActionCommand())){
all.setSelected(false);
one.setSelected(true);
}else {
one.setSelected(false);
all.setSelected(true);
}
}
private void oneActionPerformed(java.awt.event.ActionEvent evt) {
if("one".equals(evt.getActionCommand())){
one.setSelected(false);
all.setSelected(true);
}else{
all.setSelected(false);
one.setSelected(true);
}
}
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ToggleButton.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ToggleButton().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JRadioButton all;
private javax.swing.JRadioButton one;
// End of variables declaration
}
答案 0 :(得分:3)
您的测试if("all".equals(evt.getActionCommand()))
和if("one".equals(evt.getActionCommand()))
始终为false
,因为您从未在actionCommand
上设置任何JRadioBuitton
。现在,我真的不明白为什么你在UI已经这样做时试图设置单选按钮的值。您不必重新修改按钮的状态。
您可能缺少的是ButtonGroup
(并将JRadioButton
添加到该组),以便在选择一个JRadioButton时,另一个会自动取消选择。