初学者:我有2个Javaclass;一个 JFrame,以及一个带有重要变量和方法的 class。如何控制优先级? (更多阅读)

Beginner: I have 2 Java classes; a JFrame, and a class with important variables and methods. How to control precedence? (More to read)

我有 2 个 classes 叫做 ShopFrame(JFrame) 和 Importer(有一些字段)

JFrame 有一个 JList,为了填充它,它从导入器 class 调用了一个 DefaultListModel,但是当我 运行 JFrame class 时它总是显示为空].

在 Importer class 中,我声明了一个空的 DefaultListModel,然后在 main 方法中,我使用方法用数据库信息填充它。如果我在该方法中编写一个 println 循环,它会告诉我所有对象都已成功添加到声明的 DefaultListModel 中。 问题 是 JFrame 似乎在它被填充之前召唤声明的对象。

这可能是一个非常愚蠢的继承问题,或者什么的!但如果可以的话请帮帮我。

我认为这不需要示例,但请询问是否需要,这似乎更像是一个简单的逻辑问题。

谢谢!

ShopFrame Class

    public class ShopFrame extends JFrame {

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ShopFrame frame = new ShoppingFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

    public ShoppingFrame() {
    setTitle("Shopping Application\r\n");
    // Adaptive window size
    setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 900, 556);
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{45, 0, 20, 0, 45, 1, 45, 0, 20, 0, 45, 0};
    gridBagLayout.rowHeights = new int[]{45, 0, 0, 0, 0, 0, 20, 0, 0, 20, 0, 0, 20, 0, 30, 0, 45, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    getContentPane().setLayout(gridBagLayout);

    JPanel panel = new JPanel();
    panel.setMinimumSize(new Dimension(5, 20));
    panel.setBackground(SystemColor.scrollbar);
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.gridheight = 17;
    gbc_panel.insets = new Insets(0, 0, 0, 5);
    gbc_panel.fill = GridBagConstraints.BOTH;
    gbc_panel.gridx = 5;
    gbc_panel.gridy = 0;
    getContentPane().add(panel, gbc_panel);

    //Importer.productsModel.addElement("This gets added to the empty DefaultListModel, but not the Importer Class's info.");
    JList<String> productBox = new JList<String>(Importer.productsModel);
    GridBagConstraints gbc_list = new GridBagConstraints();
    gbc_list.gridheight = 4;
    gbc_list.gridwidth = 3;
    gbc_list.insets = new Insets(0, 0, 5, 5);
    gbc_list.fill = GridBagConstraints.BOTH;
    gbc_list.gridx = 1;
    gbc_list.gridy = 2;
    getContentPane().add(productBox, gbc_list);
    }

进口商Class

class Importer {

// Declarations
public static ArrayList<Product> productList = new ArrayList<Product>();
public static DefaultListModel<String> productsModel = new DefaultListModel<String>();

// Connect to database
private static Connection connect = null;

// Running parts of SQL
private static Statement statement = null;

// Running parts of SQL that take parameters
private static PreparedStatement preparedStatement = null;

// Result from a SELECT CELL statement
private static ResultSet resultSetProducts = null;
}

public static void main(String[] args) {
    try {
        connect = DriverManager.getConnection(
                "jdbc:mysql://localhost/shop?useSSL=true"
                + "&user=root&password=Pa$$");

        statement = connect.createStatement();

        // This imports products into a collection
        resultSetProducts = statement.executeQuery(
                "select * from shop.products");
        exportProductsAsCollection(resultSetProducts, productList, productsModel);

        connect.close();

    } catch (Exception e) {
        System.out.println(e);
    }
}

public static void exportProductsAsCollection(ResultSet resultSetProducts, ArrayList<Product> productList, DefaultListModel<String> productModel) throws SQLException {

    while (resultSetProducts.next()) {
        // Adds each product object (Product) into ArrayList
        productList.add(new Product(resultSetProducts.getString("ProductName"), resultSetProducts.getDouble("Price")));
    }
      // Exports names of products for use in JList
      String[] arr = new String[productList.size()];
      for (int i = 0; i < arr.length; i++)
      {
         arr[i] = productList.get(i).getProductName();
         productModel.addElement(arr[i]);
         // To test
     System.out.println(arr[i]);
      }
    }

您开始申请。 ShopFrame 的主要方法被调用。这将调用 ShopFrame 的构造函数。此构造函数创建一个以 Importer.productsModel 作为其模型的 JList。

请注意,在上述序列中,Importer 的主要方法从未在任何地方被调用:一个应用程序只有一个入口点,而不是两个。因为它是填充 productsModel 的方法,所以它从未被填充,因此是空的。

删除该主要方法,并将其转换为创建、填充和returns框架中所需数据的方法。删除所有这些 public 静态变量。

从您的框架调用该新方法,并用返回的内容填充 JList。

Swing 相当复杂 API。在进一步使用 Swing 之前,我会熟悉 类、objects 方法的概念。另请注意,您的问题与继承无关,尽管标题是这么说的。所以这可能是您应该熟悉的另一个概念。