-1

我有一个关于切换按钮的基本问题:以下代码确实会产生所需的结果,但是,布尔值似乎应该相反。代码在编程上是否正确?

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
}
4

1 回答 1

3

if("all".equals(evt.getActionCommand()))你的测试if("one".equals(evt.getActionCommand()))总是false因为你从来没有actionCommand在你JRadioBuitton的 's 上设置任何东西。现在,我真的不明白为什么你在 UI 已经这样做的时候尝试设置单选按钮的值。您不必重新修改按钮的状态。

What you may lack is a ButtonGroup(and add both JRadioButton's to that group) so that when one JRadioButton is selected the other gets automatically deselected.

于 2013-01-07T16:53:12.307 回答