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);
}
}
}
+1,觀察效果很好。 :-) – Astrobleme
謝謝你的回報;工作和this.setVisible(假);用於打開下一頁時當前頁面不可見 – user2961276
歡迎您。標記爲解決方案,如果您願意;) – alex2410