简体   繁体   English

如何使JFrame(带有计时器)从另一个类打开另一个JFrame

[英]How to make a JFrame (with timer) open another JFrame from another class

New here - my first question. 新来的-我的第一个问题。 Anyway, I am here because I want to make a JFrame, that is timed for 10000 milliseconds, I think, and then when it closes, it should open another (which is in another class). 无论如何,我在这里是因为我想制作一个JFrame,我认为它的时间为10000毫秒,然后当它关闭时,它应该打开另一个(在另一个类中)。 I already did the timer part, not the 'closing the timed JFrame, and opening another' part. 我已经完成了计时器部分,而不是“关闭定时的JFrame,然后打开另一个”部分。

I remember doing this, and found an answer. 我记得这样做,并找到了答案。 It went something like NewClass.show() ('NewClass' is the class name that should open) and then you type in OldClass.dispose() ('OldClass' is the class name that should close). 它类似于NewClass.show() (“ NewClass”是应打开的类名),然后键入OldClass.dispose() (“ OldClass”是应关闭的类名)。

Here is my code so far: 到目前为止,这是我的代码:

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);

    }
}

I didn't make the second class (which would be called 'LoadingScreen.class', but I will, and it will just have 'JSomethings' or whatever (like JFrame, JPanel, etc...) 我没有做第二个类(称为“ LoadingScreen.class”,但我会的,它将只有“ JSomethings”或诸如此类(例如JFrame,JPanel等)。

I can make the second class, but all I want is the first class to close after the timer finishes at 10 seconds, or 10000 milliseconds, and then automatically open the second class. 我可以进行第二堂课,但我只想在计时器在10秒(即10000毫秒)结束后关闭第一堂课,然后自动打开第二堂课。

Thanks 谢谢

Try adding the call to your second class like below 尝试将通话添加到您的第二堂课,如下所示

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();
    }
});

Another good practice is to call frame.setVisible(true) at the last so that you wont find any drift in the frame position on the screen. 另一个好的做法是最后调用frame.setVisible(true) ,这样您就不会在屏幕上的帧位置发现任何偏移。

Suppose your second class is like, 假设你的第二堂课是,

public class LoadingScreen extends JFrame   
{    
    public LoadingScreen(String title)  
    {   
        super(title);  
    }

    public void showGUI()   
    {
        //  Do whatever you want
        setSize(500,500);
        setVisible(true);
    }
}

You just have to call, 你只需要打电话,

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

当您调用frame.setVisoble(true)时,不会收到错误消息,无法从静态上下文引用非静态方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM