我正在编写一个登录页面的程序我已经完成了所有工作,包括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);
}
}
}
答案 0 :(得分:3)
this.setVisible(false);
方法后, JFrame
始终将actionPerformed()
设置为不可见。只有成功登录才需要使用它。
似乎在return;
解决问题后添加JOptionPane.showMessageDialog(null, message);
。