JDialog设置另一个窗口 [英] JDialog disposing another window

查看:92
本文介绍了JDialog设置另一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个带有开始屏幕"框架的Java游戏.在开始屏幕中,我有一个名为buttonLogin的按钮.按下buttonLogin后,将弹出由LoginDialog类启动的登录对话框,要求您输入用户名和密码.在对话框中,有两个按钮,登录和取消.按登录后,我的游戏将打开,但开始屏幕仍然可见.

I'm currently working on a java game which opens up with a "start screen" frame. In the startscreen, I have a button called buttonLogin. Once you press buttonLogin, a login dialog launched by a LoginDialog class will pop up asking you for a username and password. In the dialog there are two buttons, login and cancel. Once you press login, my game will open, but the start screen is still visible.

我的问题是我不知道如何在LoginDialog类的actionPerformed方法中编写代码以关闭现有的StartScreen窗口.

My problem is that I do not know how to write code in the actionPerformed method of my LoginDialog class to close the existing StartScreen window.

请记住,我是在LoginDialog类而不是StartScreen类中编写的.

Keep in mind that I am writing in the LoginDialog class and not the StartScreen class.

推荐答案

根据要实现的目标,可以使用setVisible方法或dispose方法.

Depending on what you want to achieve, you can use the setVisible method or the dispose method.

如果需要,您可以将StartScreen实例作为参数传递给LoginDialog类.

If needed, you can just pass your StartScreen instance as a parameter to your LoginDialog class.

另一种方法是为您的LoginDialog类提供一个登录后"操作的设置器.然后StartScreen可以创建并设置用于处置开始屏幕的操作.

Another approach would be to give your LoginDialog class a setter for an 'after-login' action. The StartScreen can then create and set an action which disposes the startscreen.

编辑

为了使登录后"操作更加清楚,我的意思是类似

To make the 'after-login' action a bit more clear, I meant something along the lines of

public class LoginDialog{
  Action afterLoginAction;
  public void setAfterLoginAction( Action action ){
    afterLoginAction = action;
  }
  public void loginButtonPressed(){
    //do your stuff
    if ( afterLoginAction != null ){
      afterLoginAction.actionPerformed( new ActionEvent( ... ) );
    }
  }
}

public class StartScreen extends JWindow{
  public void showLoginScreen(){
    LoginDialog loginDialog = new LoginDialog();
    loginDialog.setAfterLoginAction( new Action(){
       @Override
       public void actionPerformed( ActionEvent e ){
           StartScreen.this.dispose();
       }
     } );
    loginDialog.setVisible( true );
  }
}

这篇关于JDialog设置另一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆