在 Java Swing 应用程序中保留对象值
我有一个有两个按钮的 Swing UI:1. 将数据库值加载到 Hashmap 2. 将输入值与数据库值进行比较。 以前,这两个操作都是使用相同的按钮完成的,因此哈希图被填充并且输入被正确比较。但现在却不是这样了。 调试后,我发现哈希图值为空,因为这些值在第二次操作期间丢失了。
如何解决这个问题。这样,当我单击第二个按钮时,哈希映射值仍然存在。
代码示例: Button1:加载-
LoadMaps = new JButton( new AbstractAction("LoadMaps") {
@Override
public void actionPerformed( ActionEvent e ) {
DRGCalc t = new DRGCalc();
t.loadHashMaps();
}
});
Button2:计算-
public void actionPerformed(ActionEvent evt) {
DRGCalc d = new DRGCalc();
int i = d.calculateDRG(Codes);
}
由于 t 和 d 是两个独立的对象,因此当我单击 d 时,t 中的值不会持续存在。为此,我在 calulateDRG 调用中包含了按钮 1 中使用的方法,它工作正常,但我希望两者是分开的。如何做到这一点? 谢谢
I have a swing UI having two buttons : 1. Loading database values to Hashmap 2. Comparing input values to database values.
Previously both the operations were done using the same button so the Hashmap was getting populated and the inputs were compared correctly. But now its not so.
After debugging I came to know that the hashmap values are null as the values are lost during the second operation.
How to approach this problem. So that the hash map values persist while i click the second button.
Code Example:
Button1: Loading-
LoadMaps = new JButton( new AbstractAction("LoadMaps") {
@Override
public void actionPerformed( ActionEvent e ) {
DRGCalc t = new DRGCalc();
t.loadHashMaps();
}
});
Button2: Calculation-
public void actionPerformed(ActionEvent evt) {
DRGCalc d = new DRGCalc();
int i = d.calculateDRG(Codes);
}
As t and d are two seperate objects so the values in t wont persist when I am clicking d. For this I have included the method used in button 1 inside the calulateDRG call and its working fine but I want both to be separate. How to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该对象仅存在于 ActionListener 中。如果您希望该对象可用于类中的其他 ActionListener 或其他方法,那么您需要将其设为类变量。因此,在您的类中您需要定义:
然后在 ActionListener 中将代码更改为:
This object only exist within the ActionListener. If you want the object to be available to other ActionListeners or other methods in your class then you need to make it a class variable. So in your class you need to define:
Then in the ActionListener you change the code to: