posts - 195,  comments - 30,  trackbacks - 0
基本的需求是: 按下Btn Begin, 开始处理,同时监听Btn Pause,一旦Pause就Pause,如果没有Pause就执行直到结束
我们需要将事件响应程序写成Thread,实现异步监听。
在嵌入类中调用this,需要类名+this.
代码1:响应部分
package firstversion;

public class ThreadForUpdate extends Thread{
    ControlPanel cp;
    Simulator r;
    
    
public ThreadForUpdate(ControlPanel cp,Simulator r)
    {
        
this.cp=cp;
        
this.r=r;
    }
    
    
public void run()
    {
        
while(cp.flag==true)
     {
        r.DisplaySolutionRecursive();
        cp.textFieldStability.setText(r.node.stabilityState.stability.toString());
        cp.textFieldDirection.setText(r.node.controlledState.direction.toString());
        cp.textFieldDifficulty.setText(r.node.controlledState.difficulty.toString());
        cp.textFieldActionExecuting.setText(
"("+r.node.stabilityState.actor+","+r.node.stabilityState.actionBeExecuting+")");
        cp.textFieldStability.setText(r.node.stabilityState.stability.toString());
        cp.textFieldX.setText(
""+r.node.controlledState.x);
        cp.textFieldY.setText(
""+r.node.controlledState.y);
        cp.textFieldTraffic.setText(r.node.traffic.toString());
        
if(r.node.isTerminal==true)
            cp.textFieldNextAction.setText(
"");
        
else
            cp.textFieldNextAction.setText(r.node.bestAction.toString());
        cp.textFieldEngagement.setText(r.node.engagement.toString());
        
        
try {
            Thread.sleep(
200);
        } 
catch (InterruptedException e) {
            e.printStackTrace();
        }
      }
    }

}
代码2:事件响应
btnBegin = new JButton("Begin");
        btnBegin.setEnabled(
false);
        
final ThreadForUpdate threadUpdate=new ThreadForUpdate(ControlPanel.this,r);;
        btnBegin.addActionListener(
new ActionListener() {

            
public void actionPerformed(ActionEvent arg0) {
                threadUpdate.start();
                }            
        });





posted on 2012-04-04 04:36 luis 阅读(511) 评论(0)  编辑 收藏 引用 所属分类: Java笔记
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜