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

  • SEARCH
  • Home
  • 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 8659301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:58:58+00:00 2026-06-12T15:58:58+00:00

I have a problem. In my program i have 2 classes: MainFrame public class

  • 0

I have a problem.

In my program i have 2 classes:

  1. MainFrame

    public class MainFrame extends javax.swing.JFrame 
    {
    
        Logika logika;
    
        .
        .
        .
    
        private void przyciskKeyPressed(java.awt.event.KeyEvent evt) {
            // TODO add your handling code here:
            char znak = evt.getKeyChar();
            int kod = evt.getKeyCode();
    
    
            if(kod==KeyEvent.VK_A)
            {
                logika.key_pressed("a");
            }
        }
    
    }
    
  2. Logika

    public class Logika {
    
        .
        .
        .
    
    
        Calendar cal = Calendar.getInstance();
    
    
    
    
        public void start()
        {
            gra_rozpoczeta=true;
            punkty=0;
            liczba=1;
            x[0]=251;
            y[0]=301;
    
            cal = Calendar.getInstance();
            czas=cal.getTimeInMillis();
            while(gra_rozpoczeta==true)
            {   
                cal=Calendar.getInstance();
    
                dif=dif-(int)(cal.getTimeInMillis()-czas);
                if(dif<0)
                    akcja();
                try {
                    Thread.currentThread().sleep(50);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Logika.class.getName()).log(Level.SEVERE, null, ex);
                }
                czas=cal.getTimeInMillis();
            }
    
        }
    
        private void akcja()
        {
            dif=500;
    
            liczba++;
            if(liczba==10)
                key_presed=true;
            if(key_presed==true)
                gra_rozpoczeta=false;
        }
    
        public void key_pressed(String s)
        {
            key_presed=true;
            key=s;
        }
    
    }
    

With this methods i have problem.

Loop while last 5 seconds but i want to stop it earlier if i will press key “a”.

Now it is working that:
-program start,
-loop last 5 seconds(im pressing “a”)
-The program responds to the pressed key’s only after the loop

Is it possible to do ?

  • 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-12T15:58:59+00:00Added an answer on June 12, 2026 at 3:58 pm

    Your code examples are a bit lacking, but I would guess that this behaviour is due to improper use of threads. When your mainframe starts the loop, it does not start it in a new thread, it starts it in the main thread, the one that is also listening to keyboard input. So while the loop is running, keyboard entry is blocked, and processed only after the loop is finished.

    If you want to use logica in a different thread, you must change it to be like this:

    public class Logica implements Runnable {
        [...]
        @Override
        public void run() {
            [do loop]
        }
    }
    

    and start it as a new thread in MainFrame like this:

    //start the loop as a new thread...
    final Thread thread = new Thread(logika);
    thread.start();
    //keep listening for keyboard input in the main thread
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have program consisting of a number of classes. I have a problem with
I have a problem with classes. This is my code: self.shapeClass = [HWRectangle class];
I have two classes, a Teacher class and a Student class. In my program
I have a OOP question. I have 3 classes. Program (main class), login form
This is my server code I have a problem because my program freeze and
Problem: Have made a small mail program which works perfectly on my developer pc
I have a problem with 2D transformation program I have the source code import
I have a problem with my iPhone program. I have imported a folder into
Basically i have a problem with this timer program I am trying to put
with my RCP program I have the problem that I want to have more

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.