在 GUI 中更新 JLabel(设置文本不起作用)



好吧,我正在制作一个点击游戏,等等等等......关键是,我无法让 JLabel 更新...我很困惑...看到我以前做过这个...这是我的代码。

            import javax.swing.*;
            import java.awt.*;
            import java.awt.event.*;
            public class ClickingGame extends JPanel implements ActionListener {
      private static final long serialVersionUID = 1L;
    static JFrame frame;
    static JButton startbutton, clickingbutton, timerstop;
    static JLabel timelabel, scorelabel;
    static int time = 0;
    static JTextField entertime;
    static Timer clock;
    static Timer countdown;
public ClickingGame() {
    setLayout(new GridLayout(3, 2, 5, 5));
    startbutton = new JButton("Start CountDown");
    timelabel = new JLabel("Time Left = NULL", SwingConstants.CENTER);
    entertime = new JTextField();
    clickingbutton = new JButton("Click Here!");
    scorelabel = new JLabel("Score = NULL", SwingConstants.CENTER);
    timerstop = new JButton("Stop Timer!");
    clock = new Timer(1000, this);
    countdown = new Timer(1000, this);

    add(entertime);
    add(startbutton);
    add(timelabel);
    add(scorelabel);
    add(clickingbutton);
    add(timerstop);
    clickingbutton.setEnabled(false);
    timerstop.setEnabled(false);
    startbutton.addActionListener(this);
}
public static void openGUI() {
    frame = new JFrame("Clicking Game");
    ClickingGame contentpane = new ClickingGame();
    frame.setContentPane(contentpane);
    frame.pack();
    frame.setVisible(true);

}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == startbutton) {
        startbutton.setEnabled(false);
        clickingbutton.setEnabled(true);
    }
    if (e.getSource() == clickingbutton) {
        int score = 0;
        score++;
        scorelabel.setText("Score = "+score);
        scorelabel.repaint();
    }

}
 }

如您所见,我使用了"setText"方法,但它不起作用... :(有人可以帮忙吗?

您永远不会将 ActionListener 添加到 clickingButton。除非它有听众,否则它不会工作,对吧?

所以:

public ClickingGame() {
  setLayout(new GridLayout(3, 2, 5, 5));
  startbutton = new JButton("Start CountDown");
  timelabel = new JLabel("Time Left = NULL", SwingConstants.CENTER);
  entertime = new JTextField();
  clickingbutton = new JButton("Click Here!");
  scorelabel = new JLabel("Score = NULL", SwingConstants.CENTER);
  timerstop = new JButton("Stop Timer!");
  clock = new Timer(1000, this);
  countdown = new Timer(1000, this);
  add(entertime);
  add(startbutton);
  add(timelabel);
  add(scorelabel);
  add(clickingbutton);
  add(timerstop);
  clickingbutton.setEnabled(false);
  timerstop.setEnabled(false);
  startbutton.addActionListener(this);
  clickingbutton.addActionListener(this);  // ** add this!
}
另外,请

改进您的代码格式,特别是如果您要请志愿者帮助您。

您尚未将操作侦听器添加到单击按钮。

在方法actionPerform中,您需要使局部变量"score"为字段(全局),以便它可以在调用之间实际增长:

int score; // Our field here :)
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == startbutton) {
        score = 0; //Start with 0
        startbutton.setEnabled(false);
        clickingbutton.setEnabled(true);
    }
    if (e.getSource() == clickingbutton) {
        score++; //Add one for each click
        scorelabel.setText("Score = "+score);
        scorelabel.repaint();
    }
}

最新更新