java中怎么做闪屏窗口。
2个回答
展开全部
我得代码如下
import java.awt.*;
import javax.swing.*;
public class SplashFrame extends JFrame{
public SplashFrame() {
Image imgSplash = Toolkit.getDefaultToolkit().getImage( "splash.png ");
ImagePane pnlImage = new ImagePane(imgSplash);
JWindow splashWindow = new JWindow(this);
Container pane = JWindow.getContentPane();
pane.add(pnlImage, BorderLayout.CENTER);
Dimension scmSize = Toolkit.getDefaultToolkit().getScreenSize();
int nImageWidth = imgSplash.getWidth(this);
int nImageHeight = imgSplash.getHeight(this);
splashWindow.setSize(nImageWidth, nImageHeight);
splashWindow.setLocation(scmSize.width / 2 - nImageWidth / 2,
scmSize.height / 2 - nImageHeight / 2);
splashWindow.show();
splashWindow.toFront();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.dispose();
}
//public static void main(String[] args) {
// SplashFrame frm = new SplashFrame();
//}
}
class ImagePane extends JPanel {
private Image imgSplash;
public ImagePane(Image image) {
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForID(0);
} catch (Exception e) {
e.printStackTrace();
}
this.imgSplash = image;
}
public void paint(Graphics g) {
g.drawImage(imgSplash, 0, 0, this);
}
public Dimension getPreferredSize() {
return new Dimension(imgSplash.getWidth(this), imgSplash
.getHeight(this));
}
}
当把这个当作一个独立的程序来运行,闪屏显示正常
但是但我在另外LoginFrame.java里面new一个SplashFrame来显示就不能正常显示,只是显示一个灰白的Window窗体
import java.awt.*;
import javax.swing.*;
public class SplashFrame extends JFrame{
public SplashFrame() {
Image imgSplash = Toolkit.getDefaultToolkit().getImage( "splash.png ");
ImagePane pnlImage = new ImagePane(imgSplash);
JWindow splashWindow = new JWindow(this);
Container pane = JWindow.getContentPane();
pane.add(pnlImage, BorderLayout.CENTER);
Dimension scmSize = Toolkit.getDefaultToolkit().getScreenSize();
int nImageWidth = imgSplash.getWidth(this);
int nImageHeight = imgSplash.getHeight(this);
splashWindow.setSize(nImageWidth, nImageHeight);
splashWindow.setLocation(scmSize.width / 2 - nImageWidth / 2,
scmSize.height / 2 - nImageHeight / 2);
splashWindow.show();
splashWindow.toFront();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.dispose();
}
//public static void main(String[] args) {
// SplashFrame frm = new SplashFrame();
//}
}
class ImagePane extends JPanel {
private Image imgSplash;
public ImagePane(Image image) {
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
mt.waitForID(0);
} catch (Exception e) {
e.printStackTrace();
}
this.imgSplash = image;
}
public void paint(Graphics g) {
g.drawImage(imgSplash, 0, 0, this);
}
public Dimension getPreferredSize() {
return new Dimension(imgSplash.getWidth(this), imgSplash
.getHeight(this));
}
}
当把这个当作一个独立的程序来运行,闪屏显示正常
但是但我在另外LoginFrame.java里面new一个SplashFrame来显示就不能正常显示,只是显示一个灰白的Window窗体
追问
我试了一下怎么没反应啊。没有窗口出来。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询