java - 获取摆动标签文本并将其传递给父类
问题描述
我有一个带有摆动 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
frompayButton.getActionListeners()
然后调用我声明的函数时,java 无法识别该getPaidAmount()
函数。
我的问题是,我如何获得它paidAmount
并将其从按钮传输到面板,然后从面板传输到框架,从框架传输到拥有框架的类?
解决方案
尝试将函数放在 actionListener 之外和类中PayPanel
public double getPaidAmount() {
return this.paidAmount;
}
并使用payPanelObject.getPaidAmount()
推荐阅读
- python - 从新闻网站beautifulsoup python的抓取链接抓取新闻文章
- alexa - 在我的网站上部署/参考 Alexa Skills 的好方法是什么
- python-3.x - 如何在 dask 中的 sqlalchmey 中应用多个 whereclause,同时从 teradata 获取大型数据集
- c++ - 我的以 2 为底数、n 为指数并计算答案模 10^9+7 的幂函数有什么问题?
- html - 上传到flutter-web后如何获取文件名(例如hello.pdf”)
- sql - 循环遍历多个记录的函数,如果不满足日期格式,则将该行存储在表中
- javascript - 表单 react-bootstrap 中的数据和时间
- python - python vscode ImportError:没有名为simplejson的模块
- python - 了解在文本分类中使用什么 keras 和 TensorFlow
- git - 为什么 GitLab 合并分支的 git 日志图倒置了?