Results 1 to 4 of 4
Thread: Something I am very confused by:
- 02-05-2013, 02:14 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Something I am very confused by:
Hello, I wanted to ask about something that I am profoundly puzzled by; I realize that you use defined methods that get overridden such as the paint method or the actionPerformed method, but what happens when you have two buttons for example that you want to change after an event? How would you do that when you can only override the actionPerformed method? Would you override it again? The same question goes for paint; how can you paint something after using the paint method once before? Thank you so much!
p.s. If possible can anyone tell me how to set a specific button size and location please? Thanks again!
-
Re: Something I am very confused by:
You could remove the old ActionListener from the JButton and add a new one, but easier is to have logic inside the ActionListener that responds to the state of the program. For instance if you want to toggle the color of something on button press from red to blue to red to blue, ... you have the action listener change a class variable and then change the color based on that.
Surely you're not overriding paint, right? You're overriding paintComponent after reading all our previous recommendations on this subject, right? The answer is the same -- paintComponent should change its behavior depending on the state of the class, in other words depending on what values are held by class fields.The same question goes for paint; how can you paint something after using the paint method once before? Thank you so much!
You should avoid setting specific size and location. Know the layout managers well and use them is your best option.p.s. If possible can anyone tell me how to set a specific button size and location please? Thanks again!
- 02-05-2013, 03:19 AM #3
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Something I am very confused by:
Thank you so much for your reply. So I think I kind of got the gist; if I want to make use paintComponent again, then I have to use it in a seperate class, right? That makes sense. Also, I meant paintcomponent but why are you so against using paint? Lastly, which layout manager do you recommend? Is there absolutely no way of deciding for yourself the size and location of your buttons, like you can do for a rectangle?
-
Re: Something I am very confused by:
No. Your paintComponent method uses the variables held in the class and changes what it displays based on the state of those variables. Only one paintComponent and one component is usually needed.
We've been through this. paint cannot do Swing graphics only AWT and so it will not have double buffering. If you don any animation it will look choppy and awful. paint is responsible for not only painting the component, but also its children and borders, and if you're not careful you can mess up the graphics of these things inadvertently.That makes sense. Also, I meant paintcomponent but why are you so against using paint?
All of them. We often nest JPanels, each using its own layout manager, and so can create complex layouts using simple layout managers. I recommend (again) that you not read but study the tutorials on this subject.Lastly, which layout manager do you recommend?
There are ways but it makes for very brittle code, code that is difficult to maintain or improve.Is there absolutely no way of deciding for yourself the size and location of your buttons, like you can do for a rectangle?
Similar Threads
-
Help Please - I'm confused
By NextToNothing in forum Threads and SynchronizationReplies: 4Last Post: 07-04-2011, 11:37 PM -
Help i am so confused..:(
By angeltiner in forum AWT / SwingReplies: 6Last Post: 04-11-2010, 01:43 AM -
I am confused
By prof.deedee in forum New To JavaReplies: 6Last Post: 10-30-2009, 11:32 PM -
Very confused Plz help!!
By ratb0y in forum NetBeansReplies: 0Last Post: 02-14-2009, 04:34 PM -
a lot confused
By vineethraj in forum New To JavaReplies: 4Last Post: 01-18-2008, 12:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks