Per quanto riguarda la classe Prenotation ho fatto così:
package view;
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 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 Prenotation extends JFrame {
private JButton buttonOK = new JButton("OK");
public JRadioButton option1 = new JRadioButton("Sms");
public JRadioButton option2 = new JRadioButton("Email");
private JLabel labelImage = new JLabel();
public Prenotation() {
super("Swing JRadioButton Demo");
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
option2.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(option1, constraints);
constraints.gridx = 1;
add(option2, constraints);
constraints.gridx = 2;
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 3;
add(labelImage, constraints);
constraints.gridy = 2;
add(buttonOK, constraints);
RadioButtonActionListener actionListener = new RadioButtonActionListener();
option1.addActionListener(actionListener);
option1.addActionListener(actionListener);
buttonOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String selectedOption = "";
if (option1.isSelected()) {
selectedOption = "Sms";
}
else if (option2.isSelected()) {
selectedOption = "Email";
}
JOptionPane.showMessageDialog(Prenotation.this,
"Hai scelto: " + selectedOption);
}
});
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
class RadioButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JRadioButton button = (JRadioButton) event.getSource();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Prenotation().setVisible(true);
}
});
}
}
Magari ho lasciato qualcosa di troppo, invece per la parte relativa al controllore ho qualche difficoltà
Come faccio a tenere nota del bottone che ho cliccato
Al momento la parte relativa all'ActionPerformed l'ho impostata così ma c'è da cambiarne il funzionamento
package controller;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import view.Prenotation;
public class PrenotationController implements ActionListener {
Prenotation view;
public PrenotationController(Prenotation view){
this.view = view;
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Hai Cliccato");
int sms;
int email;
sms = Integer.parseInt(view.option1.getText());
email = Integer.parseInt(view.option1.getText());
System.out.println("Hai scelto:");
}
}