好的 - 我有以下代码 - 在已加星标的部分我收到错误。 ItemManager类要求我用Items填充()。但是,当我这样做时,我得到的错误是项目无法解析为变量,我不确定如何解决这个问题。
package presentation;
import javax.swing.*;
import business.ItemManager;
import java.awt.*;
import java.awt.event.*;
import business.*;
public class CreateInventoryUI extends JFrame {
private static final long serialVersionUID = -3940805393905465307L;
private JButton addBtn = new JButton ("Add Item to Inventory");
private JButton showBtn = new JButton ("Display Inventory");
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateInventoryUI frame = new CreateInventoryUI("Inventory");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception - Sorry");
}
}
});
}
public CreateInventoryUI(String name) { // title bar name
super(name);
// layout here
Container container = getContentPane();
FlowLayout layout = new FlowLayout();
container.setLayout(layout);
layout.setAlignment(FlowLayout.CENTER);
container.add(new JButton("Display inventory"));
container.add(new JButton("Add Item to Inventory"));
addBtn.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent event) {
// controller code
***ItemManager mngr = new ItemManager();
mngr.store(Items);***
}
});
showBtn.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent event) {
// controller code
ItemManager mngr = new ItemManager();
mngr.get(Items);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* Constructs a <code>String</code> with all attributes
* in name = value format.
*
* @return a <code>String</code> representation
* of this object.
*/
public String toString()
{
final String TAB = " ";
String retValue = "";
retValue = "CreateInventoryUI ( "
+ super.toString() + TAB
+ "addBtn = " + this.addBtn + TAB
+ "showBtn = " + this.showBtn + TAB
+ " )";
return retValue;
}
}
答案 0 :(得分:1)
您正在将Items
传递给mngr.store
,但您没有在任何地方声明Items
。 [此外,按照惯例,类名以大写开头,变量以小写开头,因此您可能希望将变量命名为items
,其类型为Items
]
答案 1 :(得分:0)
Items
未在代码示例中的任何位置定义。
此外,您可能想要添加这些按钮:
container.add(addBtn);
container.add(showBtn);
在CreateInventoryUI
方法中,不为container.add()
创建新实例。