/
MainPanel.java
156 lines (135 loc) · 4.86 KB
/
MainPanel.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.*;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
String code = "BC89FE5A";
JFormattedTextField field0 = new JFormattedTextField(code);
field0.setHorizontalAlignment(SwingConstants.RIGHT);
field0.setColumns(8);
JFormattedTextField field1 = new JFormattedTextField(code);
field1.setHorizontalAlignment(SwingConstants.RIGHT);
Font mono = new Font(Font.MONOSPACED, Font.PLAIN, field1.getFont().getSize());
field1.setFont(mono);
field1.setColumns(8);
JFormattedTextField field2 = new JFormattedTextField(code);
field2.setHorizontalAlignment(SwingConstants.RIGHT);
Map<TextAttribute, Object> attr = new ConcurrentHashMap<>();
attr.put(TextAttribute.TRACKING, -.011f);
field2.setFont(mono.deriveFont(attr));
field2.setColumns(8);
JFormattedTextField field3 = new JFormattedTextField(code) {
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width += 1;
return d;
}
};
field3.setHorizontalAlignment(SwingConstants.RIGHT);
field3.setFont(mono);
field3.setColumns(8);
JFormattedTextField field4 = new JFormattedTextField(code);
field4.setHorizontalAlignment(SwingConstants.RIGHT);
field4.setFont(mono);
field4.setColumns(9);
AlignedLabel l0 = new AlignedLabel("Default:");
AlignedLabel l1 = new AlignedLabel("MONOSPACED:");
AlignedLabel l2 = new AlignedLabel("TRACKING-.011f:");
AlignedLabel l3 = new AlignedLabel("PreferredSize+1:");
AlignedLabel l4 = new AlignedLabel("Columns+1:");
AlignedLabel.groupLabels(l0, l1, l2, l3, l4);
Box box = Box.createVerticalBox();
box.add(makeTitledPanel(l0, field0));
box.add(makeTitledPanel(l1, field1));
box.add(makeTitledPanel(l2, field2));
box.add(makeTitledPanel(l3, field3));
box.add(makeTitledPanel(l4, field4));
JTextArea log = new JTextArea();
EventQueue.invokeLater(() -> {
append(log, field0, code);
append(log, field1, code);
append(log, field2, code);
});
JPanel p = new JPanel(new BorderLayout());
p.add(box, BorderLayout.WEST);
add(p, BorderLayout.NORTH);
add(new JScrollPane(log));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setPreferredSize(new Dimension(320, 240));
}
private static Container makeTitledPanel(Component title, Component c) {
JPanel box = new JPanel();
box.add(title);
box.add(c);
return box;
}
private static void append(JTextArea log, JComponent c, String str) {
Font font = c.getFont();
FontRenderContext frc = c.getFontMetrics(font).getFontRenderContext();
Rectangle2D r2 = font.getStringBounds(str, frc);
Rectangle r = SwingUtilities.calculateInnerArea(c, null);
log.append(String.format("%s%n %s%n %s%n", font, r, r2));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class AlignedLabel extends JLabel {
private static final int INDENT = 10;
// private AlignedLabel[] group;
private List<AlignedLabel> group;
private int maxWidth;
protected AlignedLabel(String text) {
super(text, RIGHT);
}
@Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
// Align the width with all other labels in group.
d.width = getMaxWidth() + INDENT;
return d;
}
private int getMaxWidth() {
if (maxWidth == 0 && Objects.nonNull(group)) {
int max = group.stream()
.map(AlignedLabel::getSuperPreferredWidth)
.reduce(0, Integer::max);
group.forEach(al -> al.maxWidth = max);
}
return maxWidth;
}
private int getSuperPreferredWidth() {
return super.getPreferredSize().width;
}
public static void groupLabels(AlignedLabel... list) {
List<AlignedLabel> group = Arrays.asList(list);
group.forEach(al -> al.group = group);
}
}