如何将动作侦听器设置为3个按钮 [英] How to set Action Listener to 3 buttons
问题描述
我正在尝试制作带有三个按钮的秒表,分别是开始",暂停"和停止".我的老师只教我们如何将动作监听器设置为两个按钮.如何将动作侦听器"设置为三个按钮?到目前为止,这是我的编码
I'm trying to make a stopwatch with three buttons, "Start", "Pause", and "Stop". My instructor only taught us how to set Action Listeners to two buttons. How do I set up Action Listeners to three buttons? Here is my coding so far
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");
startButton.addActionListener(this);
stopButton.addActionListener(this);
public void actionPerformed(ActionEvent actionEvent) {
Calendar aCalendar = Calendar.getInstance();
if (actionEvent.getActionCommand().equals("Start")){
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
} else {
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
}
}
我尚未为暂停"功能设置任何动作监听器,因为无论如何我都不知道如何暂停计时器.但是我想先将动作链接到按钮,然后再弄清楚如何暂停.
I haven't made any Action Listeners for my "Pause" feature yet because I don't know how to pause the timer anyway. But I wanted to link the action to the button before I figured out how to pause.
推荐答案
您正在寻找的是if-then-else if-then
语句.
基本上,像往常一样将ActionListener
添加到所有三个按钮...
Basically, add the ActionListener
to all three buttons as you have been doing...
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");
startButton.addActionListener(this);
stopButton.addActionListener(this);
pauseButton.addActionListener(this);
然后提供一系列if-else-if
条件,以测试每种可能发生的情况(您期望的)
Then supply a if-else-if
series of conditions to test for each possible occurrence (you are expecting)
public void actionPerformed(ActionEvent e) {
Calendar aCalendar = Calendar.getInstance();
if (e.getSource() == startButton){
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
} else if (e.getSource() == stopButton) {
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
} else if (e.getSource() == pauseButton) {
// Do pause stuff
}
}
仔细研究 if-then和if-then-其他声明以获取更多详细信息
Take a closer look at The if-then and if-then-else Statements for more details
您可以考虑使用AcionEvent
的actionCommand
属性,而不是尝试使用按钮的引用,这意味着您将无需引用原始按钮... >
Instead of trying to use the reference to the buttons, you might consider using the actionCommand
property of the AcionEvent
instead, this means you won't need to be able to reference the original buttons...
public void actionPerformed(ActionEvent e) {
Calendar aCalendar = Calendar.getInstance();
if ("Start".equals(e.getActionCommand())){
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
} else if ("Stop".equals(e.getActionCommand())) {
aJLabel.setText("Elapsed time is: " +
(double) (aCalendar.getTimeInMillis() - start) / 1000 );
} else if ("Pause".equals(e.getActionCommand())) {
// Do pause stuff
}
}
这还意味着您可以将ActionListener
重用于JMenuItem
s之类的东西,只要它们具有相同的actionCommand
...
It also means that you could re-use the ActionListener
for things like JMenuItem
s, so long as they had the same actionCommand
...
现在,话虽如此,我鼓励您不要遵循这种范例.通常,我鼓励您使用Action
s API,但是对于您现在所处的位置,这可能有点太先进了,相反,我鼓励您利用
Now, having said that, I would encourage you not to follow this paradigm. Normally, I would encourage you to use the Action
s API, but that might be a little too advanced for where you're up to right now, instead, I would encourage you to take advantage of Java's anonymous class support, for example....
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start = aCalendar.getTimeInMillis();
aJLabel.setText("Stopwatch is running...");
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
aJLabel.setText("Elapsed time is: "
+ (double) (aCalendar.getTimeInMillis() - start) / 1000);
}
});
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Do pause stuff
}
});
这将每个按钮的职责隔离到一个单独的ActionListener
中,这使得更容易查看正在发生的事情以及在需要时进行修改,而不必担心或影响其他按钮.
This isolates the responsibility for each button to a single ActionListener
, which makes it easier to see what's going on and when required, modify them without fear or affecting the others.
它也不需要维护对按钮的引用(因为可以通过ActionEvent
getSource
属性获得)
It also does away with the need to maintain a reference to the button (as it can be obtained via the ActionEvent
getSource
property)
这篇关于如何将动作侦听器设置为3个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!