简体   繁体   English

在JOptionPane中单击“确定”时如何停止JFrame关闭

[英]How to stop JFrame from closing when clicking ok in a JOptionPane

I'm writing a program for a login page I have everything working including the JOptionPane except when I click ok on the option pane it closes that and the JFrame how do I change my code to stop closing the JFrame and just close the JOptionPane 我正在为登录页面编写程序,所有工作都包括JOptionPane在内,但当我在选项窗格上单击“确定”时,它关闭了该窗口,而JFrame如何更改我的代码以停止关闭JFrame并仅关闭JOptionPane

package softwareDesign;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.sql.ResultSetMetaData;


public class LoginPage extends JFrame implements ActionListener {
JPanel panel;
JLabel label;
JTextField userfield;
JPasswordField passfield;
JButton loginButton, createButton;
boolean authenticated = false;
MySQLEngine engine = new MySQLEngine("root", "");

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    new LoginPage();

}

public LoginPage() {
    super("The Online Music Store");

    Container c = getContentPane();
    panel = new JPanel();

    label = new JLabel();
    label.setText("Log In");
    Font Fancyfont = new Font("Calibri(Body)", Font.BOLD, 26);
    label.setFont(Fancyfont);

    userfield = new JTextField(20);
    Font Fancyfont1 = new Font("Calibri(Body)", Font.ITALIC, 12);
    userfield.setFont(Fancyfont1);

    passfield = new JPasswordField(10);
    Font Fancyfont2 = new Font("Calibri(Body)", Font.ITALIC, 12);
    passfield.setFont(Fancyfont2);

    loginButton = new JButton("Log In");
    loginButton.addActionListener(this);
    loginButton.setBackground(Color.CYAN);

    createButton = new JButton("Create Account");
    createButton.setBackground(Color.CYAN);
    createButton.addActionListener(this);

    engine.connect();

    panel.add(userfield);
    panel.add(passfield);
    panel.add(loginButton);
    panel.add(createButton);
    c.add(label, BorderLayout.NORTH);
    c.add(panel);

    setVisible(true);
    setSize(300, 300);
    setLocation(500, 200);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getActionCommand().equals("Create Account")) {
        this.setVisible(false);
        new createAccount();
    }

    if (e.getActionCommand().equals("Log In")) {
        ResultSet rs;
        ResultSetMetaData rsmd = null;
        int colCount = 0;
        String[] colNames = null;

        try {
            rs = engine.executeQuery("select * from login");
            rsmd = rs.getMetaData();
            colCount = rsmd.getColumnCount();
            colNames = new String[colCount];
            for (int i = 1; i <= colCount; i++) {
                colNames[i - 1] = rsmd.getColumnName(i);
            }

            String[] currentRow = new String[colCount];// array to hold the
                                                        // row data
            while (rs.next()) { // move the rs pointer on to the next record
                                // (starts before the 1st)
                for (int i = 1; i <= colCount; i++) {
                    currentRow[i - 1] = rs.getString(i);
                }

                if ((currentRow[0].equals(userfield.getText()))
                        && (currentRow[1].equals(passfield.getText()))) {
                    authenticated = true;   
                }

            }

        //System.out.println(authenticated);

        }
        catch (SQLException a)
        {
            System.err.println("SQLException: " + a.getMessage());
        }

    if(authenticated == true)
    {
        try 
        {
            new Info();
        }
        catch (SQLException e1) 
        {
            e1.printStackTrace();
        }
    }

    else if(authenticated == false)
    {
        String message = "Incorrect Username/Password";
        JOptionPane.showMessageDialog(null, message);
    }

    this.setVisible(false);
    }
}
}

this.setVisible(false); set your JFrame invisible always, after executing actionPerformed() method. 在执行actionPerformed()方法之后,始终将JFrame设置为不可见。 You need to use that, only with success login. 您仅在成功登录后才需要使用它。

Seems adding return; 似乎增加了return; after JOptionPane.showMessageDialog(null, message); JOptionPane.showMessageDialog(null, message); solve your problem. 解决您的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM