我如何才能只退出我在Main中创建的新MainGame?Main是一个原始的游戏层。并且MainGame是对话窗口(例如模式窗口)。
Main.java:(主代码)
public class Main extends JWindow
{
private static JWindow j;
public static MainGame mp;
public static void main(String[] args)
{
new Thread(new Runnable()
{
public void run()
{
mp = new MainGame();
mp.runit();
//mp.stopit();
}
}).start();
j = new Main();
j.setVisible(true);
}
}MainGame.java:(这是由Main扩展的,我只想这么说)。
public class MainGame extends JWindow
{
private static JWindow j;
public MainGame()
{
// some GUI ...
}
public static void runit()
{
j = new MainGame();
j.setVisible();
}
}发布于 2011-09-23 01:05:07
1)更好的做法是实现CardLayout,因为为新的Window创建Top-Level Container,然后您只需在卡片之间切换
2)不要在运行时创建大量Top-Level Container,因为在当前实例存在之前,
setDefaultCloseOperation(Whatever);的setVisible(false)和
3)如果您要创建构造函数public JWindow(Frame owner),那么您将直接调用
SwingUtilities.getAccessibleChildrenCount()和SwingUtilities.getWindowAncestor()
import javax.swing.*;
import java.awt.*;
public class Testing {
private JFrame f = new JFrame("Main Frame");
private JWindow splashScreen = new JWindow();
public Testing() {
splashScreen = new JWindow(f);
splashScreen.getContentPane().setLayout(new GridBagLayout());
JLabel label = new JLabel("Splash Screen");
label.setFont(label.getFont().deriveFont(96f));
splashScreen.getContentPane().add(label, new GridBagConstraints());
splashScreen.pack();
splashScreen.setLocationRelativeTo(null);
splashScreen.setVisible(true);
new Thread(new Runnable() {
@Override
public void run() {
readDatabase();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}).start();
}
public void readDatabase() {
//simulate time to read/load data - 10 seconds?
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
public void createAndShowGUI() {
JLabel label = new JLabel("My Frame");
label.setFont(label.getFont().deriveFont(96f));
f.add(label);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
System.out.println("JFrame getAccessibleChildrenCount count -> "
+ SwingUtilities.getAccessibleChildrenCount(f));
System.out.println("JWindow getParent -> "
+ SwingUtilities.getWindowAncestor(splashScreen));
splashScreen.dispose();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Testing t = new Testing();
}
});
}
}发布于 2011-09-23 00:53:47
我并不是很喜欢你的设计。但是有'j.dispose();‘。这应该是可行的。here是java文档。
注意:
发布于 2011-09-23 04:09:33
和MainGame是一个对话窗口
但这并不是您的代码所使用的。您使用的是JWindow。
你应该为一个模式窗口使用一个JDialog。然后,您只需处理()该窗口。
https://stackoverflow.com/questions/7518111
复制相似问题