简体   繁体   English

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

[英]How to pass an object argument to a method called in actionPerformed?

I am writing a stock control system program for a school project. 我正在为一个学校项目编写一个库存控制系统程序。 This is the last thing I need to do, but seeing as I am a relative Java noob, I kindly request your assistance. 这是我需要做的最后一件事,但是由于我是一个相对较弱的Java新手,所以请您提供帮助。

I have a DisplayRecord class, which is created by taking String input from a "search" JTextField in the Search class, finding the Object (Product p) it's linked to, and passing it to the displayRecord method. 我有一个DisplayRecord类,该类是通过从Search类的“搜索” JTextField中获取String输入,找到其链接到的对象(产品p)并将其传递给displayRecord方法而创建的。 This part works perfectly. 这部分工作完美。

I want to take that Product p and pass it to the EditProduct class or the DeleteRecord class (depending on the JButton pressed) so the user can then edit the Name, Quantity or Cost of that same Product. 我想使用该Product p并将其传递给EditProduct类或DeleteRecord类(取决于所按下的JButton),以便用户然后可以编辑同一产品的名称,数量或成本。 Here are my DisplayRecord, EditProduct and DeleteRecord classes. 这是我的DisplayRecord,EditProduct和DeleteRecord类。 I have no idea what to do. 我不知道该怎么做。

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();
        }
    }
}

My EditProduct class: 我的EditProduct类:

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) {

        }
    }
}

Aaand the DeleteRecord class: 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();
        }
    }
}

I'm sorry for the massive post and text wall but I honestly have no idea where my problem is or how to work around this problem (and I'm also new to StackOverflow). 对于大量的帖子和文本墙,我感到抱歉,但是老实说,我不知道我的问题在哪里或如何解决此问题(而且我对StackOverflow还是陌生的)。 Can anyone help me? 谁能帮我?

It seems to me that DisplayRecord can only display one Product at a time. 在我看来, DisplayRecord一次只能显示一个Product If that is indeed the case, you can store that Product in a field and then access it from actionPerfomed() . 如果确实如此,则可以将该Product存储在字段中,然后从actionPerfomed()访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActionListener-跟踪是否在actionPerformed方法内部调用actionPerformed? - ActionListener - Tracing if actionPerformed is called inside actionPerformed method? 如果没有显式调用'actionPerformed'方法怎么调用呢? - How does the 'actionPerformed' method get called without an explicit call to it? Spring AOP:如何将参数从被调用方法传递给建议方法? - Spring AOP: How to pass argument from called method to advice method? 如何从ActionListener类的actionPerformed()方法在框架上绘制对象 - How to draw an object on a Frame from an actionPerformed() method of ActionListener class 如何在Java中将调用方法的对象作为该方法的参数传递? - How to pass an object that is calling a method as an argument to the method in Java? 如何为 actionPerformed 方法暂停程序 - How to pause a program for the actionPerformed method 如何在动作执行中调用图形方法? - how to call a graphical method in actionperformed? actionPerformed中如何调用paintScreen方法? - How to call in the paintScreen method in actionPerformed? 使用在Jlist中的Jbutton ActionPerformed方法中创建的对象 - Using object created in Jbutton ActionPerformed method in Jlist 如何传递指向某个对象的指针作为方法的参数? - How can I pass a pointer to some object as argument to a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM