简体   繁体   中英

How to use Java SWT .setBackground() method

I am trying to learn how to use Java and the Java SWT library using eclipse, however, I cannot find anything that can change the background color of a shell. This is my code so far:

Display display = new Display();
Shell shell = new Shell(display);
shell.setBackground(SWT.COLOR_BLACK);

However, this is always raising an issue. Is this the best way of doing it, and, if so, how do I make it work. Or is there a better way of doing, and if so, what is it?

setBackground is the correct API to use to set background colours.

However the background colour is not normally inherited by controls you add to the shell so the shell background might be hidden. You can change this behaviour using the setBackgroundMode API:

shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));

shell.setBackgroundMode(SWT.INHERIT_DEFAULT);

Also note that if your code is part of an Eclipse plug-in then the Eclipse CSS styling may override the colours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM