Forman Smith

Greenhorn
+ Follow
since Jun 20, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
3
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Forman Smith

@Campbell
Thank you! Now I see I was wrong and know what was wrong. Thank you. Usually I use array, but this time I learn something more.
@Piet Souris
Thank you that you taught me a good way to use these buttons.And the bartender ......
8 years ago
Several days ago, i saw the code like below, so i write it myself and find it works.
i want to know, why the five different buttons have the same name?
When the new button bn is created, what happened to the old button bn?
8 years ago

Piet Souris wrote:hi Forman,

your remarks are mostly correct, but I will go through them to get it 100% correct.

1) Not exactly. A variable that is shared between two or more threads faces the following risks:

a) memory inconsistency. Your processor has, in all likelyhood, more than one core. Every
core has its own piece of memory on-board, and Threads are allowed to store variables
in the memory that belongs to the core that the Thread is running on.
So, you might change a variable, but chances are that this change is not seen by a thread
that has its own copy of that variable. Using 'volatile' ensures that your whole program only
has one place where that variable is stored.

b) thread interference. That is something that has to do with two threads writing at the same
time to the same shared variable, therefore one of the threads results might get lost. The way
to prevent that from happening is to use synchonization in some form.
You change 'flag' in a couple of places: in your main program when the button is clicked,
and in yout thread, where you alternate between flag = 1 and flag = 2.
Usually that will work, but there are no guarantees.

Anyway, this is quite complicated stuff. If you do want to work with threads, and who doesn't,
then a thorough read of the Oracle tutorial about Swing concurrency is essential.

2) yes

3) well, this wasn't actually necessary, I admit. In the constructor of 'RoundPanel' I said
'thread = new Thread(this)'.

and in the 'start' method I needed a reference to this thread. If I had used, in the constructor,
'Thread thread = new Thread(this)', then thread would be a local variable that would not
be available in the 'start' method. That's why I made 'thread' a member variable.

But of course I could have put 'Thread thread = new Thread(this)' into the start method,
and then I would not need 'thread' to be a member variable.
But I like a thread to have a name, so that I can always issue something like 'thread.interrupt()',
in an attempt to finish a thread. But that is not relevant here.

Greetz,
Piet




Thanks a lot ,I understand most of it. You are really great.
8 years ago
Thank you so much, Piet Souris , you help me to know the reason and tell me how to modify it, thank you for your kindndss .
I find that you helped on changing these :
1、change the "int flag" to "volatile int flag"; to make sure the flag value is what i want , but not the value that may be changed by something else.
2、add "panel.start();" to make sure the panel start after the construction of frame is finished. Is my understand right?
3、put"Thread thread;" in front, i don't know the reason of this change-_-!!would you please tell me more?
8 years ago
If i change the line 64 from



to

it will work.
what is the difference?
8 years ago
This is a small task to simulate a ball falling and upspring。
Sometimes, when i click the button, it works well, the ball fall down and unspring , over and over.
But sometimes, after i cilck the button, the red just stay there, no movement.
What cause this? how to resolve this?
here is the code:

8 years ago
I want to let the JLbale move with mouse,so i tried this:




This does not work, something wrong in line 33.

But when i tryed this that i move the code frome line 44 to line 48 and place them under line 33,it works. i find this called "anonymous inner classes", but i don't quite understand.


Who can explain this to me? Thanks!
8 years ago
The following code achieve that when i click the different button, different string will display on panel.
Here is the problem:
1,for now,when i click the next button,last displayed string will disappear,how to make all strings remain? Just like add one line of string each time when i click the button, but not replace the last string.
2,I use flag to control the display, is there any other better ways to chieve but not use flag?

8 years ago

Rob Camick wrote:

In your case you are adding the component to the content pane of the frame so you can use frame.revalidate().



I add the

it does work!
Thank you.
8 years ago
I want to drawing something on the panel when i click the button. But now Repaint() does not function unless i modify the size of the frame by mouse......
how should i do?
8 years ago
I need to add JPanel to frame.like this



but why

failed?
8 years ago
I build a GUI, i want to drawing a line(50,50,400,400) when i click the button. Why it does not work? How to make it work?
8 years ago
According to page 384 of <head first java>, i modifyed the original code to below, i want to draw the line when i click the button.But the result is : just start point and the end point remain,no line show up.Why?

8 years ago