我刚开始学习 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");
}
}
}
最佳答案
您必须从 Button
获取 ActionCommand
的值,同时您从事件中获取它,这会给您旧值,因为您是在 if
block 中设置文本,因为您还需要在该点上设置 setActionCommand
。
if("Stop".equals(startB.getActionCommand())){//To avoid NullPointer
countF.setText("");
}
关于java 嵌套的 e.getActionCommand 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26290581/