我有一個(gè)帶有 swing gui 的程序來訂購某些產(chǎn)品。處理訂單的類有一個(gè),JFrame有JPanel一個(gè)JButton。按下該按鈕時(shí),我需要處理訂單的類中的輸入。但是無法弄清楚如何一直獲得該輸入。包含按鈕的面板: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從付款按鈕中獲取,然后調(diào)用該getPaidAmount()函數(shù)。但是當(dāng)我ActionListener從中獲取payButton.getActionListeners()然后調(diào)用我聲明的函數(shù)時(shí),java 無法識(shí)別該getPaidAmount()函數(shù)。我的問題是,我如何獲取它paidAmount并將其從按鈕傳輸?shù)矫姘?,然后從面板傳輸?shù)娇蚣?,再從框架傳輸?shù)綋碛锌蚣艿念悾?
1 回答

元芳怎么了
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
嘗試將函數(shù)放在 actionListener 之外并放在類中PayPanel
public double getPaidAmount() { return this.paidAmount; }
并使用payPanelObject.getPaidAmount()
添加回答
舉報(bào)
0/150
提交
取消