Java 如何使一个JFrame(带计时器)从另一个类打开另一个JFrame
新的-我的第一个问题。无论如何,我在这里是因为我想制作一个JFrame,它的时间是10000毫秒,我想,当它关闭时,它应该打开另一个(在另一个类中)。我已经做了定时器部分,而不是“关闭定时JFrame,然后打开另一个”部分 我记得我这样做了,并找到了答案。它类似于Java 如何使一个JFrame(带计时器)从另一个类打开另一个JFrame,java,swing,jframe,splash-screen,multiple-instances,Java,Swing,Jframe,Splash Screen,Multiple Instances,新的-我的第一个问题。无论如何,我在这里是因为我想制作一个JFrame,它的时间是10000毫秒,我想,当它关闭时,它应该打开另一个(在另一个类中)。我已经做了定时器部分,而不是“关闭定时JFrame,然后打开另一个”部分 我记得我这样做了,并找到了答案。它类似于NewClass.show()('NewClass'是应该打开的类名),然后您键入OldClass.dispose()('OldClass'是应该关闭的类名) 以下是我目前的代码: import java.awt.Cursor; imp
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毫秒结束后关闭,然后自动打开第二个类
谢谢尝试将呼叫添加到第二节课,如下所示
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)
,这样您就不会在屏幕上的帧位置发现任何偏移。尝试将调用添加到第二个类中,如下所示
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)
,这样您就不会在屏幕上的帧位置发现任何漂移。假设您的第二个类是
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();
假设你的第二节课是
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();
当您调用frame.setVisoble(true)时,是否收到错误?无法从静态上下文引用非静态方法?当您调用frame.setVisoble(true)时,是否收到错误?无法从静态上下文引用非静态方法?请参阅“将被称为“SplashScreen.class”的内容”为什么不用于此?Java Web Start还支持启动映像。请参阅“将被称为‘SplashScreen.class’”为什么不用于此?Java Web Start还支持启动映像。感谢您的第二个建议-添加
frame.setVisible(true)
last,但我仍然不明白你对调用第二类代码的意思。我是Java新手,这是我在没有使用YouTube的情况下制作的第一个程序。当我尝试你所做的事情时,它只显示了第一类,但在第二类中没有显示JFrame。我做错了什么吗?我感到困惑。基本上,我只想处理LoadingScreen.class文件,当SplashScreen.class在10秒后处理完毕时,LoadingScreen.class应该显示出来。有时候,Java可能会有点烦人和困难。感谢您的第二个建议-添加frame.setVisible(true)
last,但我仍然不明白你对调用第二类代码的意思。我是Java新手,这是我在没有使用YouTube的情况下制作的第一个程序。当我尝试你所做的事情时,它只显示了第一类,但在第二类中没有显示JFrame。我做错了什么吗?我感到困惑。基本上,我只想处理LoadingScreen.class文件,当SplashScreen.class在10秒后处理完毕时,LoadingScreen.class应该显示出来。有时候,Java可能会有点烦人和困难。