I am newbie in java. I am facing problem now.
My purpose is to put the time in textfield. I need press "Start" button and then the time will start go.
But after i compile this code. It got error. I have no idea to do it.
Can anybody help me? Thanks everyone!!

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;

   public void pause() {           
        pauseTime = System.currentTimeMillis();
       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");

        final JButton buttonPlay = new JButton(Play);

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

        JTextField text2 = new JTextField(15); 
        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());

        JPanel bottomPanel = new JPanel(new FlowLayout());

        JPanel leftPanel = new JPanel(new FlowLayout());

        JPanel rightPanel = new JPanel(new FlowLayout());

        add(leftPanel, BorderLayout.WEST);


    public void actionPerformed(ActionEvent e) {

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

               } else {

    public static void main(String[] args) {
        JFrame frame = new PlayScene();


What is your error? I have copied the code and it seems you have another class missing which is the buttonPlay object. Is this your error? If it is, declare your buttonPlay somewhere and you would fix it. If it is for your jpanel, create your button and add it to your panel.

thank you for your reply! I had solve the problem. thank you!

I marked it solved then, next time do it yourself, cocfly123!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.