import java.awt.FlowLayout;
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.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class CalculateCirlceGUI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1200596029840561525L;
JTextField field1 = new JTextField(3);
JLabel label = new JLabel(“Enter the Circle Radius:”);
JLabel label1 = new JLabel();
JButton button = new JButton(“Calculate”);
JRadioButton option1 = new JRadioButton(“Area”);
JRadioButton option2 = new JRadioButton(“Perimeter”);
public CalculateCirlceGUI() {
super(“Swing JRadioButton Demo”);
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (option1.isSelected()) {
int r = Integer.parseInt(field1.getText());
label1.setText(“” + (Math.PI * r * r));
} else if (option2.isSelected()) {
int r = Integer.parseInt(field1.getText());
label1.setText(“” + (2 * Math.PI * r));
} else {
label1.setText(“Please select area or perimeter”);
}
}
});
setLayout(new FlowLayout());
add(label);
add(field1);
add(option1);
add(option2);
add(button);
add(label1);
setSize(210, 200);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CalculateCirlceGUI().setVisible(true);
}
});
}
}
OUTPUT:
