-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnswer.java
More file actions
67 lines (54 loc) · 2.35 KB
/
Answer.java
File metadata and controls
67 lines (54 loc) · 2.35 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
package com.posadskiy.javaswingtutor.presentation.component;
import javax.swing.*;
import java.awt.*;
public class Answer extends CreateFrame {
public Answer() {
createFrame();
}
@Override
public void createFrame() {
String headerText = "Your frame";
JPanel content = new JPanel(new BorderLayout());
content.setBackground(new Color(15, 23, 42));
content.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
JLabel header = new JLabel(headerText, SwingConstants.CENTER);
header.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
header.setForeground(new Color(248, 250, 252));
header.setBorder(BorderFactory.createEmptyBorder(10, 12, 10, 12));
content.add(header, BorderLayout.NORTH);
JPanel footer = new JPanel(new FlowLayout(FlowLayout.RIGHT));
footer.setOpaque(false);
JButton closeBtn = new JButton("Close");
closeBtn.addActionListener(e -> setVisible(false));
footer.add(closeBtn);
content.add(footer, BorderLayout.SOUTH);
setContentPane(content);
// User content goes into CENTER by default (BorderLayout.CENTER)
JTextField field = new JTextField();
add(field);
setTitle(headerText);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
applyHalfScreenBounds(false);
setVisible(true);
}
private void applyHalfScreenBounds(boolean openOnLeft) {
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
try {
PointerInfo pi = MouseInfo.getPointerInfo();
if (pi != null && pi.getDevice() != null) {
device = pi.getDevice();
}
} catch (Exception ignored) {}
GraphicsConfiguration gc = device.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
int usableX = bounds.x + insets.left;
int usableY = bounds.y + insets.top;
int usableW = bounds.width - insets.left - insets.right;
int usableH = bounds.height - insets.top - insets.bottom;
int halfW = usableW / 2;
int x = openOnLeft ? usableX : (usableX + halfW);
int w = openOnLeft ? halfW : (usableW - halfW);
setBounds(x, usableY, w, usableH);
}
}