2013-01-07 2 views
-1

토글 버튼에 대한 기본적인 질문이 있습니다. 다음 코드는 원하는 결과를 생성하지만 불리언 값이 반대로 보입니다. 코드가 프로그래밍 방식으로 올바 릅니까? 당신이 당신의 JRadioBuitton 년대에 어떤 actionCommand을 설정하지도토글 버튼 전환

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

GUI 빌더를 사용하는 경우 올바른지 확인하십시오. –

+0

대부분의 코드가 IDE에서 생성되었으므로 하나의 채널을 클릭하면 allActionPerformed 및 oneActionPerformed 이벤트 처리기가 혼란 스럽습니다. oneActionPerformed에서 one.setSelected를 true로 설정하면 안됩니까? 그 반대의 경우도 마찬가지입니까? – user1955475

+0

당신이 원하는 바에 달려 있습니다. 결과물은 무엇입니까? 틀릴 것입니다. 더 빨리 도움을 받으려면 SSCCE –

답변

3

귀하의 테스트 if("all".equals(evt.getActionCommand()))if("one".equals(evt.getActionCommand())) 항상 false 있습니다. 자, UI가 이미 그렇게하는 동안 라디오 버튼의 값을 설정하려고하는 이유를 모르겠습니다. 버튼의 상태를 다시 설명하지 않아도됩니다.

ButtonGroup (해당 그룹에 JRadioButton)을 추가하면 하나의 JRadioButton을 선택하면 다른 하나가 자동으로 선택 취소됩니다.

+0

+1을 게시하십시오. 그 버튼이 같은 그룹에 속하는지 아닌지 알지 못합니다. 둘 다 할 수있는 이유가 있습니다. 선택해야합니다. –