我正在尝试制作一个有三个按钮的秒表,"开始","暂停"one_answers"停止"。我的老师只教我们如何将动作监听器设置为两个按钮。如何将动作监听器设置为三个按钮?这是我到目前为止的代码
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 );
}
}
我还没有为我的"暂停"功能做任何动作监听器,因为我不知道如何暂停计时器。但是我想在我弄清楚如何暂停之前将动作链接到按钮
您要查找的是if-then-else if-then
语句。
基本上,将ActionListener
添加到所有三个按钮中,就像您一直在做的那样…
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
系列条件来测试每种可能发生的情况(您期望的)
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-else语句以了解更多细节
不要尝试使用对按钮的引用,您可以考虑使用AcionEvent
的actionCommand
属性,这意味着您将不需要能够引用原始按钮…
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
…
话虽如此,我还是希望大家不要遵循这种模式。通常情况下,我会鼓励您使用Action
API,但对于您目前所处的位置,这可能有点太高级了,相反,我会鼓励您利用Java的匿名类支持,例如....
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
,这使得更容易看到正在发生的事情,并且在需要时,修改它们而不必担心或影响其他按钮。
它也不需要维护对按钮的引用(因为它可以通过ActionEvent
getSource
属性获得)
如果你不想实现ActionListener,你可以添加匿名监听器到你的按钮,像这样:
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
JButton pauseButton = new JButton("Pause");
startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//start action logic here
}
});
stopButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//stop action logic here
}
});
pauseButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//action logic here
}
});
这个解决方案必须工作:)
所有你需要添加的是在按钮创建之后。
startButton.setActionCommand("Start");
stopButton.setActionCommand("Stop");
pauseButton.setActionCommand("Pause");
和在actionPerformed方法中使用this
switch(actionEvent.getActionCommand())
{
// cases
}