这可能有几种方法,但让我们保持简单。
基本上,这样做是将label
(背景)图像添加到内容窗格的中心位置。然后,它适用于一个BorderLayout
到label
,并增加了对copyrt
的label
中心位置...
public void showSplash() {
JPanel content = (JPanel) getContentPane();
content.setBackground(Color.blue);
// Set the window's bounds, centering the window
int width = 700;
int height = 450;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width)/2;
int y = (screen.height - height)/2;
setBounds(x, y, width, height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon("java-tip.gif"));
JLabel copyrt = new JLabel("Splash Screen!!!", JLabel.CENTER);
content.add(label, BorderLayout.CENTER);
// Fun starts here...
// copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
// content.add(copyrt, BorderLayout.SOUTH);
label.setLayout(new BorderLayout());
Font font = copyrt.getFont();
copyrt.setFont(font.deriveFont(Font.BOLD, 24f));
label.add(copyrt);
Color oraRed = new Color(200, 50, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
// Don't do this, as it will cause the EDT to be stopped. Instead
// setup some kind of callback that can tell when the resources are
// loaded and start the rest of the application from there...
// Wait a little while, maybe while loading resources
//try {
// Thread.sleep(duration);
//} catch (Exception e) {
//}
//setVisible(false);
}
这也是一个不错的主意做EDT范围内任何可能阻止其进一步处理事件,像使用sleep
或长时间运行/无限循环。
所有UI交互都应该在EDT的上下文中执行,所有长时间运行或潜在阻塞的任务应该在单独的线程上运行。
看看Concurrency in Swing更多细节
更新
如果我尝试运行你的榜样,启动画面永远不会出现,因为Thread.sleep
是防止被显示在屏幕上。事实上,你“可能”得到它的工作实际上是一种侥幸,因为你可能从“主”线程调用showSplash
方法,而不是EDT。
相反,如果我用SwingWorker
替换Thread.sleep
,我不仅可以看到启动画面,还可以控制进度更新和处理启动画面的时间安排等额外好处...
例如...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SplashDemo extends JWindow {
public static void main(String[] args) {
new SplashDemo();
}
public SplashDemo() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
showSplash();
}
});
}
public void showSplash() {
JPanel content = (JPanel) getContentPane();
content.setBackground(Color.blue);
// Set the window's bounds, centering the window
int width = 700;
int height = 450;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - width)/2;
int y = (screen.height - height)/2;
setBounds(x, y, width, height);
// Build the splash screen
JLabel label = new JLabel(new ImageIcon(getClass().getResource("/java_animated.gif")));
JLabel copyrt = new JLabel("Splash Screen!!!", JLabel.CENTER);
content.add(label, BorderLayout.CENTER);
label.setLayout(new GridBagLayout());
Font font = copyrt.getFont();
copyrt.setFont(font.deriveFont(Font.BOLD, 24f));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
label.add(copyrt, gbc);
ImageIcon wait = new ImageIcon(getClass().getResource("/wait.gif"));
label.add(new JLabel(wait), gbc);
Color oraRed = new Color(200, 50, 20, 255);
content.setBorder(BorderFactory.createLineBorder(oraRed, 10));
// Display it
setVisible(true);
toFront();
new ResourceLoader().execute();
}
public class ResourceLoader extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
// Wait a little while, maybe while loading resources
try {
Thread.sleep(5000);
} catch (Exception e) {
}
return null;
}
@Override
protected void done() {
setVisible(false);
}
}
}
如果你愿意,我用下面的图片...
小便!男人你必须有我的帖子出现与速度大声笑 – Opjeezzeey
我不完全确定如何添加一个摇摆计时器。启动画面编译并运行得非常好。我期待的是完全美容。 – Opjeezzeey
@Opjeezzeey它“出现”运行良好,但相信我们,它不.. – MadProgrammer