小部件的背景颜色在JFace PopupDialog

我想使用JFace PopupDialog作为用户输入的轻量级对话框。 但是我对文本小部件的背景颜色有一些问题。

正如您在下面的1中看到的,SWT.MULTI文本小部件没有背景和边框,SWT.SINGLE文本小部件没有背景。 我尝试用以下方法覆盖背景颜色:

Text comment = new Text(composite, SWT.MULTI|SWT.BORDER); comment.setFocus(); comment.setBackground(new Color(Display.getDefault(), new RGB(000, 000, 000))); // method of PopupDialog applyBackgroundColor(new Color(Display.getDefault(), new RGB(000, 000, 000)), comment); 

有没有人知道如何妥善处理这个问题?

提前致谢!

左侧是文本SWT.Multi,右侧是SWT.SINGLE

编辑:根据要求,这是弹出窗口的源。 我将PopupDialog子类化,因为我希望在Cursor位置旁边打开弹出窗口:

 public class MouseLocationPopupDialog extends PopupDialog { private final static int SHELL_STYLE = PopupDialog.INFOPOPUP_SHELLSTYLE; public MouseLocationPopupDialog(Shell parent, String infoText) { this(parent, SHELL_STYLE, true, false, false, false, false, null, infoText); } public MouseLocationPopupDialog(Shell parent, String titleText, String infoText) { this(parent, SHELL_STYLE, true, false, false, false, false, titleText, infoText); } public MouseLocationPopupDialog(Shell parent, String infoText, final Point size) { this(parent, infoText); getShell().setSize(size); } public MouseLocationPopupDialog(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize, boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText, String infoText) { super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions, titleText, infoText); } @Override protected void adjustBounds() { super.adjustBounds(); Display d = Display.getCurrent(); if (d == null) { d = Display.getDefault(); } Point point = d.getCursorLocation(); getShell().setLocation(point.x + 9, point.y + 14); } } 

实际用法如下:

 final PopupDialog dialog = new MouseLocationPopupDialog(HandlerUtil.getActiveShell(event), "Title", "Bottom bar") { @Override protected Control createDialogArea(Composite parent) { Control composite = super.createDialogArea(parent); Composite table = new Composite((Composite) composite, SWT.NONE); table.setLayout(new GridLayout(2, true)); // text is a member variable text = new Text(table, SWT.SINGLE | SWT.BORDER); Button submit = new Button(table, SWT.PUSH); return composite; } @Override protected Control createContents(Composite parent) { Control contents = super.createContents(parent); final Color backgroundColor = new Color(Display.getCurrent(), new RGB(255, 255, 255)); text.setBackground(backgroundColor); final Color foregroundColor = new Color(Display.getCurrent(), new RGB(0,0,0)); text.setForeground(foregroundColor); backgroundColor.dispose(); foregroundColor.dispose(); return contents; } }; dialog.open(); 

请注意,此Popup独立于其他UI元素:代码不会像其他JFace对话框(例如TitleAreaDialog)那样等待弹出窗口open()的完成

首先,使用SWT.BORDER而不是SWT.BORDER_SOLID如果你很幸运,这会以某种方式导致你的问题。 除此之外,仅从你的小片段就很难看出出了什么问题。 除非稍后有一些其他代码重置背景颜色,否则这应该有效。

更新 :尝试覆盖PopupDialog的方法getBackground()并让它返回您想要的颜色。 您的代码可能位于createDialogArea(..)PopupDialog将此颜色应用于代码之后的所有内容。 如果您只想更改特定控件的背景颜色,可以尝试以下操作:

 @Override protected Control createContents(Composite parent) { Composite contents = super.createContents(parent); // set the color here return contents; }