我剛開始學習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");
}
}
}
@bisonsausage:我的猜測是正確的 - 你永遠不設置按鈕的actionCommand因爲我沒有看到'setActionCommand(...)'被調用的任何地方。 – 2014-10-10 01:43:56
我不確定是否瞭解您的新代碼。通過setActionCommand,是否看起來像startB.setActionCommand(「Stop」); ? – bisonsausage 2014-10-10 02:00:11
'請注意,如果您通過setText(...)設置JButton的文本,則不會自動設置actionCommand.'您不需要顯式調用setActionCommand()。當action命令爲空時,getActionCommand()方法將返回文本值。 OP將文本設置爲「Stop」,它將更改從getActionCommand()方法返回的值,但是,代碼正在檢查事件生成時仍處於「開始」狀態的事件操作命令。 – camickr 2014-10-10 02:02:40