2016-11-02 8 views
0

初心者はここで混乱していますので、私は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」を設定し、スイッチケースで使用することができます。

チェック私は1ランダム例(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); 
      } 
     }); 
    } 
} 
関連する問題