Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:47:49+00:00 2026-06-04T01:47:49+00:00

I am newbie in java. I am facing problem now. My purpose is to

  • 0

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

Errors

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

Code

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);

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:47:50+00:00Added an answer on June 4, 2026 at 1:47 am

    There are some problems in your code. I would advise you do like that (as fast correction):

    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);
            }
        });
    }
    
    }
    

    Basically you need:

    • correct scope of buttonPlay
    • correctly init javax.swing.Timer with its own listener (not your button listener)
    • use SwingUtilities always – when you need to update or create Swing components and you running code not in Swing EDT

    “Decorative” issues:
    * you should also declare static constants as upper-case

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie on Java EE and got a problem that I don't understand
I'm a newbie in java and I have a small problem. I want to
(Newbie to Java, old time C# guy.) I have noticed a lot of the
I'm a newbie in Java and I have a problem regarding panels. I have
i'm a total newbie to java and Alfresco and i have this simple problem:
Time for my daily newbie Java question :-D I must not be understanding conditionals
I am a newbie at java/java servlet. I need the simpleCaptcha for a form,
Java Newbie question : I need to capture the text being written to a
I am newbie to java programming language. My problem is: I want to read
I'm a newbie in Java and I haven't work with threads in past. Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.