JPanel没有更新

时间:2016-05-07 12:13:59

标签: java swing

我有两个面板。其中一个是GamePanel,另一个是ControlPanel。当我按下启动按钮(在控制面板中添加)时,球开始移动直到它碰到某些东西。我在ControlPanel中有得分,时间和等级标签,我希望他们在比赛期间更新,但他们不会。

ControlPanel控制

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

public class ControlPanel extends JPanel {

    public static JLabel timeValue, scoreValue, levelValue;
    public static JButton launch;

    public static Timer timer;

    ControlPanel() {
        this.setLayout(null);
        this.setBounds(0, 600, 1200, 200);
    }

    public void addUnits() {

        launch = new JButton("Launch");
        launch.setBounds(450, 70, 90, 65);

        launch.addChangeListener(new ChangeListener() {
            int start = 0;

            public void stateChanged(ChangeEvent e) {
                if (ControlPanel.launch.getModel().isPressed()) {

                    ActionListener taskPerformer = new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            timeValue.setText(++start + " sec");
                        }
                    };

                    timer = new Timer(1000, taskPerformer);
                    timer.setRepeats(true);
                    timer.start();

                    GamePanel.ball.moveBall();
                }
            }
        });

        add(launch);

        JLabel timeLabel = new JLabel("Time");
        timeLabel.setBounds(615, 70, 90, 25);
        add(timeLabel);

        timeValue = new JLabel("0 sec");
        timeValue.setBounds(690, 70, 90, 25);
        add(timeValue);

        JLabel scoreLabel = new JLabel("Score");
        scoreLabel.setBounds(605, 110, 90, 25);
        add(scoreLabel);

        scoreValue = new JLabel(String.valueOf(Score.score));
        scoreValue.setBounds(690, 110, 90, 25);
        add(scoreValue);

        JLabel levelLabel = new JLabel("Level");
        levelLabel.setBounds(800, 110, 90, 25);
        add(levelLabel);

        levelValue = new JLabel(String.valueOf(GamePanel.status));
        levelValue.setBounds(860, 110, 90, 25);
        add(levelValue);
    }
}

这是我的moveBall方法

public void moveBall() {

    // calculate move       

    ActionListener taskPerformer = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {

            // initiate move

            if(hit()) {
                ControlPanel.timer.stop();

                level = getLevel() + 1;
                score = // calculate score;

                timer.stop();

                ControlPanel.levelValue.setText(String.valueOf(level));
                ControlPanel.scoreValue.setText(String.valueOf(score));

                ControlPanel.levelValue.revalidate();
                ControlPanel.scoreValue.revalidate();

                ControlPanel.levelValue.repaint();
                ControlPanel.scoreValue.repaint();
            }

            paintingComponent.repaint();
        }
    };
    timer = new Timer(10, taskPerformer);
    timer.start();
}

0 个答案:

没有答案