Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在单击JOptionPane.showMessageDialog';s Ok按钮_Java_Swing_Authentication_Jframe_Awt - Fatal编程技术网

Java 如何在单击JOptionPane.showMessageDialog';s Ok按钮

Java 如何在单击JOptionPane.showMessageDialog';s Ok按钮,java,swing,authentication,jframe,awt,Java,Swing,Authentication,Jframe,Awt,我正在创建一个登录窗口,我发现的示例很难实现,因为它们没有显示在尝试登录后如何转到下一个窗口。 因为它没有告诉我如何关闭登录窗口,然后在“登录完成”或“确认登录”消息后打开其他进程的窗口 我用IntelliJ做了UI设计,这是我的代码。 我知道这个密码有错误。因为JOptionPane.showMessageDialog()返回void,但我尝试获取int return。我只是举个例子 我知道我必须使用“JOptionPane.showConfirmDialog”以整数类型接收输入,但我只想让用

我正在创建一个登录窗口,我发现的示例很难实现,因为它们没有显示在尝试登录后如何转到下一个窗口。 因为它没有告诉我如何关闭登录窗口,然后在“登录完成”或“确认登录”消息后打开其他进程的窗口

我用IntelliJ做了UI设计,这是我的代码。 我知道这个密码有错误。因为JOptionPane.showMessageDialog()返回void,但我尝试获取int return。我只是举个例子

我知道我必须使用“JOptionPane.showConfirmDialog”以整数类型接收输入,但我只想让用户知道他们刚刚登录,所以除了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);
    }
}


只需将新框架放在messagebox之后。并将当前帧放在此处,不要使用null发送。
JOptionPane.showMessageDialog(**null**,“成功登录!”,“Info”,JOptionPane.YES\u选项)


然后更改当前帧的可见性,或执行您喜欢的操作。你不需要检查他是否点击了按钮。因为这个消息框除了说“你在”之外没有其他用途。我使用了JOptionPane.showOptionDialog()解决了这个问题。 我发现JOptionPane.showMessageDialog()只返回JOptionPane.showOptionDialog()返回int值

这是我的测试代码

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");
                }
            }
        });
    }
}