嗨,我有一个名为main.java的主类,其中有:

import my.rcsv1.accounting.DBConnect;

public class main {
    public static void main(String[] args) {
        DBConnect connect = new DBConnect();
        RCSAccV1GUI gui = new RCSAccV1GUI();
    }

}


我还有另外两个类DBConnect和RCSAccV1GUI。 DBConnect类包含以下内容:

public class DBConnect {

    public Connection con;
    public Statement st;
    public PreparedStatement pst = null;
    public ResultSet rs;


    public DBConnect(){
        try{
            String driverMySQL = "com.mysql.jdbc.Driver";
            Class.forName(driverMySQL);
            con = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=pass");
            st = con.createStatement();
        }catch(ClassNotFoundException | SQLException ex){
            JOptionPane.showMessageDialog(null, ex);
        }
    }
}


RCSAccV1GUI类是一个空白JFrame,当前仅具有一个标签作为标题。

package my.rcsv1.accounting;

import my.rcsv1.accounting.DBConnect;

public class RCSAccV1GUI extends javax.swing.JFrame {
    public RCSAccV1GUI() {
        initComponents();
    }
    public static void main(String args[]) {


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RCSAccV1GUI().setVisible(true);
            }
        });
}


当我在NetBeans中运行主类时,它会继续运行,并且不会生成JFrame。为什么在运行主类时无法打开JFrame?

最佳答案



    gui.setVisible(true);




    RCSAccV1GUI gui = new RCSAccV1GUI();


或在此块内

public RCSAccV1GUI() {
    initComponents();
}


将可见性设置为true。

10-08 20:00