问题
I've always wanted to create custom components in Java, or customize existing ones, but my searches never resulted in anything useful. So I decided to ask the StackOverflow community:
Where can I find information about customizing Java GUI Components in general?
And when I mean customizing, I'm not talking about changing colors, fonts etc. I mean really customize them. Here are two mockup example components:
Notes
I started this question mainly to find how to create the above two custom components. But then I realized that there isn't a general question about hacking swing components. So I thought it would be better to have a list of resources.
In case you are wondering how do the two components in the mockup work, here it is:
A customized JScrollPane that has two Scrollbars for each orientation. Each scrollbar can act differently. For example, the outer ones scroll normally and the inner ones move the view in a more Picasa-like way. I actually got a working(!) answer from google groups here, but that was just code.
The second one is a custom JComboBox which, when the popup list is expanded, has a JComponent at the end. This can be anything, from a simple JLabel with an icon, to a JButton that manipulates the list in a way.
But this question isn't about those specific components. It's about finding resources (websites, books, articles etc.) to learn how to create them.
回答1:
This article, How To Write a Custom Swing Component should be really helpful. It covers replicating a Windows slider control that has both discrete steps and a continuous range.
回答2:
The JDK is open source. To understand how you write a Swing component, there is no better place to look than the source for Swing components. In general, when you create custom components, you are interested in one or more of the following: {interaction semantics, visual representation, compositional structure}
Start with the simplest cases:
Passive components: JLabel
Interactive components: JButton
JLabel
will demonstrate the barebones extension of JComponent
for custom drawing of a component; in this case, it is drawing the 'text' string value as its representation.
JButton
will demonstrate the barebones extension for user interaction in addition to drawing the component.
回答3:
Filthy Rich Clients is an excellent, very readable book on this topic. It's more about improving the look and feel of Swing components than creating entirely new ones, but it does get into the guts of Swing and provides a lot of practical examples.
回答4:
This is a pretty open question, the short simple answer is that you subclass JComponent or one of it's descendants and add the functionality that you require. Obviously, depending on what you're wanting to do this may vary in difficulty. For starters I recommend you read Sun's tutorial on using Swing components where there are examples on how to use scroll panes and popup menus. Once you have read through those and experimented you might have more specific questions that will be easier for people to provide considered answers to.
If the two components in your mock up are all you are wanting to implement then giving us some better idea of how you want them to function will yield better answers.
回答5:
Read Swing Hacks: Tips & Tools for Building Killer GUIs (Marinacci, Adamson). IMHO an essential book for effectively working with swing in general.
回答6:
Personally I find the above answers too general or vague. Also, as someone is inevitably going to bump into this thread just as I did, I might as well provide what I found:
A tutorioal on CodeProject, and,
another blog entry that builds up on the former.
Hope this helps...
来源:https://stackoverflow.com/questions/822899/how-to-create-a-custom-swing-component