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



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

启动画面代码:

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 的问题。也许您应该使用摆动计时器,如果您需要执行后台任务,甚至应该使用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中。或者也许你不想要那个。我不太确定您的程序流程。

EDit 测试此程序。它运行良好。它显示 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);
            }   
        });
    }
}

最新更新