如何将对象的一部分传递给另一个类

时间:2013-11-21 23:28:35

标签: java

我试图将属于某个对象的变量传递给另一个类,以便我可以使用它们。我无法弄清楚该怎么做。请帮忙!

这是对象

package appgui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainFrame extends JFrame {
    private BankAccount account;
    private JLabel label;
    private JTextField amountField;

    private static final int FRAME_HEIGHT = 600;
    private static final int FRAME_WIDTH = 400;

    public MainFrame(String t, BankAccount anAccount) {
        super(t);

        account = anAccount;

        // Label that displays the result
        label = new JLabel("Balance: " + account.getBalance());
        label.setFont(new Font("Arial", Font.BOLD, 22));
        label.setForeground(Color.RED);

        // Create the label, text field, and button for entering amount
        JLabel amountLabel = new JLabel("Amount: ");
        amountField = new JTextField(7);

        // Create the Control Panel that holds all components
        JPanel controlPanel = new JPanel();
        controlPanel.add(amountLabel);
        controlPanel.add(amountField);
        controlPanel.add(createWithdrawButton());
        controlPanel.add(createDepositeButton());
        controlPanel.add(label);

        add(controlPanel);
        setSize(FRAME_HEIGHT, FRAME_WIDTH);
        //setLayout(new GridBagLayout());
    }

    private JButton createDepositeButton() {
        JButton depositButton = new JButton("Deposit");

        ActionListener listener1 = new DepositListener();
        depositButton.addActionListener(listener1);
        return depositButton;
    }

这是我试图将上面的一些变量传递到的类,因为我需要它们的值,如amountFieldaccountlabel

package appgui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

class DepositListener implements ActionListener {

    public void actionPerformed(ActionEvent event) {

        double depositAmount = Double.parseDouble(amountField.getText());

        account.deposit(depositAmount);
        label.setText("Balance: " + account.getBalance());
    }
}

4 个答案:

答案 0 :(得分:1)

你不想得到“对象的一部分”,而是得到对象本身,并通过getter方法查询它的状态,并通过setter方法设置它的状态。

您的侦听器需要对调用它的GUI的引用。我认为最干净的是在构造函数参数中传递它:

class DepositListener implements ActionListener {
    private MainFrame mainFrame;

    public DepositListener(MainFrame mainFrame) {
       this.mainFrame = mainFrame;
    }

    public void actionPerformed(ActionEvent event) {

        // you'll need to give MainFrame public methods to extract information, e.g.,
        double depositAmount = Double.parseDouble(mainFrame.getAmount());
        // ... etccc

        // and to display information
        mainFrame.setBalanceText(....);

    }
}

对波希米亚人的答案(1+)进行重复,是的,使用接口,但我不喜欢获取实际组件,而只是获取必要的信息。只暴露需要暴露的东西:

public interface AccountDisplay {
    double getAmount();
    BankAccount getAccount(); // this is OK if BankAccount is non GUI
    void setBalanceText(String text);
}

答案 1 :(得分:1)

那么,内心阶层怎么样:

public class MainFrame extends JFrame {
    ....
    class DepositListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            double depositAmount = Double.parseDouble(amountField.getText());

            account.deposit(depositAmount);
            label.setText("Balance: " + account.getBalance());
        }
    }
}

这将有效。

答案 2 :(得分:1)

为其他类感兴趣的字段创建一个接口,让对象的类实现它,并让另一个类的方法期望接口的实例:

public interface AccountDisplay {
    JLabel getAmount();
    BankAccount getAccount();
    JTextField getLabel();
}

public class MainFrame extends JFrame implements AccountDisplay {
    // implement the getters of the interface to return the appropriate fields
}

class DepositListener implements ActionListener {
    private AccountDisplay accountDisplay;

    public DepositListener(AccountDisplay accountDisplay) {
        this.accountDisplay = accountDisplay;
    }

    public void actionPerformed(ActionEvent event) {

        double depositAmount = Double.parseDouble(accountDisplay.getAmount().getText());

        accountDisplay.getAccount().setBalanceText(....);

    }
}

答案 3 :(得分:0)

只需为您尝试访问的变量创建getter,并让第二个类保存对第一个类的对象引用