1

わかりました、私はクリック ゲームを作成していますが、何とか何とか... ポイントは、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」メソッドを使用しましたが、うまくいきませんでした... :( 誰か助けてくれませんか?

4

3 に答える 3

5

ClickButtonにActionListenerを追加することはありません。リスナーがいなければ機能しませんよね?

それで:

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!
}

また、特にボランティアに支援を求める場合は、コードのフォーマットを改善してください。

于 2012-07-03T03:07:01.363 に答える
2

クリックボタンにアクションリスナーを追加していません。

于 2012-07-03T03:08:15.180 に答える
1

メソッド 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();
    }
}
于 2012-07-03T05:34:45.447 に答える