秒表在java中不起作用

时间:2012-05-18 09:42:50

标签: java swing timer compiler-errors jbutton

我是java的新手。我现在面临问题。 我的目的是把时间放在文本字段中。我需要按“开始”按钮,然后时间将开始。 但是在编译完这段代码之后。它有错误。我不知道这样做。 有谁能够帮我?谢谢大家!!

错误

I:\PlayScene.java:108: cannot find symbol
symbol: variable buttonPlay
                buttonPlay.setText(Play);
                ^
I:\PlayScene.java:111: cannot find symbol
symbol: variable buttonPlay
                buttonPlay.setText(Pause);
                ^
I:\PlayScene.java:103: cannot find symbol
symbol  : variable buttonPlay
location: class PlayScene
    buttonPlay.addActionListener(new ActionListener() {
    ^
3 errors

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer.*;
import java.util.*;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;

public class PlayScene extends JFrame implements ActionListener{

//declaration for time 
private static final String Play = "Play";
private static final String Pause = "Pause";
private Timer timer = new javax.swing.Timer(100, this);
private long initTime = System.currentTimeMillis();
private long startTime;
private long pauseTime;
private boolean isRunning;

public void start() {
  if (isRunning == false) {
      startTime = System.currentTimeMillis();
    } else {
       startTime = System.currentTimeMillis() - (pauseTime - startTime);
    }

    isRunning = true;
    timer.start();
 }

 public void pause() {           
    pauseTime = System.currentTimeMillis();
    timer.stop();
}
   private String getCurrentTime(long time) {
    return myFormat(time);
}

private String myFormat(final long time) {
    final long hr = TimeUnit.MILLISECONDS.toHours(time);
    final long min = TimeUnit.MILLISECONDS.toMinutes(time
            - TimeUnit.HOURS.toMillis(hr));
    final long sec = TimeUnit.MILLISECONDS.toSeconds(time
            - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
    final long ms = TimeUnit.MILLISECONDS.toMillis(time
            - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min)
            - TimeUnit.SECONDS.toMillis(sec));
    return String.format("%02d:%02d:%02d.%01d", hr, min, sec, ms/100);
}

public PlayScene(){

    super("Memory Game");
    setBounds(300,40,800,600);

    final JButton buttonPlay = new JButton(Play);

    //create label
    JLabel labelTimer = new JLabel("Timer");

    JTextField text2 = new JTextField(15); 
    text2.setEnabled(false);
    text2.setText(getCurrentTime(System.currentTimeMillis() - initTime));

    //Labels for upper 
    JLabel up = new JLabel();
    //JLabel space = new JLabel("\n");

    //Label for left
    JLabel left = new JLabel();
    JLabel space1 = new JLabel("                           ");

    //Label for right
    JLabel right = new JLabel();
    JLabel space2 = new JLabel("                            ");

    //create up panel 
    final JPanel upPanel = new JPanel(new FlowLayout());
    upPanel.add(labelTimer);
    upPanel.add(text2);

    JPanel bottomPanel = new JPanel(new FlowLayout());
    bottomPanel.add(buttonPlay);

    JPanel leftPanel = new JPanel(new FlowLayout());
    leftPanel.add(space1);

    JPanel rightPanel = new JPanel(new FlowLayout());
    rightPanel.add(space2);

    add(upPanel,BorderLayout.NORTH);
    add(bottomPanel,BorderLayout.SOUTH);
    add(leftPanel, BorderLayout.WEST);
    add(rightPanel,BorderLayout.EAST);  

    setVisible(true);
}

public void actionPerformed(ActionEvent e) {

    buttonPlay.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (Pause.equals(cmd)) {
                pause();
                buttonPlay.setText(Play);

           } else {
                buttonPlay.setText(Pause);
                start();
            }
        }
    });        
}

public static void main(String[] args) {
    JFrame frame = new PlayScene();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

2 个答案:

答案 0 :(得分:5)

buttonPlay的范围仅限于PlayScene构造函数的长度,因此无法在其他方法中访问。要解决它,请将其设为类属性。

答案 1 :(得分:3)

您的代码中存在一些问题。我建议你这样做(快速校正):

public class PlayScene extends JFrame {

//declaration for time 
private static final String Play = "Play";
private static final String Pause = "Pause";
private Timer timer;
private long initTime = System.currentTimeMillis();
private long startTime;
private long pauseTime;
private boolean isRunning;

public void start() {
    if (!isRunning) {
        startTime = System.currentTimeMillis();
    } else {
        startTime = System.currentTimeMillis() - (pauseTime - startTime);
    }

    isRunning = true;
    timer.start();
}

public void pause() {
    pauseTime = System.currentTimeMillis();
    timer.stop();
}

private String getCurrentTime(long time) {
    return myFormat(time);
}

private String myFormat(final long time) {
    final long hr = TimeUnit.MILLISECONDS.toHours(time);
    final long min = TimeUnit.MILLISECONDS.toMinutes(time - TimeUnit.HOURS.toMillis(hr));
    final long sec = TimeUnit.MILLISECONDS.toSeconds(time - TimeUnit.HOURS.toMillis(hr)
            - TimeUnit.MINUTES.toMillis(min));
    final long ms = TimeUnit.MILLISECONDS.toMillis(time - TimeUnit.HOURS.toMillis(hr)
            - TimeUnit.MINUTES.toMillis(min) - TimeUnit.SECONDS.toMillis(sec));
    return String.format("%02d:%02d:%02d.%01d", hr, min, sec, ms / 100);
}

public PlayScene() {
    super("Memory Game");
    setBounds(300, 40, 800, 600);

    final JButton buttonPlay = new JButton(Play);

    JLabel labelTimer = new JLabel("Timer");

    final JTextField text2 = new JTextField(15);
    text2.setEnabled(false);
    text2.setText(getCurrentTime(System.currentTimeMillis() - initTime));

    JLabel up = new JLabel();

    JLabel left = new JLabel();
    JLabel space1 = new JLabel("                           ");

    JLabel right = new JLabel();
    JLabel space2 = new JLabel("                            ");

    final JPanel upPanel = new JPanel(new FlowLayout());
    upPanel.add(labelTimer);
    upPanel.add(text2);

    JPanel bottomPanel = new JPanel(new FlowLayout());
    bottomPanel.add(buttonPlay);

    JPanel leftPanel = new JPanel(new FlowLayout());
    leftPanel.add(space1);

    JPanel rightPanel = new JPanel(new FlowLayout());
    rightPanel.add(space2);

    add(upPanel, BorderLayout.NORTH);
    add(bottomPanel, BorderLayout.SOUTH);
    add(leftPanel, BorderLayout.WEST);
    add(rightPanel, BorderLayout.EAST);

    setVisible(true);

    buttonPlay.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (Pause.equals(cmd)) {
                pause();
                buttonPlay.setText(Play);

            } else {
                buttonPlay.setText(Pause);
                start();
            }
        }
    });

    timer = new Timer(100, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    text2.setText(getCurrentTime(System.currentTimeMillis() - initTime));
                }
            });
        }
    });
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new PlayScene();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    });
}

}

基本上你需要:

  • 正确的buttonPlay范围
  • 使用自己的侦听器(而不是按钮侦听器)正确初始化javax.swing.Timer
  • 始终使用SwingUtilities - 当您需要更新或创建Swing组件并运行不在Swing EDT中的代码时

“装饰”问题: *你还应该将静态常量声明为大写