/
MainPanel.java
178 lines (152 loc) · 5.29 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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Optional;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.*;
public final class MainPanel extends JPanel {
private final transient WindowPreferencesHandler handler = new WindowPreferencesHandler();
private MainPanel() {
super(new BorderLayout());
JButton clearButton = new JButton("Preferences#clear() and JFrame#dispose()");
clearButton.addActionListener(e -> {
try {
handler.prefs.clear();
handler.prefs.flush();
} catch (BackingStoreException ex) {
ex.printStackTrace();
Toolkit.getDefaultToolkit().beep();
}
Component c = (Component) e.getSource();
Optional.ofNullable(SwingUtilities.getWindowAncestor(c)).ifPresent(Window::dispose);
});
JButton exitButton = new JButton("exit");
exitButton.addActionListener(e -> {
handler.saveLocation();
Component c = (Component) e.getSource();
Optional.ofNullable(SwingUtilities.getWindowAncestor(c)).ifPresent(Window::dispose);
});
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(clearButton);
box.add(Box.createHorizontalStrut(2));
box.add(exitButton);
add(new JLabel("TEST"));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(box, BorderLayout.SOUTH);
EventQueue.invokeLater(() -> {
// Component c = SwingUtilities.getRoot(getRootPane());
Container c = getTopLevelAncestor();
if (c instanceof Window) {
Window frame = (Window) c;
frame.addWindowListener(handler);
frame.addComponentListener(handler);
handler.initFrameSizeAndLocation(frame);
}
});
}
@Override public Dimension getPreferredSize() {
return handler.dim;
}
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 WindowPreferencesHandler extends WindowAdapter implements ComponentListener {
private static final String PREFIX = "xxx_";
public final Preferences prefs = Preferences.userNodeForPackage(getClass());
public final Dimension dim = new Dimension(320, 240);
public final Point pos = new Point();
public void initFrameSizeAndLocation(Window frame) {
int wdim = prefs.getInt(PREFIX + "dimw", dim.width);
int hdim = prefs.getInt(PREFIX + "dimh", dim.height);
dim.setSize(wdim, hdim);
// setPreferredSize(dim);
frame.pack();
Rectangle screen = frame.getGraphicsConfiguration().getBounds();
pos.setLocation(screen.getCenterX() - dim.width / 2d, screen.getCenterY() - dim.height / 2d);
int xpos = prefs.getInt(PREFIX + "locx", pos.x);
int ypos = prefs.getInt(PREFIX + "locy", pos.y);
pos.setLocation(xpos, ypos);
frame.setLocation(pos.x, pos.y);
}
public void saveLocation() {
prefs.putInt(PREFIX + "locx", pos.x);
prefs.putInt(PREFIX + "locy", pos.y);
prefs.putInt(PREFIX + "dimw", dim.width);
prefs.putInt(PREFIX + "dimh", dim.height);
try {
prefs.flush();
} catch (BackingStoreException ex) {
ex.printStackTrace();
Toolkit.getDefaultToolkit().beep();
}
}
@Override public void componentHidden(ComponentEvent e) {
/* not needed */
}
@Override public void componentMoved(ComponentEvent e) {
Frame frame = (Frame) e.getComponent();
if (frame.getExtendedState() == Frame.NORMAL) {
Point pt = frame.getLocationOnScreen();
if (pt.x < 0 || pt.y < 0) {
return;
}
pos.setLocation(pt);
}
}
@Override public void componentResized(ComponentEvent e) {
JFrame frame = (JFrame) e.getComponent();
if (frame.getExtendedState() == Frame.NORMAL) {
dim.setSize(frame.getContentPane().getSize());
}
}
@Override public void componentShown(ComponentEvent e) {
/* not needed */
}
// @Override public void windowActivated(WindowEvent e) {
// /* not needed */
// }
// @Override public void windowClosed(WindowEvent e) {
// /* not needed */
// }
@Override public void windowClosing(WindowEvent e) {
saveLocation();
e.getWindow().dispose();
}
// @Override public void windowDeactivated(WindowEvent e) {
// /* not needed */
// }
// @Override public void windowDeiconified(WindowEvent e) {
// /* not needed */
// }
// @Override public void windowIconified(WindowEvent e) {
// /* not needed */
// }
// @Override public void windowOpened(WindowEvent e) {
// /* not needed */
// }
}