Java 如何创建自定义 Swing 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/822899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:54:22  来源:igfitidea点击:

How to create a custom Swing Component

javaswingjcomponent

提问by pek

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 创建自定义组件,或自定义现有组件,但我的搜索从未产生任何有用的结果。所以我决定问问 StackOverflow 社区:

Where can I find information about customizing Java GUI Components in general?

在哪里可以找到有关自定义 Java GUI 组件的一般信息?

And when I mean customizing, I'm not talking about changing colors, fonts etc. I mean reallycustomize them. Here are two mockup example components:

当我的意思是定制时,我不是在谈论改变颜色、字体等。我的意思是真正定制它们。以下是两个样机示例组件:

Custom Components Mockup

自定义组件模型

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.

每个方向都有两个滚动条的自定义 JScrollPane。每个滚动条的作用都不同。例如,外部的正常滚动,内部的以更像Picasa的方式移动视图。我实际上从这里的google 组那里得到了一个有效的(!)答案,但这只是代码。

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.

第二个是自定义 JComboBox,当弹出列表展开时,它的末尾有一个 JComponent。这可以是任何东西,从带有图标的简单 JLabel 到以某种方式操作列表的 JButton。

But this question isn't about those specific components. It's about finding resources (websites, books, articles etc.) to learn how to create them.

但这个问题与那些特定组件无关。它是关于寻找资源(网站、书籍、文章等)来学习如何创建它们

采纳答案by John K

This article, How To Write a Custom Swing Componentshould be really helpful. It covers replicating a Windows slider control that has both discrete steps and a continuous range.

这篇文章,如何编写自定义 Swing 组件应该真的很有帮助。它涵盖复制具有离散步长和连续范围的 Windows 滑块控件。

回答by ninesided

This is a pretty open question, the short simple answer is that you subclass JComponentor 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 componentswhere there are examples on how to use scroll panesand 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.

这是一个非常开放的问题,简短的简单答案是您子类化JComponent或其后代之一并添加您需要的功能。显然,根据您要执行的操作,这可能会有所不同。对于初学者,我建议您阅读 Sun 的使用 Swing 组件教程,其中有关于如何使用滚动窗格弹出菜单的示例。一旦你通读了这些并进行了实验,你可能会有更具体的问题,人们更容易提供经过深思熟虑的答案。

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.

如果模型中的两个组件都是您想要实现的,那么让我们更好地了解您希望它们如何运行将产生更好的答案。

回答by alphazero

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}

JDK 是开源的。要了解如何编写 Swing 组件,没有比查看 Swing 组件的源代码更好的地方了。通常,当您创建自定义组件时,您会对以下一项或多项感兴趣:{交互语义、视觉表示、组合结构}

Start with the simplest cases:

从最简单的情况开始:

Passive components: JLabel
Interactive components: JButton

被动组件:JLabel
交互组件:JButton

JLabelwill demonstrate the barebones extension of JComponentfor custom drawing of a component; in this case, it is drawing the 'text' string value as its representation.

JLabel将演示JComponent组件的自定义绘图的准系统扩展;在这种情况下,它正在绘制“文本”字符串值作为其表示形式。

JButtonwill demonstrate the barebones extension for user interaction in addition to drawing the component.

JButton除了绘制组件外,还将演示用于用户交互的准系统扩展。

回答by Daniel Hiller

Read Swing Hacks: Tips & Tools for Building Killer GUIs (Marinacci, Adamson). IMHO an essential book for effectively working with swing in general.

阅读Swing Hacks: Tips & Tools for Building Killer GUIs(Marinacci,Adamson)。恕我直言,这是一本有效使用摇摆一般必不可少的书。

回答by Amanda S

Filthy Rich Clientsis 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.

Filthy Rich Clients是一本关于这个主题的优秀的、可读性很强的书。与其说是创建全新的组件,不如说是改进 Swing 组件的外观和感觉,但它确实深入了解了 Swing 的本质并提供了许多实际示例。

回答by kralyk

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:

我个人认为上述答案过于笼统或模糊。此外,由于有人不可避免地会像我一样碰到这个线程,我不妨提供我发现的内容:

Hope this helps...

希望这可以帮助...