2016-11-02 10 views
0

Java 초심자, 여기서 혼란 스럽다. 그래서 switch 문을 만드는 법을 알고 JRadio 버튼을 사용하는 방법을 알고있다. 라디오 버튼을 사용하여 switch 문을 수행하는 데 문제가 있습니다. 나는 JRadio 버튼을 payFrequency라는 JRadio 버튼 그룹에서 사용하고 있습니다.JRadio Java에서 switch 문을 만드는 법

업데이트 : 사용하려고하는 코드의 예입니다. 아래의 내용이 잘못되었다는 것을 알고 있으므로, 내가하려고했던 것의 예와 제공 할 것을 권할 것입니다. (payFrequency는 다른 라디오 버튼이있는 버튼 그룹입니다. 해당 정보와 관련이 있는지 알 수 없습니다.)

  switch(PayFrequency) 
       case jRadioButton1.isSelected(): 
        sal1= (sal1a + sal1b) * 2.15; 
        break; 
       case jRadioButton2.isSelected(): 
        sal1= (sal1a + sal1b) * 4.3; 
        break; 
       case jRadioButton3.isSelected(): 
        sal1= (sal1a + sal1b) * 4.3; 
        break;      
       default 
        sal1= sal1a + sal1b; 
+0

는 지금까지 시도 무슨 질문을 이해 할 수 없습니다. 우리에게 몇 가지 코드를 보여 주면 우리가 당신을 도울 수있을 것입니다. –

답변

0

JDK7 +는 all primitives and String objects 스위치를 지원합니다. 따라서 switch 문에서 라디오 버튼을 사용할 수 없습니다. 그러나 스위치에 myRadioButton.getText()을 사용하면 라디오 버튼의 텍스트 레이블을 반환합니다. 그런 다음 스위치 내부의 각 사례에 대해 적절한 조치를 취할 수 있습니다.

0

다소 까다 롭지 만 setActionCommand (String s)를 사용하여 JRadioButton에 "id"를 설정 한 다음 스위치 대/소문자로 사용할 수 있습니다.

확인 내가 한 임의의 예 (Original Example)에서 수정이 코드 :

import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JRadioButton; 
import javax.swing.SwingUtilities; 


public class SwingJRadioButtonDemo extends JFrame { 


private static final long serialVersionUID = -  8307105427074441939L; 

    private JButton buttonOK = new JButton("OK"); 

    private JRadioButton optionLinux = new JRadioButton("Linux"); 
    private JRadioButton optionWin = new JRadioButton("Windows"); 
    private JRadioButton optionMac = new JRadioButton("Macintosh"); 



    public SwingJRadioButtonDemo() { 
     super("Swing JRadioButton Demo"); 
     //Set ID and add to group 
     ButtonGroup group = new ButtonGroup(); 
     optionLinux.setActionCommand ("1"); 
     group.add(optionLinux); 
     optionWin.setActionCommand ("2"); 
     group.add(optionWin); 
     optionMac.setActionCommand ("3"); 
     group.add(optionMac); 



     optionWin.setSelected(true); 


     setLayout(new GridBagLayout()); 
     GridBagConstraints constraints = new GridBagConstraints(); 
     constraints.gridx = 0; 
     constraints.gridy = 0; 
     constraints.anchor = GridBagConstraints.CENTER; 
     constraints.insets = new Insets(10, 10, 10, 10); 

     add(optionLinux, constraints); 
     constraints.gridx = 1; 
     add(optionWin, constraints); 
     constraints.gridx = 2; 
     add(optionMac, constraints); 

     constraints.gridx = 0; 
     constraints.gridy = 1; 
     constraints.gridwidth = 3; 


     constraints.gridy = 2; 
     add(buttonOK, constraints); 

    RadioButtonActionListener actionListener = new  RadioButtonActionListener(); 
     optionLinux.addActionListener(actionListener); 
     optionWin.addActionListener(actionListener); 
     optionMac.addActionListener(actionListener); 

     buttonOK.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent event) { 
       //Get "ID" 
      String selectedOption = group.getSelection ( ).getActionCommand (); 
       //Switch on "IDS" 
       switch(selectedOption) { 
        case "1": 
        JOptionPane.showMessageDialog( SwingJRadioButtonDemo.this, 
          "You selected: Linux with id: " +  selectedOption); 
         break; 
        case "2": 
        JOptionPane.showMessageDialog( SwingJRadioButtonDemo.this, 
          "You selected: Windows with id: " +  selectedOption); 
         break; 
        case "3": 
        JOptionPane.showMessageDialog( SwingJRadioButtonDemo.this, 
          "You selected Mac with id: " +  selectedOption); 
         break; 
       } 
      } 

     }); 

     pack(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
    } 

    class RadioButtonActionListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      JRadioButton button = (JRadioButton) event.getSource(); 
      if (button == optionLinux) { 

       System.out.println ("Linux"); 

      } else if (button == optionWin) { 

       System.out.println ("Windows"); 

      } else if (button == optionMac) { 

       System.out.println ("Mac"); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new SwingJRadioButtonDemo().setVisible(true); 
      } 
     }); 
    } 
}