首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java语言中,如何退出从主JWindow创建的从JWindow?

在java语言中,如何退出从主JWindow创建的从JWindow?
EN

Stack Overflow用户
提问于 2011-09-23 00:19:11
回答 3查看 1.9K关注 0票数 2

我如何才能只退出我在Main中创建的新MainGame?Main是一个原始的游戏层。并且MainGame是对话窗口(例如模式窗口)。

Main.java:(主代码)

代码语言:javascript
复制
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扩展的,我只想这么说)。

代码语言:javascript
复制
public class MainGame extends JWindow 
{
  private static JWindow j;
  public MainGame()
  { 
    // some GUI ... 
  }

  public static void runit()
  {
    j = new MainGame();
    j.setVisible();

  }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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()

代码语言:javascript
复制
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();
            }
        });
    }
}
票数 3
EN

Stack Overflow用户

发布于 2011-09-23 00:53:47

我并不是很喜欢你的设计。但是有'j.dispose();‘。这应该是可行的。here是java文档。

注意:

  • dispose();-从memory.
  • setVisibilty(false);中删除窗口-只是将其从屏幕上隐藏起来。
  • 当窗口关闭时,您可以重写'dispose()‘函数来执行某些操作(如果是游戏,则更新比分),但在重写函数的末尾,您必须调用'super.dispose();’,因此将调用类窗口的函数。
票数 1
EN

Stack Overflow用户

发布于 2011-09-23 04:09:33

和MainGame是一个对话窗口

但这并不是您的代码所使用的。您使用的是JWindow。

你应该为一个模式窗口使用一个JDialog。然后,您只需处理()该窗口。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7518111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档