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



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

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

代码

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

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

请参阅下面发布的更新代码。 问题是当我单击 Pause 时,onPause() 方法中的调试语句始终显示 getLastFileMarker() 方法达到了最大值。 这意味着,当单击暂停按钮并将其 flage 设置为 true isPausePressed时,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");
}

解析时间戳方法

 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 按钮的三个线程。 并且刚刚实现了他们的侦听器,如下所示。 onPlay()方法(如上update_1所示)调用具有"while-loop"的parseTimeStamp(),该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();
                }
            }
        }
    });

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

在您单击按钮的

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

这意味着您不应该从"事件调度线程"内部执行此类循环,因为这会阻止循环和事件并行发生。有关 Swing 中的"事件调度线程"和并发性的更多信息,请参阅此文档(和子文档)。

更新:

有关示例,请参见下文。请注意,不需要任何线程创建或其他奇怪的东西。Swing 会自动为您启动臭名昭著的事件调度线程,所有与事件相关的代码将自动在该线程内运行。这样,您的主循环代码将与 actionListener 的 actionPerformed 方法并行运行(只要您单击按钮)。因此,在执行 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
    }

最新更新