-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontFrame.java
More file actions
76 lines (59 loc) · 2.51 KB
/
FontFrame.java
File metadata and controls
76 lines (59 loc) · 2.51 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gridbag;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
/**
* A frame that uses a grid bag layout to arrange font selection components.
*/
public class FontFrame extends JFrame {
public static final int TEXT_ROWS = 10;
public static final int TEXT_COLUMNS = 20;
private JComboBox<String> face;
private JComboBox<Integer> size;
private JCheckBox bold;
private JCheckBox italic;
private JTextArea sample;
public FontFrame() {
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
ActionListener listener = e -> updateSample();
// construct components
JLabel faceLabel = new JLabel("Face: ");
face = new JComboBox<>(new String[]{"Serif", "SansSerif", "Monospaced",
"Dialog", "DialogInput"});
face.addActionListener(listener);
JLabel sizeLabel = new JLabel("Size: ");
size = new JComboBox<>(new Integer[]{8, 10, 12, 15, 18, 24, 36, 48});
size.addActionListener(listener);
bold = new JCheckBox("Bold");
bold.addActionListener(listener);
italic = new JCheckBox("Italic");
italic.addActionListener(listener);
sample = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
sample.setText("The quick brown fox jumps over the lazy dog");
sample.setEditable(false);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder());
// add components to grid, using GBC convenience class
add(faceLabel, new GBC(0, 0).setAnchor(GBC.EAST));
add(face, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0)
.setInsets(1));
add(sizeLabel, new GBC(0, 1).setAnchor(GBC.EAST));
add(size, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0)
.setInsets(1));
add(bold, new GBC(0, 2, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(italic, new GBC(0, 3, 2, 1).setAnchor(GBC.CENTER).setWeight(100, 100));
add(sample, new GBC(2, 0, 1, 4).setFill(GBC.BOTH).setWeight(100, 100));
pack();
updateSample();
}
private void updateSample() {
String fontFace = (String) face.getSelectedItem();
int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
+ (italic.isSelected() ? Font.ITALIC : 0);
int fontSize = size.getItemAt(size.getSelectedIndex());
Font font = new Font(fontFace, fontStyle, fontSize);
sample.setFont(font);
sample.repaint();
}
}