如何将动作侦听器设置为3个按钮
我正在尝试制作带有三个按钮的秒表,“开始”,“暂停”和“停止”。 我的讲师只教我们如何将动作监听器设置为两个按钮。 如何将动作侦听器设置为三个按钮? 到目前为止,这是我的编码
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 ); } }
我还没有为我的“暂停”function制作任何动作监听器,因为我不知道如何暂停计时器。 但是在我弄清楚如何暂停之前,我想将动作链接到按钮。
您正在寻找的是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 } }
这也意味着你可以为JMenuItem
的东西重用ActionListener
,只要它们具有相同的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方法中使用此方法。
switch(actionEvent.getActionCommand()) { // cases }