ちょっと難しいですが、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);
}
});
}
}
は、あなたがこれまでに試してみました何あなたの質問を理解することはできません。私たちにいくつかのコードを表示すると、あなたを助けることができます。 –