如何在while循环中引用按钮侦听器?

时间:2015-01-02 10:54:01

标签: java swing while-loop actionlistener onclicklistener

在我的Java代码中,我有一个while循环,如下所示。我想在达到文件结尾或按下GUI中的暂停按钮时,while循环退出。如何将按钮监听器从GUI链接到while循环?

注意:在下面的代码中,this.isPausePressed是暂停按钮监听器内的一个标志,如果按下暂停按钮则为true,否则为

while ( ((line = this.logFileBuffer.readLine()) != null) && (!this.isPausePressed) ) {
    statusarea.append(Log.d(TAG, "parseTimeStamp", "isPauseButtonPressed: " + this.isPausePressed) + "\n");
    String timeStamp = line.split(this.VALUE_SEPARATOR)[0];
    Thread timeStampThread = new Thread(new TimeStampTimerThread(++this.markCounter, timeStamp), "TimeStampThread_" + this.markCounter);
    timeStampThread.start();
    this.setLastFileMarker(this.markCounter);
}

Update_1

我创建了三个线程,每个线程使用actionListener(播放,暂停和停止)控制其中一个GUI按钮。在每个相应的buttonListener中,我设置了相应的标志开/关,但问题仍然存在。

请参阅下面发布的更新代码。问题是当我点击暂停时,onPause()方法中的调试语句总是显示getLastFileMarker()方法达到了它的最大值。这意味着,当点击暂停按钮并将其isPausePressed设置为true时,while-loop不会退出。

注意:setLastFileMarker()在ParseTimeStamp()方法的wile-loop中设置。所有这些方法都已发布。好心地嘲笑它

createGUI {
this.playButtonThread = new Thread(this.playButtonRunnable, "playButtonThread");
    this.pauseButtonThread = new Thread(this.pauseButtonRunnable, "pauseButtonThread");
    this.stopButtonThread = new Thread(this.stopButtonRunnable, "stopButtonThread");

    this.playButtonThread.start();
    this.pauseButtonThread.start();
    this.stopButtonThread.start();
  }//CreateGui

 Runnable playButtonRunnable = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        playButtonListener();
    }
};

Runnable pauseButtonRunnable = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        pauseButtonListener();
    }
};

Runnable stopButtonRunnable = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        stopButtonListener();
    }
};

protected void playButtonListener() {
    // TODO Auto-generated method stub
    Bplay.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            if (isPausePressed) {
                isPlayPressed = true;
                isPausePressed = false;
                continueReading();
            }else if (!isPlayPressed) {
                isPlayPressed = true;
                try {
                    onPlay();
                } catch (IOException
                        | InterruptedException | MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
}

protected void pauseButtonListener() {
    // TODO Auto-generated method stub
    Bpause.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            if (isPlayPressed) {
                isPlayPressed = false;
                isPausePressed = true;
                try {
                    onPause();
                } catch (IOException | MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
}

protected void stopButtonListener() {
    // TODO Auto-generated method stub
    Bstop.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            if (isPlayPressed) {
                isStopPressed = true;
                try {
                    onStop();
                } catch (MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
}

    protected void continueReading() {
    // TODO Auto-generated method stub

}

  private void onPause() throws IOException, MqttException {
    // TODO Auto-generated method stub
    statusarea.append(Log.w(TAG, "onPause", "onPause() is called") + "\n");

    if (!this.logFileBuffer.markSupported()) {
        statusarea.append(Log.w(TAG, "onPause", "stream does not support .mark() and .reset()   
    operations.") + "\n");
    }else {
        this.logFileBuffer.mark(this.getLastFileMarker());
        statusarea.append(Log.i(TAG, "onPause", "last mark set in line#: " + 
  this.getLastFileMarker()) + "\n");
        this.timer.cancel();
        //this.getClient().disconnect();
    }
    simulationarea.append(Log.w(TAG, "onPause", "PAUSE") + "\n");
}

ParseTimeStamp方法

 while ( ((line = this.logFileBuffer.readLine()) != null) && (!isPausePressed) ) {
                statusarea.append(Log.d(TAG, "parseTimeStamp", "isPauseButtonPressed: " +   
 isPausePressed) + "\n");
                String timeStamp = line.split(this.VALUE_SEPARATOR)[0];
                Thread timeStampThread = new Thread(new TimeStampTimerThread(++this.markCounter,   
 timeStamp), "TimeStampThread_" + this.markCounter);
                timeStampThread.start();
                this.setLastFileMarker(this.markCounter);
            }

Update_2

我删除了三个线程按照建议控制GUI按钮。并刚刚实现了他们的听众,如下所示。上面显示在update_1中的onPlay()方法会调用parseTimeStamp(),其中包含'while-loop',也会在update_1上面发布。 在运行时,单击pause按钮时while-loop不会退出。

代码

Bplay.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            if (isPausePressed) {
                isPlayPressed = true;
                isPausePressed = false;
                continueReading();
            }else if (!isPlayPressed) {
                isPlayPressed = true;
                try {
                    onPlay();
                } catch (IOException
                        | InterruptedException | MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

    Bpause.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            if (isPlayPressed) {
                isPlayPressed = false;
                isPausePressed = true;
                try {
                    onPause();
                } catch (IOException | MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

    Bstop.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            if (isPlayPressed) {
                isStopPressed = true;
                try {
                    onStop();
                } catch (MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

1 个答案:

答案 0 :(得分:2)

你只需要在循环开始之前的某个地方设置一次监听器。

当您单击该按钮时,按钮的单击侦听器将异步触发(即从另一个线程作为您的循环代码,例如event dispatch thread)。因此,循环将突然“看到”isPausePressed的更改值并退出,就像那样。

这意味着你不应该在“事件调度线程”中执行这些循环,因为这会阻止循环和事件并行发生。有关“事件调度线程”的更多信息以及Swing中的并发性,请参阅this文档(和子文档)。

更新

有关示例,请参见下文。请注意,不需要任何线程创建或其他奇怪的东西。 Swing会自动为您启动臭名昭着的事件调度线程,所有与事件相关的代码将自动在该线程内运行。这样,您的主循环代码将很乐意与actionListener的actionPerformed方法并行运行(只要您单击按钮)。因此,在{/ em>执行While循环期间,isPausePressed标志将在看似的情况下发生变化。这真的很简单。

    private boolean isPausePressed = false;

    JButton b = new JButton("Pause!");
    b.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            isPausePressed = true;
        }
    });

    // ....

    while (!isPausePressed)
    {
        // do some work
    }