簡體   English   中英

bug報錯不加載window和Java Swing

[英]Bug error does not load the window with Java Swing

首先,我將它們放在上下文中,我正在使用 Java Swing 進行一個小項目。在使用 Java 8 和 Ant 制作該項目之前,它運行良好。 我決定將代碼更新為 Java 17 和 Maven,此時問題開始了(我使用的是 Netbeans IDE)

現在問題來了,當你運行程序時它只顯示一個小的 window 甚至沒有顯示應該出現的組件之一

運行程序時應該顯示的view是這樣的

introducir la descripción de la imagen aquí

取而代之的是

introducir la descripción de la imagen aquí

擴容時 windows introducir la descripción de la imagen aquí

在實際的 pom.xml 文件中,我有這些依賴項

    <dependencies>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.9.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.8.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <optional>true</optional>
        </dependency>

    </dependencies>

Ant 的項目有這些庫

在此處輸入圖像描述

程序啟動時,會執行布局構造函數,調用這些類

public PrincipalLayout() {
        this.service = new ProductService();
        //La base de datos esta vacia
        this.model = new TableModel(service.selectAllProducts());
}

//constructor and parameters of ProductService
private final InMemoryProductRepository mysqlController;
private List<Product> inMemoryList;
public ProductService(){
        this.mysqlController = new InMemoryProductRepository();
        var optionalProducts = mysqlController.selectAll();
        
        if(optionalProducts.isPresent())
            optionalProducts.get().forEach(p-> this.inMemoryList.add( p));
        if(optionalProducts.isEmpty())
            this.inMemoryList.addAll(Collections.emptyList())
}

//the selectAll from the repository
@Override
public Optional<List<Product>> selectAll() {
        List<Product> products = Collections.emptyList();

        try {
            PreparedStatement preparedStatement = ConnectionJDBC
                    .getConnection().prepareStatement(SQL_SELECT.getCommand());
            ResultSet resultSet = preparedStatement.executeQuery();
     
            while (resultSet.next()) {
                var product = new Product(
                        resultSet.getInt("product_id"),
                        resultSet.getString("name"),
                        resultSet.getDouble("price")
                );
                products.add(product);
            }

            ConnectionJDBC.close(resultSet);
            ConnectionJDBC.close(preparedStatement);
            ConnectionJDBC.close(ConnectionJDBC.getConnection());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Ocurrio un error al ejecutar SellectAll", "ERROR", 0);
        }
   
        return Optional.of(products);
}

我仍在編輯問題,看看是否可以通過這種方式找到可能存在錯誤的地方,我仍然接受代碼中的建議:)

好吧,我終於解決了這個問題,發生的事情是 .form 文件生成了一個名為“initComponents”的方法,正如您將在上面的構造函數中看到的那樣,我沒有調用該方法......所以將它添加到構造函數中解決了它。 這是胡說八道。

解決方案

public PrincipalLayout() {
        this.service = new ProductService();
        this.model = new TableModel(service.selectAllProducts());
        initComponents();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM