简体   繁体   English

单击 JOptionPane.showMessageDialog 的确定按钮后如何关闭其他 Swing window

[英]How to close other Swing window after clicking JOptionPane.showMessageDialog's Ok button

I'm creating a login window, and the examples I found are having difficulty implementing because they don't show how to move on to the next window after trying to log in. Because It does not tell me how to close Login window and then opens other process's window after "login done" or "Confirm to login" message.我正在创建一个登录 window,而我发现的示例难以实施,因为它们没有显示在尝试登录后如何进入下一个 window。因为它没有告诉我如何关闭登录 Z05B8C74CBD96FBF2FB4F4C1A35270 和在“登录完成”或“确认登录”消息后打开其他进程的 window。

I made UI design with IntelliJ and this is my code.我使用 IntelliJ 进行 UI 设计,这是我的代码。 I know this code occours Error.我知道此代码出现错误。 because JOptionPane.showMessageDialog() returns void but i tried to get int return.因为JOptionPane.showMessageDialog()返回 void 但我试图让 int 返回。 I uesd it Just for example.我只是举例。

I understand that I have to use "JOptionPane.showConfirmDialog" to receive input as an integer type, but I just want to let user know that they just have logged in, so I don't want to show anything other than the OK button.我知道我必须使用“JOptionPane.showConfirmDialog”来接收 integer 类型的输入,但我只想让用户知道他们刚刚登录,所以我不想显示除 OK 按钮之外的任何内容。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginForm {
    private JPanel Login;
    private JTextField IDField;
    private JPasswordField PasswordField;
    private JButton LoginOKBtn;
    private JButton btnIDFind;
    private JButton btnPWFind;
    private JButton btnCreateAccount;

    //Test ID and Password
    final String testID="user";
    final String testPW="pw";

    public LoginForm() {
        //Login Btn Event
        LoginOKBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String ID=IDField.getText();
                String PW=new String(PasswordField.getPassword());
                System.out.println(ID);
                System.out.println(PW);

                //when ID and PW is collect then Show Dialog
                if (ID.equals(testID) && PW.equals(testPW)){
                    int loginOKbtnclicked = JOptionPane.showMessageDialog(null, "Succesfully Loged in!", "Info", JOptionPane.YES_OPTION);
                    if (loginOKbtnclicked == JOptionPane.OK_OPTION){
                        System.out.println("TODO - Closing Window Code");
                    }
                    else{

                    }
                }
                else {
                    JOptionPane.showMessageDialog(null,"WRONG Password or ID","Alert!",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Login");
        frame.setContentPane(new LoginForm().Login);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Just put the new frame after the messagebox.只需将新框架放在消息框之后。 And put the current frame here, do not send it with null .并将当前帧放在这里,不要用null发送。 JOptionPane.showMessageDialog(**null**, "Succesfully Loged in,", "Info". JOptionPane;YES_OPTION);

Then change the visibility of the current frame, or do what you like.然后改变当前帧的可见性,或者做你喜欢的事。 You do not need to do check if he clicked the button or not.你不需要检查他是否点击了按钮。 Since that messagebox has no purpose other than saying "You are in".因为那个消息框除了说“你在里面”之外没有其他目的。

I solved this using JOptionPane.showOptionDialog() .我使用JOptionPane.showOptionDialog()解决了这个问题。 I found JOptionPane.showMessageDialog() returns nothing But JOptionPane.showOptionDialog() returns int value.我发现JOptionPane.showMessageDialog()什么都不返回但是JOptionPane.showOptionDialog()返回 int 值。

This is my Test code这是我的测试代码

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login {
    private JPanel LoginPanel;
    private JButton button1;

    public static void main(String[] args) {
        JFrame frame=new JFrame("LOGIN!");
        frame.setContentPane(new Login().LoginPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public Login() {
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object[] options={"OK"};
                int in=-1;
                in=JOptionPane.showOptionDialog(
                        LoginPanel,
                        "Give me your choice",
                        "Info",
                        JOptionPane.NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE,
                        null,
                        options,
                        options[0]
                );
                if (in==0){
                    //Put Here closing old window code!
                    System.out.println("WAAAA");
                }
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对JOptionPane.showMessageDialog的OK执行操作 - How to perform action on OK of JOptionPane.showMessageDialog 在JOptionPane.showMessageDialog上如何关闭? - How to close when on JOptionPane.showMessageDialog? Swing中的动态JOptionPane.showMessageDialog - Dynamic JOptionPane.showMessageDialog in Swing 我可以设置 JOptionPane.showMessageDialog 的 window 的大小吗? - Can I set the size of the JOptionPane.showMessageDialog's window? 是否可以使用计时器关闭JOptionPane.showMessageDialog(buttonPanel,“”)?如果可以,请告诉我如何(程序代码关闭) - is it possible to close JOptionPane.showMessageDialog(buttonPanel,“ ”) with timer?if it's possible pls tell me how(the program code is down) JOptionPane.showMessageDialog等到单击确定? - JOptionPane.showMessageDialog wait until OK is clicked? JOptionPane.showMessageDialog之后不显示背景图像 - Background image is not displayed after JOptionPane.showMessageDialog 如何在升级中一次使用JOptionPane.showMessageDialog - How to use JOptionPane.showMessageDialog once in the prograde 如何从JOptionPane.showMessageDialog中读取 - How to read from a JOptionPane.showMessageDialog 当我单击JOptionPane.showMessageDialog中的“确定”按钮时,Java程序退出 - Java program exits when I click OK button in JOptionPane.showMessageDialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM