package radioButton;
import javax.swing.*;
import java.awt.*;
/**
* Created by End on 16/1/14.
*/
public class RadioButtonTest {
public static void main (String args[]){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new RadioButtonFrame();
frame.setTitle("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
package radioButton;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Created by End on 16/1/14.
*/
public class RadioButtonFrame extends JFrame {
private JPanel buttonPanel ;
private ButtonGroup group;
private JLabel label;
private static final int DEFAULT_SIZE = 36 ;
public RadioButtonFrame(){
label = new JLabel("I'm too said!");
label.setFont(new Font("Serif",Font.PLAIN,DEFAULT_SIZE));
add(label,BorderLayout.CENTER);
buttonPanel = new JPanel();
group = new ButtonGroup();
addRadioButton("Small",8);
addRadioButton("Medium",12);
addRadioButton("Large",18);
addRadioButton("Extra large",36);
add(buttonPanel,BorderLayout.SOUTH);
pack();
}
public void addRadioButton(String name, final int size){
boolean selected = size == DEFAULT_SIZE ;
JRadioButton button = new JRadioButton(name , selected);
group.add(button);
buttonPanel.add(button);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setFont(new Font("Serif",Font.PLAIN,size));
}
};
button.addActionListener(listener);
}
}