初学者:我有2个Java类;一个JFrame和一个包含重要变量和方法的类。如何控制优先权? (更多阅读)

问题描述:

我有2班叫ShopFrame(JFrame的)和进口商(有某些字段)初学者:我有2个Java类;一个JFrame和一个包含重要变量和方法的类。如何控制优先权? (更多阅读)

JFrame中有一个JList,并为要填充它从进口商召唤一个DefaultListModel类,但是当我运行JFrame类时它总是显示为空。

在Importer类中,我声明了一个空的DefaultListModel,在主方法的下面,我使用方法使用数据库信息填充它。如果我在方法中编写println循环,它会向我显示所有对象已成功添加到声明的DefaultListModel中。 问题是JFrame在填充之前似乎召唤声明的对象。

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

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

谢谢!

ShopFrame类

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 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]); 
     } 
    } 
+2

我们需要代码来查找错误,因为错误在代码中。通过阅读您的描述,我们无法可靠地知道您的代码是什么。代码很重要。 –

+0

当然,只是添加它。感谢您的时间到目前为止! –

你启动应用程序。 ShopFrame的主要方法称为。这将调用ShopFrame的构造函数。这个构造函数创建一个带有Importer.productsModel作为模型的JList。

请注意,在上述顺序中,导入程序的主要方法从不会在任何地方调用:应用程序只有一个入口点,而不是两个入口点。由于这是填充productsModel的方法,因此它从不填充,因此是空的。

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

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

Swing是一个相当复杂的API。在继续使用Swing之前,我会先教授类,对象,方法的概念。还要注意,尽管标题说了什么,但你的问题与继承无关。所以这可能是你应该熟悉的另一个概念。

+0

我一直在想你说的话 - 它非常有道理!对于这些愚蠢的错误抱歉 - 你可能不相信我只在9周前开始编程!还在习惯这一切。 –

+0

最后!经过3天的试验,我忽略了这样一个简单的概念。它现在完美。我的下一个工作就是完成这个任务,但我认为我很快就可以为其余的部分工作。 非常感谢您的建议和帮助。我很快会回来寻求更多的帮助,但我真的很感谢帮助! –