为了账号安全,请及时绑定邮箱和手机立即绑定

获取秋千标签文本并将其传递给父类

获取秋千标签文本并将其传递给父类

拉莫斯之舞 2023-02-16 17:20:09
我有一个带有 swing gui 的程序来订购某些产品。处理订单的类有一个,JFrame有JPanel一个JButton。按下该按钮时,我需要处理订单的类中的输入。但是无法弄清楚如何一直获得该输入。包含按钮的面板:public class PayPanel extends JPanel {    private double paidAmount;    private JButton payButton;    public PayPanel() {        setBorder(BorderFactory.createTitledBorder("Make Payment"));        JLabel payLabel = new JLabel("Pay with: ");        JTextField payField = new JTextField(12);        this.payButton = new JButton("Pay");        this.payButton.setPreferredSize(new Dimension(100, 20));        this.payButton.addActionListener(new ActionListener() {            public double paidAmount;            public void actionPerformed(ActionEvent e) {                this.paidAmount = Double.parseDouble(payField.getText());            }            public double getPaidAmount() {                return this.paidAmount;            }        });        setLayout(new GridBagLayout());        GridBagConstraints gridBagConstraints = new GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 1;        add(payLabel, gridBagConstraints);        gridBagConstraints.gridx = 1;        gridBagConstraints.gridy = 1;        add(payField, gridBagConstraints);        gridBagConstraints.weighty = 10;        gridBagConstraints.gridx = 2;        gridBagConstraints.gridy = 1;        add(payButton, gridBagConstraints);    }    public double getPaidAmount() {        ActionListener[] payButtonListeners = this.payButton.getActionListeners();        ActionListener payButtonActionListener = payButtonListeners[0];        return payButtonActionListener.getPaidAmount(); // this function is not recognized even though i defined it in the action listener like shown above in the contructor.     }我paidAmount在ActionListener声明中添加了变量,这样我就可以ActionListener从付款按钮中获取,然后调用该getPaidAmount()函数。但是当我ActionListener从中获取payButton.getActionListeners()然后调用我声明的函数时,java 无法识别该getPaidAmount()函数。我的问题是,我如何获取它paidAmount并将其从按钮传输到面板,然后从面板传输到框架,再从框架传输到拥有框架的类?
查看完整描述

1 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

尝试将函数放在 actionListener 之外并放在类中PayPanel

    public double getPaidAmount() { 
               return this.paidAmount;
    }

并使用payPanelObject.getPaidAmount()


查看完整回答
反对 回复 2023-02-16
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信