首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的程序无法打开第二帧

我的程序无法打开第二帧
EN

Stack Overflow用户
提问于 2015-09-20 04:14:09
回答 1查看 79关注 0票数 1

我一直在学习本教程:“教程8#如何使用第一个jframe打开第二个jframe”,在登录到第二个框架后不会弹出

代码语言:javascript
复制
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);
}
}

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

代码语言:javascript
复制
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

}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-20 05:43:12

将用户信息类中的setVisible方法更改如下:

代码语言:javascript
复制
public void setVisible(boolean b) {
    frame.setVisible(b);
}

不调用UserInfoUserInfo方法。必须将主方法的块代码移动到UserInfo的构造函数中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32675737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档