调用JButton actionperformed事件时返回一个值 [英] return a value when an JButton actionperformed event is called
问题描述
我对 JButton
动作事件有一些问题,我已经声明了一个全局变量( boolean tc1
)和一个 JButton t1
。当我按下JButton时,我需要将布尔变量的值更改为true。谁能帮我吗?我的代码就在这里。
I have some problem with JButton
action events, I have declared a global variable (boolean tc1
) and a JButton t1
. When I press the JButton I need to change the value of the boolean variable to 'true'. Can any one help me out? My code goes here.
class Tracker extends JPanel {
public static void main(String[] args) {
new Tracker();
}
public Tracker() {
JButton tr=new JButton("TRACKER APPLET");
JButton rf=new JButton("REFRESH");
boolean tc1=false,tc2=false,tc3=false,tc4=false;
JButton t1=new JButton(" ");
t1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tc1=true;
}
});
System.out.println(tc1);
//remaining part of the code...
// here i need to use the value of tc1 for further process..
}
}
谢谢。
推荐答案
tc1
必须是全局变量。
您可以在方法内定义的另一个类中使用局部变量,除非局部变量是 final
变量。
从中取出 tc1
的声明构造函数
到整个类的可见性
tc1
must be a global variable.
You can use a local variable in an another class defined inside the method, unless the local variable is a final
variable.
Take out the declaration of tc1
from the constructor
to the visibility of whole class
class Tracker extends JPanel {
boolean tc1=false,tc2=false,tc3=false,tc4=false;
public static void main(String[] args) {
new Tracker();
}
public Tracker() {
JButton tr=new JButton("TRACKER APPLET");
JButton rf=new JButton("REFRESH");
JButton t1=new JButton(" ");
t1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tc1=true;
}
});
System.out.println(tc1);
//remaining part of the code...
// here i need to use the value of tc1 for further process..
}
}
我也遇到过这个问题,幸运的是我找到了解决方案
I have also encountered this problem already and luckily I found the solution
这篇关于调用JButton actionperformed事件时返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!