将代码改为(其中showYourFatherFrame()是你的父窗体的显示方法): createSplashScreen(); // do the following on the gui thread SwingUtilities.invokeLater(new Runnable() { public void run() { showSplashScreen(); } }); initialize(); // Show the frame and take down the splash screen. Note that // we again must do this on the GUI thread using invokeLater. SwingUtilities.invokeLater(new Runnable() { public void run() { showYourFatherFrame(); hideSplash(); } }); 附showSplashScreen() & showFrame() & hideSplash()(fatherFrame代表你的父容器或父窗体) private JWindow splashScreen = null; private JLabel splashLabel = null; /** * create the splash screen */ public void createSplashScreen() { splashLabel = new JLabel(new ImageIcon("imagepath")); splashScreen = new JWindow(fatherFrame); splashScreen.getContentPane().add(splashLabel); splashScreen.pack(); Rectangle screenRect = father.getGraphicsConfiguration().getBounds(); splashScreen.setLocation( screenRect.x + screenRect.width/2 - splashScreen.getSize().width/2, screenRect.y + screenRect.height/2 - splashScreen.getSize().height/2); } /** * show the splash screen */ public void showSplashScreen() { splashScreen.show(); } /** * pop down the spash screen */ public void hideSplash() { splashScreen.setVisible(false); splashScreen = null; splashLabel = null; }
java.awt.Window.pack() public void pack() Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated. 就是这个意思啊!翻译过来就是使窗口的大小适合于优选尺寸,并为它的子组件布局。如果这个窗口和/或它的拥有者是不可显示的,那么上面两者在计算优选尺寸时被设置成为不可见。窗口将在优选尺寸的计算完成后有效。如果不使用pack(),并且在使用pack()处加入代码: splashScreen.setSize(new Dimension(100, 100)); 那么再看看结果,发现它并不是优选尺寸,也就是说,这样可能放不下整张图片;但如果在该语句后跟pack(),则这个Window就不会使用它的尺寸,而是优选尺寸。
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showSplashScreen();
}
}); initialize(); // Show the frame and take down the splash screen. Note that
// we again must do this on the GUI thread using invokeLater.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showYourFatherFrame();
hideSplash();
}
});
附showSplashScreen() & showFrame() & hideSplash()(fatherFrame代表你的父容器或父窗体) private JWindow splashScreen = null;
private JLabel splashLabel = null; /**
* create the splash screen
*/
public void createSplashScreen() {
splashLabel = new JLabel(new ImageIcon("imagepath")); splashScreen = new JWindow(fatherFrame);
splashScreen.getContentPane().add(splashLabel);
splashScreen.pack();
Rectangle screenRect = father.getGraphicsConfiguration().getBounds();
splashScreen.setLocation(
screenRect.x + screenRect.width/2 - splashScreen.getSize().width/2,
screenRect.y + screenRect.height/2 - splashScreen.getSize().height/2);
} /**
* show the splash screen
*/
public void showSplashScreen() {
splashScreen.show();
} /**
* pop down the spash screen
*/
public void hideSplash() {
splashScreen.setVisible(false);
splashScreen = null;
splashLabel = null;
}
看API也不是很明白!
每次使用了 感觉和没使用 没区别啊
public void pack()
Causes this Window to be sized to fit the preferred size and layouts of its
subcomponents. If the window and/or its owner are not yet displayable, both
are made displayable before calculating the preferred size. The Window will be
validated after the preferredSize is calculated. 就是这个意思啊!翻译过来就是使窗口的大小适合于优选尺寸,并为它的子组件布局。如果这个窗口和/或它的拥有者是不可显示的,那么上面两者在计算优选尺寸时被设置成为不可见。窗口将在优选尺寸的计算完成后有效。如果不使用pack(),并且在使用pack()处加入代码:
splashScreen.setSize(new Dimension(100, 100));
那么再看看结果,发现它并不是优选尺寸,也就是说,这样可能放不下整张图片;但如果在该语句后跟pack(),则这个Window就不会使用它的尺寸,而是优选尺寸。
二是线程的应用
ok~~~~~
但是不是会产生这样的后果,如果使用pack()函数的话,
window会自动调整各个组件的大小,即使我们写了setSize(new Dimension(...))函数,
在已有pack()函数的情况下,window也只会自动设置优先尺寸,那样的话,是不是setSize()
函数将没有意义了??