【问题标题】:How to make a JFrame (with timer) open another JFrame from another class如何使 JFrame(带计时器)从另一个类中打开另一个 JFrame
【发布时间】:2013-02-28 09:01:04
【问题描述】:

这里是新的 - 我的第一个问题。无论如何,我在这里是因为我想制作一个 JFrame,它的计时时间为 10000 毫秒,我想,然后当它关闭时,它应该打开另一个(在另一个类中)。我已经做了计时器部分,而不是“关闭定时 JFrame,然后打开另一个”部分。

我记得这样做,并找到了答案。它类似于NewClass.show()('NewClass' 是应该打开的类名),然后你输入OldClass.dispose()('OldClass' 是应该关闭的类名)。

到目前为止,这是我的代码:

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SplashScreen extends JPanel {

public SplashScreen() {
 setOpaque(false);
 setLayout(new FlowLayout());
}

public static void main(String[] args) {

    final JFrame frame = new JFrame("Loading game...");
    frame.setSize(800, 600);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SplashScreen background = new SplashScreen();
    frame.add(background);

    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            frame.dispose();
           //I want to place my code here so then this class will close, and then the other class will open
        }
    });
    timer.setRepeats(false);
    timer.start();

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("Waiting.png");
    Point hotSpot = new Point(0,0);
    Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Cursor");
    frame.setCursor(cursor);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;

    frame.setLocation(x, y);

    JButton button = new JButton();

    JPanel panel = new JPanel();

}

public void paint(Graphics g) {
    Image a=Toolkit.getDefaultToolkit().getImage("Splash Screen.gif");
    g.drawImage(a,0,0,getSize().width,getSize().height,this);
    super.paint(g);

    }
}

我没有制作第二个类(将被称为“LoadingScreen.class”,但我会的,它只会有“JSomethings”或其他任何东西(如 JFrame、JPanel 等......)

我可以做第二堂课,但我只想在计时器在 10 秒或 10000 毫秒后关闭第一堂课,然后自动打开第二堂课。

谢谢

【问题讨论】:

标签: java swing jframe splash-screen multiple-instances


【解决方案1】:

尝试将呼叫添加到您的第二类,如下所示

Timer timer = new Timer(10000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        frame.setVisible(false);
        frame.dispose();
       //I want to place my code here so then this class will close, and then the other class will open

       //SplashScreen screen = new SplashScreen();
       //screen.showGUI();
    }
});

另一个好的做法是在最后调用frame.setVisible(true),这样您就不会发现屏幕上的帧位置有任何偏移。

【讨论】:

  • 感谢您的第二个建议 - 最后添加 frame.setVisible(true),但我仍然不明白您将代码调用到第二类的意思。我是 Java 新手,这是我在不使用 YouTube 的情况下制作的第一个程序。当我尝试你所做的事情时,它只显示了第一堂课,但没有在第二堂课中显示 JFrame。我做错什么了吗?我很困惑。基本上,我想要的只是处置 LoadingScreen.class 文件,当 SplashScreen.class 在 10 秒后处置时,应该显示 LoadingScreen.class。有时,Java 可能会有点烦人和困难。
【解决方案2】:

当您调用 frame.setVisoble(true) 时,您不会收到错误 non static method cannot be referenced from static context?

【讨论】:

    【解决方案3】:

    假设你的第二节课是这样的,

    public class LoadingScreen extends JFrame   
    {    
        public LoadingScreen(String title)  
        {   
            super(title);  
        }
    
        public void showGUI()   
        {
            //  Do whatever you want
            setSize(500,500);
            setVisible(true);
        }
    }
    

    你只需要打电话,

    LoadingScreen loadScreen = new LoadingScreen("Loading screen....");
    loadscreen.showGUI();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 2019-02-22
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多