-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadioButtonFrame.java
55 lines (43 loc) · 1.56 KB
/
RadioButtonFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package radioButton;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
/**
* A frame with a sample text label and radio buttons for selecting font sizes.
*/
public class RadioButtonFrame extends JFrame {
public static final int DEFAULT_SIZE = 36;
private JPanel buttonPanel;
private ButtonGroup group;
private JLabel label;
public RadioButtonFrame() {
// add the sample text label
label = new JLabel("The quick brown fox jumps over the lazy dog");
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
// add the ration buttons
buttonPanel = new JPanel();
group = new ButtonGroup();
addRadioButton("small", 8);
addRadioButton("Medium", 12);
addRadioButton("Large", 18);
addRadioButton("Extra Large", 36);
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
/**
* Adds a ratio button that sets the font size of the sample text
*
* @param name the string to appear on the button
* @param size size the font size that this button sets
*/
private void addRadioButton(String name, int size) {
boolean selected = size == DEFAULT_SIZE;
JRadioButton button = new JRadioButton(name, selected);
group.add(button);
buttonPanel.add(button);
// this listener sets the label font size
ActionListener listener = e -> label.setFont(new Font("Serif", Font.PLAIN, size));
button.addActionListener(listener);
}
}