您好,我有一个名为 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();
或在此 block 内
public RCSAccV1GUI() {
initComponents();
}
设置可见性为真。
关于用于加载未运行的 GUI 的 Java 主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26887200/