简体   繁体   English

当我点击运行按钮时,我的 JFrame 不会出现,尽管 NetBeans 说它正在运行

[英]My JFrame won't show up when I hit Run button althought NetBeans said it is running

I created a simple form by NetBeans.我通过 NetBeans 创建了一个简单的表单。 Everythings worked fine but when I add some codes for the JTabel, it won't show up anything when I hit run.一切正常,但是当我为 JTabel 添加一些代码时,当我点击运行时它不会显示任何内容。 This is my code:这是我的代码:

public final class QuanLy extends javax.swing.JFrame {
    public QuanLy() {
        initComponents();
        show();
    }

    public ArrayList<Subject> subjectList(){
        ArrayList<Subject> subjectsList = new ArrayList<>();
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url = "jdbc:sqlserver://localhost;databaseName=QLLTCK;integratedSecurity=true;";
            Connection connection = DriverManager.getConnection(url);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM PHANCONG");

            while(resultSet.next()){
                Subject subject = new Subject(resultSet.getString("maLop"), resultSet.getString("tenMH"), 
                        resultSet.getString("ngayThi"), resultSet.getInt("caThi"), resultSet.getString("phongThi"), 
                        resultSet.getInt("soSV"), resultSet.getInt("soCBCT"), resultSet.getString("xepLoai"));
                subjectsList.add(subject);
            }
        }
        catch(Exception sqlex){
            sqlex.printStackTrace();
        }        
        return subjectsList;
    }

    @Override
    public void show(){
        ArrayList<Subject> list = subjectList();
        DefaultTableModel model = (DefaultTableModel)table.getModel();
        Object[] row = new Object[8];
        for(int i = 0; i < list.size(); i++){
            row[0] = list.get(i).getMaLop();
            row[1] = list.get(i).getTenMH();
            row[2] = list.get(i).getNgayThi();
            row[3] = list.get(i).getCaThi();
            row[4] = list.get(i).getPhongThi();
            row[5] = list.get(i).getSoSV();
            row[6] = list.get(i).getSoCBCT();
            row[7] = list.get(i).getXepLoai();
            model.addRow(row);
        }
    }
//... (those lines are created by NetBeans)
}

When I comment out the show(), public ArrayList subjectList() and public void show(), my form show up normally.当我注释掉 show()、public ArrayList subjectList() 和 public void show() 时,我的表单正常显示。

This is the image.这是图像。 Running but not showing.运行但不显示。 https://imgur.com/ibAH1ML https://imgur.com/ibAH1ML

This is the project without the codes I have shown.这是没有我展示的代码的项目。 https://imgur.com/Ha04mwl https://imgur.com/Ha04mwl

Can someone help me?有人能帮我吗? Thanks.谢谢。

Sorry for my bad English.对不起,我的英语不好。

You are overriding the show function but you are not implementing a display logic or calling the super show.您正在覆盖 show 函数,但没有实现显示逻辑或调用超级显示。 The easiest would be to just call super.show() at the end of your show function最简单的方法是在 show 函数结束时调用 super.show()

First of all the show() method is deprecated.首先,不推荐使用show()方法。 You should use setVisible(true) instead.您应该使用setVisible(true)代替。

Secondly, it is not good practice to do an overridable method call in constructor, since this could lead to undesired bugs.其次,在构造函数中调用可覆盖的方法不是一个好习惯,因为这可能会导致不希望的错误。 Constructors should never invoke overridable methods, directly or indirectly, because the superclass constructor runs before the subclass constructor, so the overriding method in the subclass will be invoked before the subclass constructor has run.构造函数不应直接或间接调用可覆盖的方法,因为超类构造函数在子类构造函数之前运行,因此子类中的覆盖方法将在子类构造函数运行之前被调用。 If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.如果覆盖方法依赖于子类构造函数执行的任何初始化,则该方法将不会按预期运行。

What you can do, is to rename your show() method to initTable() for instance, and call it just after your form has been created.例如,您可以做的是将您的show()方法重命名为initTable() ,并在您的表单创建后立即调用它。 If you created in the main method for instance, you could do something like:例如,如果您在 main 方法中创建,您可以执行以下操作:

public static void main(String[] args) {
     QuanLy frame = new QuanLy();

     // ... other initialization stuff ...

     frame.setVisible(true);
     frame.initTable(); // the initTable is you show() method renamed

     // ... rest of your code ...

}

The recommended way of doing this is like this推荐的方法是这样的


SwingUtilities.invokeLater(() -> {
    frame.pack();
    frame.setVisible(true);
});

The lambda can also be replaced with a Runnable object with its run method being overridden.也可以将 lambda 替换为 Runnable 对象,并覆盖其 run 方法。

Don't use the .show() method as it has been deprecated.不要使用 .show() 方法,因为它已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM