Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象参数传递给actionPerformed中调用的方法?_Java_Actionlistener - Fatal编程技术网

Java 如何将对象参数传递给actionPerformed中调用的方法?

Java 如何将对象参数传递给actionPerformed中调用的方法?,java,actionlistener,Java,Actionlistener,我正在为一个学校项目编写一个库存控制系统程序。这是我需要做的最后一件事,但鉴于我是一个相对的JavaNoob,我恳请您的帮助 我有一个DisplayRecord类,它是通过从搜索类中的搜索JTextField获取字符串输入,找到它链接到的对象产品p,并将其传递给DisplayRecord方法创建的。这部分工作得很好 我想根据按下的JButton将产品p传递给EditProduct类或DeleteRecord类,这样用户就可以编辑同一产品的名称、数量或成本。下面是我的DisplayRecord、E

我正在为一个学校项目编写一个库存控制系统程序。这是我需要做的最后一件事,但鉴于我是一个相对的JavaNoob,我恳请您的帮助

我有一个DisplayRecord类,它是通过从搜索类中的搜索JTextField获取字符串输入,找到它链接到的对象产品p,并将其传递给DisplayRecord方法创建的。这部分工作得很好

我想根据按下的JButton将产品p传递给EditProduct类或DeleteRecord类,这样用户就可以编辑同一产品的名称、数量或成本。下面是我的DisplayRecord、EditProduct和DeleteRecord类。我不知道该怎么办

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

public class DisplayRecord extends JFrame implements ActionListener {

    final private StockList stocks;
    final private ArrayList<Product> list;
    JFrame showWindow;

    private JPanel top, bot;
    private JPanel barcodePanel1 = new JPanel();
    private JPanel barcodePanel2 = new JPanel();
    private JPanel namePanel1 = new JPanel();
    private JPanel namePanel2 = new JPanel();
    private JPanel descPanel1 = new JPanel();
    private JPanel descPanel2 = new JPanel();
    private JPanel compPanel1 = new JPanel();
    private JPanel compPanel2 = new JPanel();
    private JPanel ratingPanel1 = new JPanel();
    private JPanel ratingPanel2 = new JPanel();
    private JPanel costPanel1 = new JPanel();
    private JPanel costPanel2 = new JPanel();
    private JPanel quantityPanel1 = new JPanel();
    private JPanel quantityPanel2 = new JPanel();
    private JLabel barcodeLabel = new JLabel();
    private JLabel nameLabel = new JLabel();
    private JLabel descLabel = new JLabel();
    private JLabel compLabel = new JLabel();
    private JLabel ratingLabel = new JLabel();
    private JLabel costLabel = new JLabel();
    private JLabel quantityLabel = new JLabel();
    private GridLayout displayLayout;
    JButton edit = new JButton("Edit");
    JButton backToMenu = new JButton("Back to Menu");
    JButton delete = new JButton("Delete");

    public DisplayRecord() {
        stocks = new StockList();
        list = stocks.getList();
        try {
            stocks.load();
        } catch (IOException ex) {
            System.out.println("Cannot load file");
        }
    }

    public void displayRecord(Product p) {
        this.setTitle("Displaying one record");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(500, 350));

        top = new JPanel();
        displayLayout = new GridLayout(7, 2, 2, 2);
        top.setLayout(displayLayout);
        top.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 5));

        bot = new JPanel();
        bot.setLayout(new BoxLayout(bot, BoxLayout.LINE_AXIS));
        bot.add(Box.createHorizontalGlue());
        bot.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));

        barcodeLabel.setText("Barcode:  ");
        nameLabel.setText("Name:  ");
        descLabel.setText("Description:  ");
        compLabel.setText("Developer:  ");
        ratingLabel.setText("EU Rating:  ");
        costLabel.setText("Cost:  ");
        quantityLabel.setText("Quantity in Stock:  ");

        JLabel barcodeField = new JLabel(Long.toString(p.getBarcode()));
        JLabel nameField = new JLabel(p.getName());
        JLabel descField = new JLabel(p.getDesc());
        JLabel compField = new JLabel(p.getCompany());
        JLabel ratingField = new JLabel(p.getRating());
        JLabel costField = new JLabel(Double.toString(p.getCost()));
        JLabel quantityField = new JLabel(Integer.toString(p.getQuantity()));

        barcodePanel1.add(barcodeLabel);
        barcodePanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        barcodePanel2.add(barcodeField);                      barcodePanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        namePanel1.add(nameLabel);
        namePanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        namePanel2.add(nameField);
        namePanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        descPanel1.add(descLabel);
        descPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        descPanel2.add(descField);
        descPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        compPanel1.add(compLabel);
        compPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        compPanel2.add(compField);
        compPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        ratingPanel1.add(ratingLabel);
        ratingPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        ratingPanel2.add(ratingField);
        ratingPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        costPanel1.add(costLabel);
        costPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        costPanel2.add(costField);
        costPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
        quantityPanel1.add(quantityLabel);
        quantityPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
        quantityPanel2.add(quantityField);
        quantityPanel2.setBorder(BorderFactory.createLineBorder(Color.black));

        top.add(barcodePanel1);
        top.add(barcodePanel2);
        top.add(namePanel1);
        top.add(namePanel2);
        top.add(descPanel1);
        top.add(descPanel2);
        top.add(compPanel1);
        top.add(compPanel2);
        top.add(ratingPanel1);
        top.add(ratingPanel2);
        top.add(costPanel1);
        top.add(costPanel2);
        top.add(quantityPanel1);
        top.add(quantityPanel2);

        edit.addActionListener(this);
        delete.addActionListener(this);
        backToMenu.addActionListener(this);

        bot.add(edit);
        bot.add(Box.createRigidArea(new Dimension(10, 0)));
        bot.add(delete);
        bot.add(Box.createRigidArea(new Dimension(10, 0)));
        bot.add(backToMenu);

        this.add(top);
        this.add(bot, BorderLayout.SOUTH);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) { // here is where I'd LIKE to pass Product p as parameter but obviously that's not a thing
        if (e.getSource() == edit) {
//            EditProduct ed = new EditProduct(); <- hypothetical!
//            ed.editProduct(p);
        } else if (e.getSource() == delete) {
//            DeleteRecord del = new DeleteRecord(); <- hypothetical!
//            del.deleteRecord(p);
        } else if (e.getSource() == backToMenu) {
            new CreateDisplay();
            this.dispose();
        }
    }
}
我的编辑产品类别:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class EditProduct extends JFrame implements FocusListener, ActionListener {

    final private StockList stocks;
    final private ArrayList<Product> list;
    JPanel top, bot;
    JLabel nameLabel, costLabel, quantityLabel = new JLabel();
    JTextField nameField, costField, quantityField = new JTextField();
    JButton save, quit = new JButton();
    private GridLayout topLayout;

    public EditProduct() {
        stocks = new StockList();
        list = stocks.getList();
    }

    public void editProduct(Product p) {
        this.setTitle("Editing a Product");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setPreferredSize(new Dimension(500, 250));

        top = new JPanel();
        topLayout = new GridLayout(3, 2, 5, 5);
        top.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 5));
        top.setLayout(topLayout);

        bot = new JPanel();
        bot.setLayout(new BoxLayout(bot, BoxLayout.LINE_AXIS));
        bot.add(Box.createHorizontalGlue());
        bot.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));

        nameLabel.setText("Name:  ");
        costLabel.setText("Cost:  ");
        quantityLabel.setText("Quantity:  ");
        top.add(nameLabel);
        top.add(costLabel);
        top.add(quantityLabel);

        nameField = new JTextField(p.getName());
        costField = new JTextField(String.valueOf(p.getCost()));
        quantityField = new JTextField(p.getQuantity()); 

        nameField.addFocusListener(this);
        costField.addFocusListener(this);
        quantityField.addFocusListener(this);

        save.setText("Save");
        save.addActionListener(this);
        quit.setText("Quit");
        quit.addActionListener(this);

        bot.add(save);
        bot.add(Box.createRigidArea(new Dimension(10, 0)));
        bot.add(quit);

        this.add(top);
        this.add(bot, BorderLayout.SOUTH);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    @Override
    public void focusGained(FocusEvent e) {
        if (e.getSource() == nameField) {
            nameField.setText("");
        } else if (e.getSource() == costField) {
            costField.setText("");
        } else if (e.getSource() == quantityField) {
            quantityField.setText("");
        }
    }

    @Override
    public void focusLost(FocusEvent fe) {
        //do nothing
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == save) {
            String newName = nameField.getText();
            double newCost = Double.parseDouble(costField.getText());
            int newQty = Integer.parseInt(quantityField.getText());
            stocks.editProduct(newName, newCost, newQty);
            this.dispose();
            JOptionPane.showMessageDialog(null, "Changes have been saved!", "Saved!", JOptionPane.PLAIN_MESSAGE);
        } else if (e.getSource() == quit) {

        }
    }
}
aa和DeleteRecord类:

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class DeleteRecord {

    private StockList stocks;
    private ArrayList<Product> list;

    public DeleteRecord() {
        stocks = new StockList();
        list = stocks.getList();
    }

    public DeleteRecord(Product p) {
        String title = "Are you sure you want to delete " + p.getName() + "?";
        if (JOptionPane.showConfirmDialog(null, title, "Deleting...", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            stocks.deleteRecord(p);
        } else {
            new CreateDisplay();
        }
    }
}

我为巨大的帖子和文本墙感到抱歉,但我真的不知道我的问题在哪里,也不知道如何解决这个问题,我也是StackOverflow的新手。有人能帮我吗?

在我看来,DisplayRecord一次只能显示一种产品。如果确实如此,您可以将该产品存储在一个字段中,然后从actionPerfomed访问它。

我也是StackOverflow的新手:欢迎使用StackOverflow!☺@保尔瓦加斯谢谢你!我仍在努力理解,但我喜欢这个网站:它成功了,谢谢!“DisplayRecord”现在正在到达“EditProduc”类,但它现在正在“nameField.setTextp.getName;”上抛出一个“NullPointerException”。我不知道它是否没有正确地接收“产品p”参数,或者是什么nameField本身很好,我用一个直接向上的字符串而不是方法调用对它进行了测试,所以问题出在getName部分;只设置了最后一个字段,其他字段保持为空。我真是个白痴。。。甚至没有注意到我的错误。我解决了这个问题,在其他地方我也犯了同样的错误,但问题仍然存在。产品一开始没有被传递,还是.getName不起作用?如果您得到一个NullPointerException,这意味着调用方法的对象为null。所以如果nameField不再为null,那么p必须为;这使得它比其他任何东西更符合我自己的理解tbh,请原谅我的新鲜感,但显然它指的是一种新产品,而不是我试图传递给该方法的产品。好消息是,我的代码本身没有缺陷,但我不知道如何“利用”,也就是说,产品p来调用它的方法。同样的事情也适用于DisplayRecord ie,将Product作为参数传递并调用.getName等,那么为什么不在EditProduct上呢?