Java 在 SWT 中设置颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50064/
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
Setting Colors in SWT
提问by Brian Gianforcaro
This is pretty simple, I come from a swing/awt background.
这很简单,我来自 Swing/awt 背景。
I'm just wondering what the proper way to set the background color for a SWT widget is?
我只是想知道为 SWT 小部件设置背景颜色的正确方法是什么?
I've been trying:
我一直在尝试:
widget.setBackground( );
Except I have no idea how to create the color Object in SWT?
除了我不知道如何在 SWT 中创建颜色对象?
采纳答案by jodonnell
To create a color, try this:
要创建颜色,请尝试以下操作:
Device device = Display.getCurrent ();
Color red = new Color (device, 255, 0, 0);
回答by qualidafial
Remember that in SWT you must explicitly dispose any resources that you create when you are done with them. This includes widgets, fonts, colors, images, displays, printers, and GCs. If you do not dispose these resources, eventually your application will reach the resource limit of your operating system and the application will cease to run.
请记住,在 SWT 中,您必须明确地处置您在完成后创建的任何资源。这包括小部件、字体、颜色、图像、显示、打印机和 GC。如果您不处理这些资源,最终您的应用程序将达到操作系统的资源限制,应用程序将停止运行。
See also: SWT: Managing Operating System Resources
另请参阅:SWT:管理操作系统资源
回答by qualidafial
For standard colors (including common colors and default colors used by the operating system) Use Display.getSystemColor(int)
, and pass in the SWT.COLOR_*
constant for the color you want.
对于标准颜色(包括操作系统使用的常用颜色和默认颜色),使用Display.getSystemColor(int)
,并传入SWT.COLOR_*
所需颜色的常量。
Display display = Display.getCurrent();
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Color listBackground = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
Note that you do not need to dispose these colors because SWT created them.
请注意,您不需要处理这些颜色,因为 SWT 创建了它们。