/
MainPanel.java
295 lines (249 loc) · 8.14 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;
import javax.swing.*;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new GridLayout(2, 1));
JTabbedPane tab1 = new JTabbedPane();
tab1.setBorder(BorderFactory.createTitledBorder("JTabbedPane"));
tab1.addTab("1111", new JScrollPane(new JTree()));
tab1.addTab("2222", new JLabel("JLabel 1"));
tab1.addTab("3333", new JLabel("JLabel 2"));
tab1.addTab("4444", new JButton("JButton 1"));
CardLayoutTabbedPane tab2 = new CardLayoutTabbedPane();
tab2.setBorder(BorderFactory.createTitledBorder("CardLayout+JRadioButton(windows like)"));
tab2.addTab("default", tab1);
tab2.addTab("5555", new JScrollPane(new JTree()));
tab2.addTab("6666", new JLabel("JLabel 3"));
tab2.addTab("7777", new JLabel("JLabel 4"));
tab2.addTab("8888", new JButton("JButton 2"));
UIManager.put("example.TabButton", "TabViewButtonUI");
UIManager.put("TabViewButtonUI", "example.OperaTabViewButtonUI");
CardLayoutTabbedPane tab3 = new CardLayoutTabbedPane();
tab3.setBorder(BorderFactory.createTitledBorder("CardLayout+JRadioButton(opera like)"));
tab3.addTab("9999", new JScrollPane(new JTree()));
tab3.addTab("000000000", new JLabel("JLabel 5"));
tab3.addTab("1111", new JLabel("JLabel 6"));
tab3.addTab("222", new JButton("JButton 3"));
// add(tab1);
add(tab2);
add(tab3);
setPreferredSize(new Dimension(320, 240));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} 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 CardLayoutTabbedPane extends JPanel {
private final CardLayout cardLayout = new CardLayout();
private final JPanel tabPanel = new JPanel(new GridLayout(1, 0, 0, 0));
private final JPanel contentsPanel = new JPanel(cardLayout);
private final ButtonGroup bg = new ButtonGroup();
protected CardLayoutTabbedPane() {
super(new BorderLayout());
int left = 1;
int right = 3;
tabPanel.setBorder(BorderFactory.createEmptyBorder(1, left, 0, right));
contentsPanel.setBorder(BorderFactory.createEmptyBorder(4, left, 2, right));
JPanel tabArea = new JPanel(new BorderLayout());
tabArea.add(tabPanel);
tabArea.add(new JLabel("test:"), BorderLayout.WEST);
add(tabArea, BorderLayout.NORTH);
add(contentsPanel);
}
@Override public final Component add(Component comp) {
return super.add(comp);
}
@Override public final void add(Component comp, Object constraints) {
super.add(comp, constraints);
}
protected Component createTabComponent(String title, Component comp) {
TabButton tab = new TabButton(title);
tab.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
((AbstractButton) e.getComponent()).setSelected(true);
cardLayout.show(contentsPanel, title);
}
});
tab.setLayout(new BorderLayout());
// tab.setLayout(new OverlayLayout(tab));
JButton close = new JButton(new CloseTabIcon(Color.GRAY)) {
@Override public Dimension getPreferredSize() {
return new Dimension(12, 12);
}
};
close.addActionListener(e -> {
tabPanel.remove(tab);
contentsPanel.remove(comp);
boolean oneOrMore = tabPanel.getComponentCount() > 1;
if (oneOrMore) {
tabPanel.revalidate();
TabButton b = (TabButton) tabPanel.getComponent(0);
b.setSelected(true);
cardLayout.first(contentsPanel);
}
tabPanel.revalidate();
});
close.setBorder(BorderFactory.createEmptyBorder());
close.setFocusPainted(false);
close.setContentAreaFilled(false);
close.setPressedIcon(new CloseTabIcon(new Color(0xFE_FE_FE)));
close.setRolloverIcon(new CloseTabIcon(new Color(0xA0_A0_A0)));
JPanel p = new JPanel(new BorderLayout());
p.setOpaque(false);
p.add(close, BorderLayout.NORTH);
tab.add(p, BorderLayout.EAST);
bg.add(tab);
tab.setSelected(true);
return tab;
}
public void addTab(String title, Component comp) {
tabPanel.add(createTabComponent(title, comp));
contentsPanel.add(comp, title);
cardLayout.show(contentsPanel, title);
}
}
class TabButton extends JRadioButton {
private static final String UI_CLASS_ID = "TabViewButtonUI";
private Color textColor; // = Color.WHITE;
private Color pressedTc; // = Color.WHITE.darker();
private Color rolloverTc; // = Color.WHITE;
private Color rolloverSelTc; // = Color.WHITE;
private Color selectedTc; // = Color.WHITE;
@Override public void updateUI() {
if (Objects.nonNull(UIManager.get(getUIClassID()))) {
setUI((TabViewButtonUI) UIManager.getUI(this));
} else {
setUI(new BasicTabViewButtonUI());
}
}
@Override public String getUIClassID() {
return UI_CLASS_ID;
}
// @Override public void setUI(TabViewButtonUI ui) {
// super.setUI(ui);
// }
@Override public TabViewButtonUI getUI() {
return (TabViewButtonUI) ui;
}
protected TabButton() {
super(null, null);
}
protected TabButton(Icon icon) {
super(null, icon);
}
protected TabButton(String text) {
super(text, null);
}
protected TabButton(Action a) {
super(a);
// super.setAction(a);
// updateUI();
}
protected TabButton(String text, Icon icon) {
super(text, icon);
// updateUI();
}
@Override protected void fireStateChanged() {
ButtonModel model = getModel();
if (model.isEnabled()) {
if (model.isPressed() && model.isArmed()) {
setForeground(getPressedTextColor());
} else if (model.isSelected()) {
setForeground(getSelectedTextColor());
} else if (isRolloverEnabled() && model.isRollover()) {
setForeground(getRolloverTextColor());
} else {
setForeground(getTextColor());
}
} else {
setForeground(Color.GRAY);
}
super.fireStateChanged();
}
@Override public Dimension getMinimumSize() {
Dimension d = super.getMinimumSize();
d.width = 16;
return d;
}
@Override public String toString() {
return "TabButton";
}
public Color getTextColor() {
return textColor;
}
public Color getPressedTextColor() {
return pressedTc;
}
public Color getRolloverTextColor() {
return rolloverTc;
}
public Color getRolloverSelectedTextColor() {
return rolloverSelTc;
}
public Color getSelectedTextColor() {
return selectedTc;
}
public void setTextColor(Color color) {
textColor = color;
}
public void setPressedTextColor(Color color) {
pressedTc = color;
}
public void setRolloverTextColor(Color color) {
rolloverTc = color;
}
public void setRolloverSelectedTextColor(Color color) {
rolloverSelTc = color;
}
public void setSelectedTextColor(Color color) {
selectedTc = color;
}
}
class CloseTabIcon implements Icon {
private final Color color;
protected CloseTabIcon(Color color) {
this.color = color;
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
g2.setPaint(color);
g2.drawLine(2, 2, 9, 9);
g2.drawLine(2, 3, 8, 9);
g2.drawLine(3, 2, 9, 8);
g2.drawLine(9, 2, 2, 9);
g2.drawLine(9, 3, 3, 9);
g2.drawLine(8, 2, 2, 8);
g2.dispose();
}
@Override public int getIconWidth() {
return 12;
}
@Override public int getIconHeight() {
return 12;
}
}