forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplashScreenEx.java
More file actions
117 lines (87 loc) · 2.88 KB
/
SplashScreenEx.java
File metadata and controls
117 lines (87 loc) · 2.88 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
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
package com.zetcode;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingWorker;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import static javax.swing.LayoutStyle.ComponentPlacement.RELATED;
/*
* This program creates a splash screen before
* displaying the main application window.
*
*/
public class SplashScreenEx extends JWindow {
private final int DELAY = 3000;
public SplashScreenEx() {
initUI();
}
private void initUI() {
var cpane = (JPanel) getContentPane();
cpane.setBorder(BorderFactory.createLineBorder(
Color.darkGray, 3));
cpane.setBackground(Color.white);
var nameLabel = new JLabel("F manager", JLabel.CENTER);
var copyLabel = new JLabel("Copyright 2019, Jan Bodnar",
JLabel.CENTER);
copyLabel.setFont(new Font("Serif", Font.BOLD, 13));
createLayout(nameLabel, copyLabel);
setSize(450, 120);
setLocationRelativeTo(null);
doDelay();
}
private void doDelay() {
var worker = new MyWorker();
worker.execute();
}
private class MyWorker extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws InterruptedException {
Thread.sleep(DELAY);
return null;
}
@Override
protected void done() {
setVisible(false);
createMainWindow();
}
};
private void createLayout(Component... arg) {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
.addComponent(arg[1], GroupLayout.DEFAULT_SIZE,
GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addPreferredGap(RELATED, GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(arg[1])
);
pack();
}
private void createMainWindow() {
var frame = new JFrame("Application");
frame.setSize(350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new SplashScreenEx();
ex.setVisible(true);
});
}
}