无法解析为变量 - GUI 控制器代码


好的 - 我

有以下代码 - 在加星标的部分,我收到错误。 类要求我用 Items 填充 ()。 但是,当我这样做时,我收到错误,即无法将 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;
    }

}

您正在将Items传递给mngr.store,但您没有在任何地方声明Items。[此外,按照惯例,类名以大写字母开头,变量以小写字母开头,因此您可能希望将变量命名为 items ,其类型为 Items ]

Items

在代码示例中的任何位置定义。

此外,您可能希望添加以下按钮:

container.add(addBtn);
container.add(showBtn);

CreateInventoryUI 方法中,不为 container.add() 创建新实例。

最新更新