java嵌套e.getActionCommand不工作

时间:2014-10-10 01:31:42

标签: java swing actionlistener

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

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");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

当您从事件中获取ActionCommand时,您必须从Button获得值{em>旧值,因为您在{{1}内设置文本阻止你在这一点上需要if

setActionCommand

答案 1 :(得分:1)

我无法分辨为什么你的代码在没有猜测的情况下无法正常工作,但无论如何都在这里 - 也许你从未将actionCommand设置为“Stop”。如果是这样,那么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

      }
   }
}