Frage

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

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);
    }
}
}
War es hilfreich?

Lösung

this.setVisible(false); set your JFrame invisible always, after executing actionPerformed() method. You need to use that, only with success login.

Seems adding return; after JOptionPane.showMessageDialog(null, message); solve your problem.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top