2014-04-14 35 views
0

代码:的Java GUI按钮unpressable

public void actionPerformed(ActionEvent evt) 

    { 
....... 


while(true) 
{ 
if (source==button1) 

        { 
        //button1 code      
        } 


if(source==button2) 

        { 
        // button2 code 
        } 
} //while true 


..... 
    //some other code that I don't want it to execute until while breaks 
}//action performed 

所以问题是执行上面的代码时,GUI被卡住,没有按钮可以被按下。如果我删除了无限循环,其余的代码将被执行并停止我的算法。有没有解决这个问题的方法?

谢谢!

回答

1

while循环是一个严重的编程错误。让你的程序在Event Dispatcher Thread中执行循环时,它无法发送任何其他事件(其他按钮按下)。

你必须消除循环。这样做的actionPerformed方法将运行在每个按钮单击(给你按钮有actionListeners ...)。