了解 Java GUI 开发、管理和设置 JComponent
我想讨论一下Java GUI,现在,我还在上学,我已经为课堂做了轻量级GUI开发。(我们简要介绍了它。)
简单明了,我不能做任何我想做的事情,我想构建一个漂亮干净的布局,但当你最大化它时,一切看起来都不太对劲,而且更糟。当放入 GridLayout 中时,JButton 会很大,或者当我明确指定按钮的大小等时,它们会跨越整行。对于 Java gui 开发来说,这是一个又一个令人头痛的问题。
使用 Microsoft WPF/XAML UI 开发更加简单,感觉就像 HTML/CSS。设置宽度、高度、边距和填充非常好,知道我的组件将放在哪里可以让我放心。您甚至可以设计自定义的外观和感觉。
我想知道你们对于开始 Java GUI 开发的人是否有任何技巧和资源。我没有得到的一件事是用 JFrame 启动新项目,即游戏。
在启动时,您将定向到带有 4 个按钮的面板。
玩游戏 - 带您进入新面板来玩游戏。 大厅 - 带您进入类似聊天的界面 等等
这些应该是面板吗?或更多 JFrame,例如当用户单击按钮时,我启动 Play Game JFrame,然后关闭菜单 JFrame。我对 Java gui 真的没有什么想法。
I wanted to have a discussion on Java GUIs, right now, I'm still in school and I've done light gui development for class.(We briefly covered it.)
Plain and simple, I couldn't do anything I want, I wanted to build a nice clean layout but everything looked off and worse when you maximize it. JButton were huge when put inside a GridLayout, or they spanned the whole row, when I clearly specified the size of the button and etc. It's been one headache after another with Java gui development.
With Microsoft WPF/XAML UI development is more straightforward, it felt like HTML/CSS. Setting the width, height, margin, and padding is great, knowing where my components are going to be puts the mind at ease. And you can even design a custom Look and Feel.
I wanted to know if do you guys have any tips and resources for someone starting Java GUI development. And the one thing I don't get is launch new items with a JFrame, i.e a game.
At Launch your directed to a panel with 4 buttons.
Play Game - Takes you to a new panel to play the game.
Lobby - Takes you to a chat like interface
and etc
Should these be panels? Or more JFrames, like when a user click a button I launch the Play Game JFrame then close the menu JFrame. I really have no ideas with Java guis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保您了解并正在使用适当的布局管理器。此 Swing 教程对于了解每个组件的工作原理非常有用: http://download .oracle.com/javase/tutorial/uiswing/layout/visual.html
还要意识到,您可以通过将面板放在另一个面板中来分层布局。有时,为了在保持事情简单的同时达到预期的效果,这是必要的。
对于您的最后一个问题,按钮可以添加到面板中,该面板可以添加到框架之前的其他面板中。
Make sure you understand and are using the appropriate layout managers. This Swing tutorial is very useful for learning how each works: http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
Also realize that you can layer layouts by putting on panel inside another. This is sometimes necessary to achieve a desired effect while keeping things simple.
For your last question, buttons can just be added to a panel that can bee adding to additional panels before a frame.
首先看一下 Mig 布局。这是一个真正的全功能布局管理器,也是目前最好的。如果由于某种原因您无法使用外部依赖项,那么您将需要查看 GridBagLayout。 GridBagLayout 功能强大,足以完成您需要的一切,但它不像 Mig Layout 那样易于使用。
在您将构建的 99% 的应用程序中,您将拥有一个 JFrame,只需转换 JPanel 即可显示不同的屏幕。
First off take a look at Mig Layout. This is a real full featured layout manager and currently is the best one available. If for some reason you can't use external dependencies then you will want to look at GridBagLayout. GridBagLayout will be powerful enough to do everything you need, but it is not as easy to use as something like Mig Layout.
In 99% of the applications you will build you will have a single JFrame and just transition the JPanels to show the different screens.
首先,我在构建 UI 面板时的经验法则之一是永远不要直接设置任何位置或大小。
然后,第二条规则是永远不要直接以像素为单位设置任何首选、最小或最大尺寸(因此,当您从一台显示器更改为另一台具有更高或更低 DPI 分辨率的显示器时,您会感到很痛苦)。看看我博客上的这篇文章 ,很旧但今天仍然有用。
第三,我尽量避免将面板嵌入到面板中,因为这会导致组件对齐问题和组件尺寸不一致。
最后,我尝试对大多数表单使用 DesignGridLayout,如果 UI 布局太复杂,有时会恢复到 GridBagLayout复杂(但复杂的 UI 布局也可能是糟糕的 UI 设计的标志)。
作为关于如何构建 UI 应用程序(通常使用任何 UI 工具包,但特别是 Swing)的一般评论,有
First off, one of my rules of thumb when building UI panels is to never set directly any location or size.
Then, a second rule is to never set any preferred, minimum or maximum size directly in pixels (thus will bite you when you change from one monitor to another one, with higher or lower DPI resolution). Take a look at this post on my blog, quite old but still useful today.
Thirdly, I try to avoid embedding panels into panels because it leads to components alignment problems and inconsistent component sizes.
Finally, I try to use DesignGridLayout for most of my forms, and sometimes revert to GridBagLayout if the UI layout is too complex (but a complex UI layout may also be a sign of poor UI design).
As a general comment about how to build UI applications (with any UI toolkit in general, but with Swing in particular), there are several recommendations that exist out there, but it is hard to find concrete implementations, you have to read a lot about these, and then try to find the way that works best for you.
是的,欢迎。与 HTML /Javascript/CSS 相比,在同等水平的学习时间下,你无法获得比 HTML /Javascript/CSS 更复杂和精致的东西。
(我还没有为您提供所有文件 - 谷歌并开始查找)。
Swing,根据我的经验,绝对感觉就像我阅读了 1000 万份文档,玩了一些演示,花了三四个月每晚 2-3 个小时,你对某些 api 的工作原理有所了解,然后就不知道了为什么有些人不这样做。这很棒。你想要一些东西可以工作,然后实现它,然后继续搞砸你的 GUI 的其余部分。
在我看来,Java swing 迫切需要一个开源 JQuery 类型插件库,它可以为您的 JComponent 提供动画效果并以您喜欢的方式呈现它们。它在桌面上绝对是二等公民,尤其是现在 CSS / HTML 浏览器渲染的复杂性在过去几年中得到了提高。
Nimbus 的外观和感觉风格绝对是朝着正确方向的改进。
您也可以转向 JavaFx。祝你好运。显然它相当不错。我还没有时间或耐心。
如果您被允许使用第三方库:尝试 JAXX 作为一个选项。这个想法是创建一个 css 类型实现,其中样式元素被分成一个可以快速配置和测试的文件。
请阅读此处以获得详细介绍:today.java.net/pub/a/today/2006/03/30/introducing-jaxx.html
JavaCSS 位于 JAXX 项目中。 JAXX 是 swing gui 界面的 xml 格式样式实现。您编写一个 xml 文档和一个 css 样式文档,并且能够将 GUI 的输入和输出绑定到您的 java 实现引擎。 CSS 样式文档允许快速复杂的 GUI 开发。使用 jaxx jar 引擎,xml 代码被转换为 java 代码,其运行速度就像部署在 .java 类文件中一样快。
该项目由法国程序员小组继续进行,现在可以在这里找到:http:// www.nuiton.org/projects/jaxx/files
该演示至少很漂亮,而且大多数功能似乎都有效。
最新版本是 JAXX 2.4.2。最新更新是 2011 年 5 月或 6 月。虽然该文档是法语,但英语版的文档仍然可以理解。只是翻译一下。
jaxx 的原始 ethan nicholas 文件最后更新于 17-07-2009
http://sourceforge.net/projects/ jaxx/
请注意:www.jaxxframework.org/wiki/Main_Page 是一个死链接,该网站已停止使用。相反,已经制作了一个镜像,可以在以下位置找到: buix.labs.libre-entreprise.org/original-jaxx/www.jaxxframework.org/wiki/Main_Page.html
该文档对于理解 jaxx 和 的含义和目的至关重要它的用途。
如果您有耐心、有大量时间并且有兴趣,还可以查看 Chet Haase 的计时框架,为您的组件设置动画,可以创建复杂的效果。它需要学习曲线和时间。
或者付费让专业的第三方库处理您的 GUI 组件? (嗯,答案很明显,没有人花时间和精力去学习创建一个漂亮的 gui 会轻易放弃它)。
所以花三年时间学习然后向别人要钱?
另一个想法 - 我非常热衷于探索,但看起来实现起来可能是一场噩梦,尽管每个人都说它很容易,嵌入一个开源网络浏览器 html / csss 渲染器,并使用 CSS / JQuery / 设计你的 gui HTML。
但看起来你必须首先从源代码编译网络浏览器(mozilla),然后将其包装在另一个程序(例如 JRex)中,然后将整个东西放入像 Jetty 这样的迷你客户端服务器中,只是为了有一个前端你一半喜欢的实现。
而且前提是浏览器实际上符合 CSS2 标准和 HTML4。忘记 HTML5。那是为了未来。
好吧,我今天脾气暴躁,但我不认为这些抱怨是完全没有道理的。
Yes, welcome. Compared to HTML /Javascript/CSS you can get nothing like the sophistication and polish for the equivalent level of time spent learning.
(I haven't sourced all the files for you for anything here - google and start looking up).
Swing, in my experience definitely feels like I read 10 million documents, played with some demos, and spend three or four months nightly for 2-3 hours, and you have some idea of how some of the api's work, and then have no idea why some don't. Its great. You want something to work and then implement that, and proceed to f*ck up the rest of your gui.
Java swing, in my opinion, is desperately crying for an open source JQuery type plugin library that will animate your JComponents and render them in a way that you like. Its a definite second class citizen on the desktop, and especially now that CSS / HTML browser rendering sophistication has improved over the last few years.
The nimbus look and feel style is an improvement definitely in the right direction.
You could also move over to JavaFx. Good luck. Apparently its quite nice. I haven't yet had the time or patience.
If you are allowed to use thirdparty library : try JAXX as an option. The idea was to create a css type implementation, where styling elements are separated into a file that can be quickly configured and tested.
Read here for good introduction: today.java.net/pub/a/today/2006/03/30/introducing-jaxx.html
JavaCSS is found in the JAXX project. JAXX is a xml format style implementation of the swing gui interface. You write an xml document, and a css style document, and are able to bind the inputs and outputs of the GUI to your java implementation engine. The css style document allows for rapid sophisticated gui development. Using the jaxx jar engine, the xml code is converted into java code that runs as rapidly as if deployed in a .java class file.
The project has been continued by a French group of programmers and is now to be found here: http://www.nuiton.org/projects/jaxx/files
The demo is at least pretty and most things seem to work.
The latest release is JAXX 2.4.2. The latest update was May or June 2011. Whilst in French, the documentation is still comprehensible in English. Just translate.
The original ethan nicholas files of jaxx were last updated on 17-07-2009
http://sourceforge.net/projects/jaxx/
Please note: www.jaxxframework.org/wiki/Main_Page is a dead link, the site is discontinued. instead a mirror has been made and can be found at: buix.labs.libre-entreprise.org/original-jaxx/www.jaxxframework.org/wiki/Main_Page.html
This documentation is essential to understand the meaning and purpose of jaxx and its use.
If you are patient, have lots of time, and are interested, also look at the timing framework by Chet Haase, to animate your components, it is possible to create sophisticated effects. It requires learning curve and time.
Or pay to get your gui components handled by a professional third party library? (Um, the obvious answer, no-one who has spent the time and effort to learn to create a pretty gui is just gonna hand that over).
So spend three years learning and then ask for moola from others?
Another idea - I am very into exploring but it looks like it might be a nightmare to implement, despite everyone saying its so easy, get an open source webbrowser html / csss renderer embedded, and design your gui on that, using CSS / JQuery / HTML.
But it looks like you have to first compile the web browser (mozilla) from source, and then wrap that in another program (e.g. JRex) and then put the whole thing in a mini- client server like Jetty, just to have a front end implementation that you halfway like.
And that is only if the browser is actually is as compliant with the CSS2 standard and HTML4. Forget about HTML5. That is for the future.
Okay, I am grumpy today, but I don't think the gripes are completely unjustified.