Creo questa radiobutton in questo modo :
codice:
public class prova extends JPanel
implements ActionListener {
int sc;
static String br1 = "1. Avviare una nuova partita";
static String br2 = "2. Caricare una partita salvata";
static String br3 = "3. Storico partita";
static String br4 = "4. Esci";
JLabel picture;
public prova() {
super(new BorderLayout());
JRadioButton b1Button = new JRadioButton(br1);
b1Button.setMnemonic(KeyEvent.VK_B);
b1Button.setActionCommand(br1);
b1Button.setSelected(true);
JRadioButton b2Button = new JRadioButton(br2);
b2Button.setMnemonic(KeyEvent.VK_C);
b2Button.setActionCommand(br2);
JRadioButton b3Button = new JRadioButton(br3);
b3Button.setMnemonic(KeyEvent.VK_D);
b3Button.setActionCommand(br3);
JRadioButton b4Button = new JRadioButton(br4);
b4Button.setMnemonic(KeyEvent.VK_R);
b4Button.setActionCommand(br4);
ButtonGroup group = new ButtonGroup();
group.add(b1Button);
group.add(b2Button);
group.add(b3Button);
group.add(b4Button);
b1Button.addActionListener(this);
b2Button.addActionListener(this);
b3Button.addActionListener(this);
b4Button.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(b1Button);
radioPanel.add(b2Button);
radioPanel.add(b3Button);
radioPanel.add(b4Button);
add(radioPanel, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public int getSelezione(ActionEvent e){
if (e.getActionCommand()=="1. Avviare una nuova partita")
sc = 1;
if (e.getActionCommand()=="2. Caricare una partita salvata")
sc = 2;
if (e.getActionCommand()== "3. Storico partita")
sc = 3;
if (e.getActionCommand()== "4. Esci")
sc = 4;
return sc;
}
public int setSelezione(){
return sc;
}
public void actionPerformed(ActionEvent e) {
sc = e.getModifiers();
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Menù di scelta");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new prova();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
chiamo in un main di un'altra classe questa classe rappresentante il JradioButton però quando la chiama è come se andasse in loop e mostra sempre la finestra senza permettermi di selezionare nessuna scelta. Da cosa può dipendere??