• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame - multithreading  RSS feed

 
John Ab
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a Java game for fun and I want to add many things to a JFrame at once. But for some reason, one class and the main method class executes, but the third class containing the second object I want to add doesn't execute. I'm still new to Java so I might get some terms wrong.

Basically I have 3 classes:

**main.java** (main method class + JFrame constructor class)

**Infout.java** (class that draws a keyboard-controlled circle + some stationary rectangles)

**obj2.java** (class that draws a second stationary circle)

Here is the code:
----------------------------

**main.java**


**Infout.java**



**obj2.java**


----------------------------------------------------

So basically, I get no compile errors, but even though I created an instance of each class variable in the main method, and added them both to the JFrame, they cannot both execute at once. If I comment out **obj2** from the main method, **Infout** will show up. If I comment out **Infout** from the main method, **obj2** will show up. But not both at once. If I try both at once, only **Infout** shows up.

As you may have saw, I thought maybe it had something to do with multithreading so I added some code for multithreading that you may have noticed but I'm sure it's wrong since I only learned about multithreading like an hour ago.

May someone pleeeease help me figure this out? I've tried everything I know to solve it, but it just won't work :C.

I would absolutely LOVE example code of maybe a simple program you guys could quickly whip up showing me how this works. I would even love more if you could explain why/how it works the way it does. I like learning!

PS: Please don't just say: You need to use layout managers. What does that mean? What does it look like in code? Examples

Thanks!

- Ab
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't try multithreading with Swing. It is not thread‑safe and you can introduce errors trying to thread Swing objects. They must only be accessed via the Event Despatch Thread (EDT). You start the EDT as shown here. Thread.sleep() will not help you.

Don't add action listeners to panels. They should be added to buttons. Don't make the panel implement ActionListener; it is better to create different classes for all the Listeners.
You are probably right. It is probably indeed a layout problem. You will have to read about layouts here; you are looking for BorderLayout. A frame defaults to border layout, and if you don't tell it where to put the components, they both go in the same place: the centre of the display. Whichever component is added last obscures the others.
Simply try
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss this sort of question in our GUIs forum: moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!