如何创建自定义Swing组件 [英] How to create a custom Swing Component
问题描述
我一直想用Java创建自定义组件,或者自定义现有组件,但我的搜索从未产生任何有用的东西。所以我决定询问StackOverflow社区:
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:
我在哪里可以找到有关自定义Java GUI组件的信息?
当我的意思是定制时,我不是在谈论改变颜色,字体等。我的意思是真的自定义它们。以下是两个模型示例组件:
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:
一个自定义的JScrollPane,每个方向有两个Scrollbar。每个滚动条的行为都不同。例如,外部滚动正常,内部滚动以更加 Picasa 的方式移动视图。我实际上从谷歌小组得到了一个工作(!)答案这里,但这只是代码。
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.
第二个是自定义JComboBox,当弹出列表展开时,它有一个JComponent结束。这可以是任何东西,从带图标的简单JLabel到以某种方式操纵列表的JButton。
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.
推荐答案
本文如何编写自定义Swing组件应该非常有用。它包括复制具有离散步骤和连续范围的Windows滑块控件。
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.
这篇关于如何创建自定义Swing组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!