java - 我的程序无法打开第二帧

标签 java swing awt

我一直在学习这个教程:《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);
}
}

这是我的第二个框架源代码:

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

}

}

最佳答案

将用户信息类中的 setVisible 方法更改为以下内容:

public void setVisible(boolean b) {
    frame.setVisible(b);
}

未调用UserInfomain 方法。 您必须将 main 方法的 block 代码移至 UserInfo 的构造函数中

关于java - 我的程序无法打开第二帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32675737/

相关文章:

java - JFrame 的边框布局定位不符合预期

java - FlowLayout 中组件和容器边框之间的自由空间

Java 代码无法在 Mac OS X 上运行

java - 创建Android应用程序架构设计图的示例

java - sikuli 找不到图像文件

java - 如何将 Antlr4 生成的词法分析器/解析器集成到我的 java 项目中

java - 通过 html 轻松休息不起作用

java - 在悬停/单击 JMenuItem 时重新绘制 JPanel

java - 带有阿拉伯字符的drawString-java

java - 为什么在 main 方法中使用 SwingUtilities.invokeLater?