gpt4 book ai didi

java - 无法在退出屏幕上投影图像

转载 作者:行者123 更新时间:2023-11-29 05:31:50 33 4
gpt4 key购买 nike

问题是:当我关闭主程序时,我有一个带有一些图像(退出屏幕)的小窗口,并且该图像不可见,只有框架。当我启动程序时,我还有另一个图像,该图像在屏幕上可见,但第二个(关闭时)不可见。当我在 Eclipse 中编译这个类时,一切正常,但是当我运行 MAIN 程序时,它却没有。两者都是从同一个类生成的,但区别在于构造函数的参数。两个 PNG 文件都在这个项目的 JAR 文件和 WorkSpace 所在的文件夹中。请帮忙。

启动画面代码:

package Models;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

import MainProject.ExpertSystem;




public class SplashScreen extends JWindow {

private static final long serialVersionUID = 1L;
private int duration;
private ExpertSystem context;
private String graphicPath;
private boolean exit = false;
int width,height;
private boolean showContext;

public SplashScreen(int d, ExpertSystem context,String graphicPath,boolean exit,int width, int height,boolean showContext) {
duration = d;
this.graphicPath = graphicPath;
this.context = context;
this.exit = exit;
this.width = width;
this.height = height;
this.showContext = showContext;
showSplashAndExit();
}
public SplashScreen(int d)
{
duration = d;
}

// A simple little method to show a title screen in the center
// of the screen for the amount of time given in the constructor
public void showSplash() {

JPanel content = (JPanel)getContentPane();
content.setBackground(Color.white);

// Set the window's bounds, centering the window

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(graphicPath));
JLabel copyrt = new JLabel("Copyright 2013, Dariusz Kruk", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
//Color oraRed = new Color(156, 20, 20, 255);
// content.setBorder(BorderFactory.createLineBorder(oraRed, 0));

// Display it
setVisible(true);

// Wait a little while, maybe while loading resources
try
{
Thread.sleep(duration);
} catch (Exception e) {}

setVisible(false);
}

public void showSplashAndExit() {

showSplash();
if(showContext == true)
{
context.setVisible(true);
}
dispose();
//System.exit(0);

if(exit == true)
{
System.exit(0);
}
}

public static void main(String[] args) {
// Throw a nice little title page up on the screen first
SplashScreen splash = new SplashScreen(5000);
// Normally, we'd call splash.showSplash() and get on with the program.
// But, since this is only a test...
splash.showSplashAndExit();
}
}

启动欢迎屏幕:

public ExpertSystem()
{

introduction = new SplashScreen(5000,this,"Multimedia/AIWelcome.png",false,478,150,true);
..........
..........
}

启动退出屏幕:

else if(input == mWyjscie)
{

int odpo=JOptionPane.showConfirmDialog(this, "Czy na pewno wyjsc?","Pytanie",JOptionPane.YES_NO_OPTION);

if(odpo==JOptionPane.YES_OPTION)
{
this.setVisible(false);
exit = new SplashScreen(5000,this,"Multimedia/e.jpg",true,613,173,false);

}
else if(odpo==JOptionPane.NO_OPTION)
{
}
}

最佳答案

这可能是您调用 Thread.sleep() 的问题,它会阻塞 EDT。也许您应该使用 Swing Timer,或者如果您需要执行后台任务,甚至可以使用 SwingWorker。如果没有后台任务,我将只使用 javax.swing.Timer 类似这样的东西:

Timer timer = new Timer(duration, new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(false);
}
});
timer.setRepeats(false);
timer.start();

代替

try 
{
Thread.sleep(duration);
} catch (Exception e) {}

setVisible(false);

请注意我是如何在计时器中调用 showSlashAndExit 的,而不是在 main 中。或者也许你不想要那个。我不太确定你的程序流程。

编辑 测试这个程序。它运行良好。显示5秒后退出

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Splash extends JWindow{
int duration;

public Splash(int duration) {
this.duration = duration;

showSplash();
}

public void showSplash() {

setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);

Timer timer = new Timer(duration, new ActionListener(){
public void actionPerformed(ActionEvent e) {
setVisible(false);
System.exit(0);
}
});
timer.start();

}


public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
Splash splash = new Splash(5000);
}
});
}
}

关于java - 无法在退出屏幕上投影图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816044/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com