是否可以在 Java 中构建这样的自定义 GUI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2049015/
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
Is it possible to build custom GUI like this in Java?
提问by AmateurProgrammer
I made this in Photoshop and I plan to use it for my file sharing application:
我在 Photoshop 中制作了这个,我计划将它用于我的文件共享应用程序:
I was wondering if it was possible to create GUI for my application that is gonna have this look and feel.
我想知道是否可以为我的应用程序创建具有这种外观和感觉的 GUI。
If I can't build it only by using eclipse or NetBeans, are there any other tools that could help me?
如果我不能仅使用 eclipse 或 NetBeans 来构建它,还有其他工具可以帮助我吗?
采纳答案by OscarRyz
Is not only possible but quite easy, you don't have to go all the way to create a custom Look and Feel.
不仅可能而且很容易,您不必一路创建自定义外观。
This is what I've made in 20 minutes:
这是我在 20 分钟内完成的:
There is a lot of information on the web on how to customize the components without having to create a whole new L&F.
网上有很多关于如何自定义组件而不必创建全新的 L&F 的信息。
Understanding the Swing Architecturehelps a lot.
了解Swing 架构有很大帮助。
Just in case you haven't read it the Swing Tutorialis here.
以防万一您还没有阅读它,Swing 教程就在这里。
Finally you'll need the doc: Java doc
最后你需要文档:Java doc
回答by Paul Tomblin
It's possible. But for most Java developers, it would require suppressing the gag reflex at such an ugly interface.
这是可能的。但是对于大多数 Java 开发人员来说,它需要在如此丑陋的界面上抑制呕吐反射。
Here's a Java interfaceI wrote using no tools other than vi. I didn't design it, I just took the artists design, held my nose, and implemented it.
这是我使用 vi 以外的任何工具编写的Java 接口。我没有设计它,我只是拿了艺术家的设计,捏着鼻子,然后实施了它。
As for specifics, I'd suggest doing as much of the look and feel through the pluggable look and feel in Swing. Also, use LayoutManagers rather than making things constant sizes in constant locations, so that things can grow and shrink to different screen resolutions and also so that if you translate things you don't end up having to resize all your text labels and then shuffling everything else around.
至于具体细节,我建议通过 Swing 中的可插入外观和感觉尽可能多地做外观和感觉。此外,使用 LayoutManagers 而不是在固定位置设置恒定大小,以便事物可以增长和缩小到不同的屏幕分辨率,并且如果您翻译内容,您最终不必调整所有文本标签的大小然后重新排列所有内容其他周围。
回答by Anton Gogolev
Oh dear, no-no-no! If you want your users' eyes to bleed, then go for it. Otherwise, follow whatever UI guidelines are appropriate for your platform.
哦,亲爱的,不不不!如果您想让用户的眼睛流血,那就去做吧。否则,请遵循适合您平台的任何 UI 指南。
To answer your question: this is certainly doable in any modern windowing system.
回答您的问题:这在任何现代窗口系统中都是可行的。
Here's what generally happens when programmers design UIs:
以下是程序员设计 UI 时通常会发生的情况:
(source: jensroesner.de)
(来源:jensroesner.de)
(source: bulkrenameutility.co.uk)
回答by St.Shadow
You can and even you can change it dynamically - see Look-n-Feel featureof swing
您可以甚至可以动态更改它 - 请参阅摇摆的Look-n-Feel 功能
回答by Geo
You'll have to create a lot of custom JComponents. Other than that, possible.
您将不得不创建大量自定义 JComponent。除此之外,还有可能。
回答by n002213f
回答by Syed M Shaaf
Well thats very much possible, although alot of Java Swing developers might not agree with the UI in the image. perhaps rightly so.
嗯,这很有可能,尽管很多 Java Swing 开发人员可能不同意图像中的 UI。也许是正确的。
Although to make it very possible try looking at JWindow
, JTable
, ImageIcon
, Dimension
, JProgressBar
.
You will also need ample of understanding for Java Layouts, and Events such as MouseEvents
, ActionEvents
.
虽然为了使它成为可能,请尝试查看JWindow
, JTable
, ImageIcon
, Dimension
, JProgressBar
。您还需要对 Java 布局和事件(如MouseEvents
, ActionEvents
.
Hope that helps.
希望有帮助。
回答by Denis Tulskiy
Creating new LaF is too much. Since all your JButtons are different, just use JButton.setIcon()
and JButton.setPressedIcon()
and use your images. The rest is loading background and using strange fonts. Use Font.createFont()
to load custom fonts. You'll probably have to draw your own JProgressbar. Override JProgressBar.paintComponent(Graphics g)
and draw your own image.
创建新的 LaF 太多了。由于所有的Jbutton将是不同的,只要使用JButton.setIcon()
和JButton.setPressedIcon()
并使用您的图像。剩下的就是加载背景和使用奇怪的字体。使用Font.createFont()
加载自定义字体。您可能必须绘制自己的 JProgressbar。覆盖JProgressBar.paintComponent(Graphics g)
并绘制您自己的图像。
回答by helpermethod
I would recommend
我会推荐
for laying out components, it's very easy to grasp and is also way more powerful than the standard layout managers (and it's got a nice debugging mode, too).
对于布局组件,它非常容易掌握,而且比标准布局管理器更强大(它也有一个很好的调试模式)。
回答by Prometal328
I have to disagree with you!Actually you canuse NetBeansto create such a GUI. It is easytoo, you have a lot of options to work with! The ones I personally use are:
我必须不同意你的意见!实际上,您可以使用NetBeans来创建这样的GUI。这也很容易,您有很多选择可以使用!我个人使用的有:
1)Create graphics with a painting software, and set it as Iconto the component. I use for the designing process InkScape, but any softwareshould do the trick. You have to be preatty careful in this process, becouse you can't resize imagesin NetBeans (well, I never tried).
1)用画图软件制作图形,设置为Icon给元件。我在设计过程中使用InkScape,但任何软件都可以做到这一点。在此过程中您必须非常小心,因为您无法在 NetBeans 中调整图像大小(嗯,我从未尝试过)。
2)extend the UIclass (Example: public class CustomButtonUI extend BasicButtonUI), and override the method paint, then use the function setUI (componentName.setUI(new CustomButtonUI);)
2)扩展UI类(例如:public class CustomButtonUI extend BasicButtonUI),并覆盖方法paint,然后使用函数setUI(componentName.setUI(new CustomButtonUI);)
This is an example code:
这是一个示例代码:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicButtonUI;
/**
*
* @author Ionut Cicio
*/
public class CustomButton extends BasicButtonUI{
int borderThickness, edgeRoundness;
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(c.getBackground());
borderThickness = 2;
edgeRoundness = 20;
g2.setColor(c.getForeground());
g2.fillRoundRect(0, 0, c.getWidth(), c.getHeight(), edgeRoundness+5, edgeRoundness+5);
g2.drawRoundRect(0, 0, c.getWidth(), c.getHeight(), edgeRoundness+5, edgeRoundness+5);
//g2.fillRect(0, 0, c.getWidth(), c.getHeight());
//g2.drawRect(0, 0, c.getWidth(), c.getHeight());
g2.setColor(c.getBackground());
g2.fillRoundRect(borderThickness, borderThickness, c.getWidth()-(int)(borderThickness*2.5), c.getHeight()-(int)(borderThickness*2.5), edgeRoundness, edgeRoundness);
g2.drawRoundRect(borderThickness, borderThickness, c.getWidth()-(int)(borderThickness*2.5), c.getHeight()-(int)(borderThickness*2.5), edgeRoundness, edgeRoundness);
super.paint(g, c);
}
@Override
protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
}
}
It always happens to use both methods together, and it gives you a really big benefit whe working on animations!
它总是碰巧同时使用这两种方法,并且在处理动画时会给您带来非常大的好处!
For some kinds of UI's as BasicTextFieldUI, understanding how painting works is preatty tricky, but still understandable.
对于像BasicTextFieldUI 这样的某些类型的UI来说,理解绘画的工作原理是相当棘手的,但仍然可以理解。