java 当 JButton actionperformed 事件被调用时返回一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14909401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return a value when an JButton actionperformed event is called
提问by ram
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.
我对JButton
动作事件有一些问题,我已经声明了一个全局变量 ( boolean tc1
) 和一个JButton t1
. 当我按下 JButton 时,我需要将布尔变量的值更改为“true”。谁能帮我吗?我的代码在这里。
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..
}
}
Thanks.
谢谢。
回答by Govind Balaji
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
tc1
必须是全局变量。
您可以在方法内部定义的另一个类中使用局部变量,除非局部变量是final
变量。
取出声明tc1
fromconstructor
到整个可见性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
我也遇到过这个问题,幸运的是我找到了解决方案
回答by vikingmaster
Define click handler:
定义点击处理程序:
public void onClick(Boolean tc1){
System.out.println(tc1) ;
//Write some logic here and use your updated variable
}
And then:
接着:
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 ;
onClick(tc1) ;
}
});
回答by Adam
You cannot change the value of a local method variable in an anonymous inner class (the action listener). You can however change an instance variable of the outer class... so you could just move tc1 to Tracker.
您不能在匿名内部类(动作侦听器)中更改局部方法变量的值。但是,您可以更改外部类的实例变量...因此您可以将 tc1 移动到 Tracker。
class Tracker extends JPanel {
public static void main(String[] args) {
new Tracker();
}
private boolean tc1; // <=== class level instead...
public Tracker() {
JButton t1 = new JButton(" ");
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tc1 = true;
}
});
}
}
回答by Bhushan
First of all you said that i have declared a global variable(boolean tc1)
, here tc1 is not a global variable, if you want a global variable then you must declare that variable as static
.
首先你说i have declared a global variable(boolean tc1)
,这里 tc1 不是一个全局变量,如果你想要一个全局变量,那么你必须将该变量声明为static
.
Then if you want to access that variable on button click event
then you can write following code:
然后,如果您想访问该变量,button click event
则可以编写以下代码:
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;
getValue();
}
});
}
public void getValue()
{
System.out.println("values is: "+ tc1);
}
}