在类之间更改JLabel

时间:2011-11-04 19:17:22

标签: java swing netbeans constructor

我的第一篇文章,请原谅任何不正确的礼仪。我正在为学校做年终项目,我需要一些帮助。我在Netbeans中制作一个GUI java应用程序。我有两节课。一个是控制计时器的类,另一个是记分板屏幕的类。我需要使用timerClass中倒计时的时间更新记分板timerLabel。它非常混乱,因为Timer类中有另一个定时器标签,它会更新。我的问题是我无法在MatchScreen()中获取更新的timerLabel。这是我的代码:

计时器类

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class TimerClass extends JFrame {

    Timer timer;
    JLabel promptLabel, timerLabel;
    int counter;
    JTextField tf;
    JButton button;
    MatchScreen call = null;

    public TimerClass() {
        call = new MatchScreen();
        setLayout(new GridLayout(4, 4, 7, 7));
        promptLabel = new JLabel(""
                + "Enter number of seconds for the timer", 
                SwingConstants.CENTER);
        add(promptLabel);
        tf = new JTextField(5);
        add(tf);
        button = new JButton("Start");
        add(button);
        timerLabel = new JLabel("waiting...", 
                SwingConstants.CENTER);
        add(timerLabel);

        event e = new event();
        button.addActionListener(e);
        System.out.println("Button pressed");
    }

    public class event implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.out.println("Action performed");
            int count = (int) (Double.parseDouble(tf.getText()));
            timerLabel.setText("Time left: " + count);
            call.setTimerLabel(count);
            System.out.println("Passed count to tc");
            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            System.out.println("Timer.start");
            timer.start();
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        /*public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
        }*/
    }

    public class TimeClass implements ActionListener {

        int counter;

        public TimeClass(int counter) {
            this.counter = counter;
        }

        public void actionPerformed(ActionEvent e) {
            counter--;
            if (counter >= 1) {
                call.setTimerLabel(counter);
            } else {
                timerLabel.setText("END");
                timer.stop();
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }

    public static void main(String args[]) {
        TimerClass gui = new TimerClass();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(250, 150);
        gui.setTitle("Time Setup");
        gui.setVisible(true);
    }
}

现在是ScoreBoard Screen

public class MatchScreen extends javax.swing.JFrame {

    int redScore = 0, blueScore = 0, blueCat1 = 0,
            blueCat2 = 0, redCat1 = 0, redCat2 = 0, winner = 0;

    public MatchScreen() {
        initComponents();
    }

    //Determine Winner of the match
    public int getWinner() {
        if (redScore > blueScore) {
            winner = 1;
        } else {
            winner = 2;
        }
        return winner;
    }

    public void setTimerLabel(int a) {
        int time = a;
        while (time >= 1) {
            timerLabel.setText("" + time);
        }
        if (time < 1) {
            timerLabel.setText("End");
        }
    }

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
        //Creates an object of the timerClass
        TimerClass gui = new TimerClass();
        gui.setSize(300, 175);
        gui.setTitle("Time Setup");
        gui.setVisible(true);
    }
}

我觉得无关紧要的一些代码是从MatchScreen()中遗漏的。

非常感谢

2 个答案:

答案 0 :(得分:1)

管理解决一般问题。我将所有代码放入一个类中。不理想,但它的确有效:/无论如何,最后期限迫在眉睫。

真诚的谢谢。

答案 1 :(得分:0)

setTimerLabel方法中有一个while循环,我认为你不打算放在那里。另外,您使用参数a并将其分配给time,然后再也不再使用a,为什么不将参数重命名为time并绕过该额外变量?< / p>

修改

抱歉,我忘了解释我所看到的内容:P如果您说call.setTimerLabel(10),那么您点击那个基本上正在运行while(time >= 1的循环(while(10 >= 1))这是无限的环。当您第一次使用setTimerLabel调用方法时,您的程序永远不会离开方法value >= 1