JAVA—尝试从类外部执行的操作内部使用变量
我试图访问actionPerformed之外的(双重)百分比变量,同时保留它所经历的更改。 这是一个下拉菜单,以及您按下的“确定”按钮。一旦你按下它,它会计算一个百分比值,然后我想在以后的程序中使用它 以下是代码片段:JAVA—尝试从类外部执行的操作内部使用变量,java,variables,actionlistener,Java,Variables,Actionlistener,我试图访问actionPerformed之外的(双重)百分比变量,同时保留它所经历的更改。 这是一个下拉菜单,以及您按下的“确定”按钮。一旦你按下它,它会计算一个百分比值,然后我想在以后的程序中使用它 以下是代码片段: btn.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ String currentCountry = (String)cb.getSele
btn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String currentCountry = (String)cb.getSelectedItem();
double percentage = 0.00;
if(currentCountry.equals("Brazil") || currentCountry.equals("Argentina")) {
cb2.removeAllItems();
for(int i = 0; i < choicesSouthAmerica.length; i++) {
cb2.addItem(choicesSouthAmerica[i]);
}
}
else {
cb2.removeAllItems();
for(int i = 0; i < choicesEurope.length; i++) {
cb2.addItem(choicesEurope[i]);
}
}
btn.setEnabled(false);
btn2.setEnabled(true);
if(currentCountry.equals("Brazil") || currentCountry.equals("Argentina")){
percentage = 1/5;
System.out.println(percentage);
}
else{
percentage = 1/8;
System.out.println(percentage);
}
}
}
);
btn.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件e){
字符串currentCountry=(字符串)cb.getSelectedItem();
双倍百分比=0.00;
if(currentCountry.equals(“巴西”)| currentCountry.equals(“阿根廷”)){
cb2.removeAllItems();
for(int i=0;i
非常感谢您可以按如下方式使用putClientProperty(对象,对象)和getClientProperty(对象)函数:
JButton btn = new JButton("Ok");
btn.putClientProperty("percentage",1.0);//or whatever initial value
btn.addActionListener(arg0 -> {
JButton source = (JButton) arg0.getSource();
double per = (double)source.getClientProperty("percentage");
per = (double)10/8;
source.putClientProperty("percentage",per);
});
double percentage = (double)btn.getClientProperty("percentage");//or use it in any other object that has access to the btn object
可以按如下方式使用putClientProperty(对象,对象)和getClientProperty(对象)函数:
JButton btn = new JButton("Ok");
btn.putClientProperty("percentage",1.0);//or whatever initial value
btn.addActionListener(arg0 -> {
JButton source = (JButton) arg0.getSource();
double per = (double)source.getClientProperty("percentage");
per = (double)10/8;
source.putClientProperty("percentage",per);
});
double percentage = (double)btn.getClientProperty("percentage");//or use it in any other object that has access to the btn object
我认为您实际上需要的只是一个静态字段(它可以有您想要的任何访问修饰符)。因此,我认为这样做应该有效:
public class Test {
static double d = 0;
public static void main(String[] args) {
JButton b = new JButton("ASDF");
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
d = 5;
}
});
}
}
我认为您实际上需要的只是一个静态字段(它可以有您想要的任何访问修饰符)。因此,我认为这样做应该有效:
public class Test {
static double d = 0;
public static void main(String[] args) {
JButton b = new JButton("ASDF");
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
d = 5;
}
});
}
}
遗憾的是,Java不支持闭包,因此不能修改匿名类范围之外的变量。但您可以访问最终变量,因此原则上您可以这样做:
class Percentage {
double p;
}
final Percentage p = new Percentage();
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// [...]
p.p = 1/5;
// [...]
}
}
);
然后,您可以在匿名类之外通过p.p
访问更新的百分比。(顺便问一下,这真的是一个“百分比”还是一个实际比率?)
但是这对于Java来说似乎不是很习惯,所以干净的解决方案可能只是用一个私有实例变量和一个getter创建一个合适的类,并使用它来代替匿名类。遗憾的是,Java不支持闭包,所以不能修改匿名类范围之外的变量。但您可以访问最终变量,因此原则上您可以这样做:
class Percentage {
double p;
}
final Percentage p = new Percentage();
btn.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// [...]
p.p = 1/5;
// [...]
}
}
);
然后,您可以在匿名类之外通过p.p
访问更新的百分比。(顺便问一下,这真的是一个“百分比”还是一个实际比率?)
但是对于Java来说,这似乎不是很惯用,因此干净的解决方案可能只是使用私有实例变量和getter创建一个适当的类,并使用它来代替匿名类。发布一个完整的、最小的示例,适当缩进,显示问题所在。我不知道为什么缩进结果是这样的,我尝试粘贴它,并使用ctrl-k缩进,但结果是这样的。我只是想知道如何在这个方法之外使用双百分比变量。我想知道在您发布的代码中存在多少个方法,以及变量在哪里声明,但由于代码没有正确缩进,所以无法理解。它是在方法内部声明的,所以您不能。但是您可以在方法中使用在外部声明的变量。即使我在方法外部声明它,一旦在方法内部对它进行了更改,我也无法访问方法外部的更改。发布一个完整的最小示例,正确缩进,显示问题。我不知道为什么缩进结果是这样的,我尝试粘贴它,并使用ctrl-k缩进,但结果是这样的。我只是想知道如何在这个方法之外使用双百分比变量。我想知道在您发布的代码中存在多少个方法,以及变量在哪里声明,但由于代码没有正确缩进,所以无法理解。它是在方法内部声明的,所以您不能。但是您可以在方法中使用在外部声明的变量。即使我在方法外部声明它,一旦在方法内部对其进行了更改,我无法访问该方法之外的更改。@NewbieWincover如果您喜欢我的答案,请不要忘记将其标记为已接受。@NewbieWincover如果您喜欢我的答案,请不要忘记将其标记为已接受。