1

新来的-我的第一个问题。无论如何,我在这里是因为我想制作一个 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 毫秒结束后关闭第一堂课,然后自动打开第二堂课。

谢谢

4

3 回答 3

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)在最后调用,这样您就不会在屏幕上发现帧位置有任何漂移。

于 2013-02-28T09:21:40.323 回答
0

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

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();
于 2013-02-28T11:19:16.857 回答
0

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

于 2013-11-14T20:52:59.283 回答