【问题标题】:Can not project image on the exit screen无法在退出屏幕上投影图像
【发布时间】:2013-12-28 15:11:25
【问题描述】:

问题是:当我关闭主程序时,我有一个带有一些图像的小窗口(退出屏幕),这个图像不可见,只有框架。当我启动程序时,我还有另一个图像,这个图像在屏幕上可见,但第二个(关闭时)不可见。当我在 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)
            {           
            }
        }

【问题讨论】:

  • 等一下,您在第一次加载时正在看到图像吗?另外,是斜线屏幕,用于打开关闭,相同的对象
  • 是的。我看到第一个 SplashScreen 和第二个框架。 SplashScreen 类有两个对象
  • 所以中间没有程序?你只是想测试你的启动画面?
  • 这是我粘贴在这里的一部分代码,因为它真的很长。

标签: java eclipse user-interface


【解决方案1】:

您调用 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);
            }   
        });
    }
}

【讨论】:

  • 如果我做错了什么,请纠正我。我应该将您的答案粘贴到构造函数中,是吗?我已经制作了全局“私人计时器计时器”;并在构造函数中新建了一个 Timer,对吗?
  • 我不想给你错误的答案。因此,如果您的程序,请您详细说明流程。哪些调用按什么顺序以及出于什么原因进行
  • OK 1. 我创建了第一个 splashScreen。 2. 在程序中做一些事情。 3.退出程序时调用Exit SplashScreen。
  • +1,启动闪屏代码在常规线程上执行,因此 Thread.sleep() 对 GUI 的绘制没有影响。当您退出应用程序时,您的代码将从侦听器中执行。所有侦听器代码都在 Event Dispatch Thread 上执行,因此 Thread.sleep() 会阻止 EDT 并防止 GUI 重新绘制。您很幸运,启动闪屏可以正常工作,因为所有 GUI 代码都应该在 EDT 上执行。有关更多信息,请参阅 Concurrency 上的 Swing 教程。
  • 我不确定,但我认为它应该进入你的 showSplash 方法。只需替换try catch
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2023-02-07
  • 1970-01-01
  • 2015-04-11
相关资源
最近更新 更多