Creating mixed components in swing + java
Is there any way to create a custom component in swing. By custom I mean say right now I am able to create a circle and do actions like dragging it etc.
But now I also want that along with the circle a text label with its number is also present. Can we combine them into a new type of开发者_StackOverflow社区 component where say we can do actions on it collectively?
If yes please give me pointers on how to do so.
Yes, just extend JComponent
and handle painting and interaction however you want. If there is a component that already does most of what you want you could extend that class and tweak it slightly.
I would suggest reading some of the tutorials about how JComponent works: http://download.oracle.com/javase/tutorial/uiswing/components/jcomponent.html
Here is a similar question but I am sure you can find more specific ones that have been asked once you get deeper into it. How to create a custom Swing Component
The Swing tutorial as a whole if very useful to understand how other components work etc. http://download.oracle.com/javase/tutorial/uiswing/components/index.html
You can create your own Swing component extending JComponent
. Several question here on Stack on the topic like this one.
Personally, I advise you to get this book: Java Swing as it contains an excellent guide on how to create Swing components.
精彩评论