2014-10-10 82 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