我一直在学习本教程:" Java Eclipse GUI教程8#如何使用First jframe打开第二个jframe"登录后第二帧不会弹出
import java.awt.EventQueue;
import java.awt.Image;
import java.sql.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Login {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login window = new Login();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connection=null;
private JTextField textFieldUN;
private JPasswordField passwordField;
private JLabel lblNewLabel_1;
/**
* Create the application.
*/
public Login() {
initialize();
connection=sqliteConnection.dbConnector();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 527, 302);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Username:");
lblNewLabel.setBounds(64, 67, 63, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(64, 114, 82, 20);
frame.getContentPane().add(lblPassword);
textFieldUN = new JTextField();
textFieldUN.setBounds(157, 64, 86, 20);
frame.getContentPane().add(textFieldUN);
textFieldUN.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String query="select* from userInfo where username=? and password=? ";
PreparedStatement pst=connection.prepareStatement(query);
pst.setString(1,textFieldUN.getText());
pst.setString(2,passwordField.getText());
ResultSet rs=pst.executeQuery();
int count=0;
while(rs.next()){
count=count+1;
}
if(count ==1)
{
JOptionPane.showMessageDialog(null,"Username and password is correct");
frame.dispose();
UserInfo usInfo= new UserInfo();
usInfo.setVisible(true);
}
else if(count>1)
{
JOptionPane.showMessageDialog(null, "Dulicate Username ans password");
}
else {
JOptionPane.showMessageDialog(null, "Username and password is NOT correct");
}
//u can use rs.close
// and write pst.close
// so u don't have to code what's on the bottom
}catch(Exception e1)
{
JOptionPane.showMessageDialog(null, e1);
}
finally{
try{
}catch(Exception e1)
{
JOptionPane.showMessageDialog(null, e1);
}
}
}
});
btnLogin.setBounds(157, 166, 89, 23);
frame.getContentPane().add(btnLogin);
passwordField = new JPasswordField();
passwordField.setBounds(156, 114, 87, 20);
frame.getContentPane().add(passwordField);
lblNewLabel_1 = new JLabel("");
Image img = new ImageIcon(this.getClass().getResource("/Crimson_Arks_final.jpg")).getImage();
lblNewLabel_1.setIcon(new ImageIcon(img));
lblNewLabel_1.setBounds(317, 25, 184, 184);
frame.getContentPane().add(lblNewLabel_1);
}
}
这是我的第二帧源代码:
import java.awt.EventQueue;
import java.awt.Image;
import java.sql.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
public class UserInfo {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserInfo window = new UserInfo();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UserInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblHelloUser = new JLabel("Hello User");
lblHelloUser.setFont(new Font("Sitka Display", Font.BOLD, 23));
lblHelloUser.setBounds(99, 42, 121, 54);
frame.getContentPane().add(lblHelloUser);
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
将用户信息类中的setVisible方法更改为以下内容:
public void setVisible(boolean b) {
frame.setVisible(b);
}
或
未调用main
UserInfo
方法。
您必须将main方法的块代码移动到UserInfo的构造函数