2014-10-10 109 views
1

我刚开始学习java GUI,并且如标题所示,我遇到了getActionCommand问题。它是一个微波模拟。当倒计时运行并且用户按下停止时,它会将定时器重置为0(或空字符串)。 CountF是JLabel,而startB是JButton。任何帮助将不胜感激java嵌套的e.getActionCommand不起作用

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

    public class Countdown extends JFrame implements ActionListener 
    { 

    private JLabel countF; 
    private JButton oneB; 
    private JButton twoB; 
    private JButton threeB; 
    private JButton fourB; 
    private JButton fiveB; 
    private JButton sixB; 
    private JButton sevenB; 
    private JButton eightB; 
    private JButton nineB; 
    private JButton zeroB; 
    private JButton startB; 
    private JButton openB; 
    private int cookingSeconds; 
    private int time; 

    public static void main(String[] args) 
    { 
     Countdown demoGui = new Countdown(); 
     demoGui.setVisible(true); 
    } 
    public Countdown() 
    { 
     super("Microwave"); 
     this.setSize(700, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLayout(new GridLayout(1,2)); 
     add(new JLabel("Food to be heated")); 
     JPanel rightP = new JPanel(); 
     add(rightP); 
     rightP.setLayout(new BorderLayout()); 
     JPanel textP = new JPanel(); 
     textP.setPreferredSize(new Dimension(300, 50)); 
     textP.setBackground(Color.WHITE); 
     countF = new JLabel(); 
     textP.add(countF); 
     rightP.add(textP, BorderLayout.NORTH); 
     JPanel tempP = new JPanel(); 
     rightP.add(tempP, BorderLayout.CENTER); 
     tempP.setLayout(new GridLayout(4,3)); 
     oneB = new JButton("1"); 
     tempP.add(oneB); 
     twoB = new JButton("2"); 
     tempP.add(twoB); 
     threeB = new JButton("3"); 
     tempP.add(threeB); 
     fourB = new JButton("4"); 
     tempP.add(fourB); 
     fiveB = new JButton("5"); 
     tempP.add(fiveB); 
     sixB = new JButton("6"); 
     tempP.add(sixB); 
     sevenB = new JButton("7"); 
     tempP.add(sevenB); 
     eightB = new JButton("8"); 
     tempP.add(eightB); 
     nineB = new JButton("9"); 
     tempP.add(nineB); 
     zeroB = new JButton("0"); 
     tempP.add(zeroB); 
     startB = new JButton("Start"); 
     tempP.add(startB); 
     openB = new JButton("Open"); 
     tempP.add(openB); 
     startB.addActionListener(this); 
     openB.addActionListener(this); 
     oneB.addActionListener(this); 
     twoB.addActionListener(this); 
     threeB.addActionListener(this); 
     fourB.addActionListener(this); 
     fiveB.addActionListener(this); 
     sixB.addActionListener(this); 
     sevenB.addActionListener(this); 
     eightB.addActionListener(this); 
     nineB.addActionListener(this); 
     zeroB.addActionListener(this); 
    } 
    public void setCountDownLabelText(String text) 
    { 
     countF.setText(text); 
    } 
    public void setOpenBEnable() 
    { 
     openB.setEnabled(true); 
     oneB.setEnabled(true); 
     twoB.setEnabled(true); 
     threeB.setEnabled(true); 
     fourB.setEnabled(true); 
     fiveB.setEnabled(true); 
     sixB.setEnabled(true); 
     sevenB.setEnabled(true); 
     eightB.setEnabled(true); 
     nineB.setEnabled(true); 
     zeroB.setEnabled(true); 
    } 

    //you need to add the event handling for 1, 2, ...9, 0 buttons to calculate the cookingSeconds. 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource() == startB) 
     { 
      time = Integer.parseInt(countF.getText()); 
      cookingSeconds = time; 
      new CountDownTimer(this, cookingSeconds).start(); 
      startB.setText("Stop"); 
      openB.setEnabled(false); 
      oneB.setEnabled(false); 
      twoB.setEnabled(false); 
      threeB.setEnabled(false); 
      fourB.setEnabled(false); 
      fiveB.setEnabled(false); 
      sixB.setEnabled(false); 
      sevenB.setEnabled(false); 
      eightB.setEnabled(false); 
      nineB.setEnabled(false); 
      zeroB.setEnabled(false); 
      if(e.getActionCommand().equals("Stop")) 
      { 
       // this is not working 
       countF.setText(""); 
      } 
     } 
     else if (e.getSource() == openB) 
     { 
      countF.setText(""); 
     } 
     else if (e.getSource() == oneB) 
     { 
      countF.setText(countF.getText() + "1"); 
     } 
     else if (e.getSource() == twoB) 
     { 
      countF.setText(countF.getText() + "2"); 
     } 
     else if (e.getSource() == threeB) 
     { 
      countF.setText(countF.getText() + "3"); 
     } 
     else if (e.getSource() == fourB) 
     { 
      countF.setText(countF.getText() + "4"); 
     } 
     else if (e.getSource() == fiveB) 
     { 
      countF.setText(countF.getText() + "5"); 
     } 
     else if (e.getSource() == sixB) 
     { 
      countF.setText(countF.getText() + "6"); 
     } 
     else if (e.getSource() == sevenB) 
     { 
      countF.setText(countF.getText() + "7"); 
     } 
     else if (e.getSource() == eightB) 
     { 
      countF.setText(countF.getText() + "8"); 
     } 
     else if (e.getSource() == nineB) 
     { 
      countF.setText(countF.getText() + "9"); 
     } 
     else if (e.getSource() == zeroB) 
     { 
      countF.setText(countF.getText() + "0"); 
     } 
    } 
} 

回答

1

我不知道为什么你的代码是不是没有工作的猜测,但无论如何,在这里去 - 也许你从来没有设置actionCommand“停止”。如果是这样,那么if块将永远不会运行。请注意,如果您通过setText(...)设置了JButton的文本,将会自动设置actionCommand,这将使而不是

如果这没有帮助,请考虑花点时间创建并发布minimal example program,以便我们完全理解您的问题,而不必猜测。


我的猜测是正确的 - 你永远不设置按钮的actionCommand因为我没有看到setActionCommand(...)被称为任何地方。

我自己,我会给每组独特的按钮有自己的ActionListeners或Actions,并让他们处理这种事情,而不是一个“交换机”类型的监听器。例如,...

import java.awt.event.*; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class StartStopActionTest { 
    public static void main(String[] args) { 
     Action startStopAction = new StartStopAction("Start", KeyEvent.VK_S, "Stop", KeyEvent.VK_S); 
     JButton startStopButton = new JButton(startStopAction); 
     JPanel panel = new JPanel(); 
     panel.add(startStopButton); 
     JOptionPane.showMessageDialog(null, panel); 
    } 
} 

class StartStopAction extends AbstractAction { 
    private String startText; 
    private int startMnemonic; 
    private String stopText; 
    private int stopMnemonic; 


    public StartStopAction(String startText, int startMnemonic, String stopText, 
     int stopMnemonic) { 
     super(startText); 
     putValue(MNEMONIC_KEY, startMnemonic); 
     this.startText = startText; 
     this.startMnemonic = startMnemonic; 
     this.stopText = stopText; 
     this.stopMnemonic = stopMnemonic; 
    } 


    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (getValue(NAME).equals(startText)) { 
     putValue(NAME, stopText); 
     putValue(MNEMONIC_KEY, stopMnemonic); 

     // start action code here 

     } else { 
     putValue(NAME, startText); 
     putValue(MNEMONIC_KEY, startMnemonic); 

     // stop action code here 

     } 
    } 
} 
+0

@bisonsausage:我的猜测是正确的 - 你永远不设置按钮的actionCommand因为我没有看到'setActionCommand(...)'被调用的任何地方。 – 2014-10-10 01:43:56

+0

我不确定是否了解您的新代码。通过setActionCommand,是否看起来像startB.setActionCommand(“Stop”); ? – bisonsausage 2014-10-10 02:00:11

+0

'请注意,如果您通过setText(...)设置JButton的文本,则不会自动设置actionCommand.'您不需要显式调用setActionCommand()。当action命令为空时,getActionCommand()方法将返回文本值。 OP将文本设置为“Stop”,它将更改从getActionCommand()方法返回的值,但是,代码正在检查事件生成时仍处于“开始”状态的事件操作命令。 – camickr 2014-10-10 02:02:40

2

你,而你是从事件,这将给你的旧值因为你设置里面if块文本还需要得到它从Button得到ActionCommand值至setActionCommand

if("Stop".equals(startB.getActionCommand())){//To avoid NullPointer 
     countF.setText(""); 
} 
+1

+1用于区分按钮动作命令和事件动作命令。 – camickr 2014-10-10 02:07:11

+1

所以我需要setActionCommand以及if内?对不起,我是Java @TAsk的新手 – bisonsausage 2014-10-10 03:31:22