2014-04-15 29 views
0

我正在编写登录页面的程序我拥有包括JOptionPane在内的所有内容,除非在选项窗格上单击确定时关闭该选项并且JFrame我如何更改我的代码停止关闭JFrame,只是关闭执行actionPerformed()方法后JOptionPane如何在JOptionPane中单击确定时关闭JFrame

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

回答

3

this.setVisible(false);设置你的JFrame无形始终。你需要使用它,只有成功登录。

看起来加return;JOptionPane.showMessageDialog(null, message);解决你的问题。

+0

+1,观察效果很好。 :-) – Astrobleme

+0

谢谢你的回报;工作和this.setVisible(假);用于打开下一页时当前页面不可见 – user2961276

+0

欢迎您。标记为解决方案,如果您愿意;) – alex2410