Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在GUI中更新JLabel(setText不工作)_Java_Swing_User Interface_Jlabel - Fatal编程技术网

Java 在GUI中更新JLabel(setText不工作)

Java 在GUI中更新JLabel(setText不工作),java,swing,user-interface,jlabel,Java,Swing,User Interface,Jlabel,好吧,我正在做一个点击游戏,诸如此类。。。关键是,我无法让JLabel更新。。。我很困惑。。。因为我以前做过这件事。。。这是我的密码 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ClickingGame extends JPanel implements ActionListener {

好吧,我正在做一个点击游戏,诸如此类。。。关键是,我无法让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。除非它有一个侦听器,否则它不会工作,对吗

因此:


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

您从未将ActionListener添加到clickingButton。除非有一个listener,否则它不会工作,对吗

因此:


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

您没有将操作侦听器添加到单击按钮。

您没有将操作侦听器添加到单击按钮。

在actionPerformed方法中,您需要将局部变量“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();
    }
}

在actionPerformed方法中,您需要将局部变量“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();
    }
}

你在骗我吗?…该死的我觉得很傻你在骗我吗?…该死的我觉得很傻请对代码块使用一致的逻辑缩进。请对代码块使用一致的逻辑缩进。“处理格式”你使用IDE吗?他们通常有一个“组合键”。它将格式化所选代码行。在Eclipse中,它将是
Ctrl+I
“处理格式化”你使用IDE吗?他们通常有一个“组合键”。它将格式化所选代码行。在Eclipse中,它将是
Ctrl+I