[英]My program can not open the second frame
I have been learning from this tutorial: "Java Eclipse GUI Tutorial 8 # How To Open A Second jframe using First jframe" and after logging in the second frame does not pop up 我已经从本教程中学习到:“ Java Eclipse GUI教程8#如何使用第一个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);
}
}
Here is my second frame source code: 这是我的第二帧源代码:
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
}
}
change your setVisible Method in User Info Class to below: 将用户信息类中的setVisible方法更改为以下内容:
public void setVisible(boolean b) {
frame.setVisible(b);
}
or 要么
The main
method of UserInfo
is not invoked. 不调用
UserInfo
的main
方法。 You must move your block code of main method into your constructor of UserInfo 您必须将main方法的代码块移动到UserInfo的构造函数中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.