如何删除 SWT 按钮的边框,使其看起来像一个标签
How to remove border of SWT Button so that it seems like a Label
我创建了一个在 SWT 中设置图像的按钮。我想删除按钮的边框。所以它看起来像一个标签。请帮助任何人。
下面的代码片段:
breakNodeButton = new Button(this, SWT.TRANSPARENT);
breakNodeButton.setBackground(new Color(getDisplay(), 204, 204, 204));
Image breakNodeLabelImg = ...
breakNodeButton.setImage(breakNodeLabelImg);
按照 Greg 的建议,您可以使用 PaintListener
覆盖按钮的外观。
例如:
Button button = new Button( shell, SWT.PUSH );
button.addPaintListener( new PaintListener() {
@Override
public void paintControl( PaintEvent event ) {
event.gc.setBackground( event.display.getSystemColor( SWT.COLOR_GREEN ) );
event.gc.fillRectangle( event.x, event.y, event.width, event.height );
Image image = event.display.getSystemImage( SWT.ICON_QUESTION );
event.gc.drawImage( image, 0, 0 );
}
} );
绘制代码将在绿色背景上的左上角绘制问号图像。
请注意,按钮的 computeSize()
方法仍然参考(空)文本和图像来计算按钮的首选大小。
因为这可能不是您想要的尺寸,您应该将按钮图像设置为您在绘制代码中使用的图像,或者建议布局使用预先计算的尺寸,例如通过设置合适的 GridData
或 RowData
或者,您可以使用 Label
并添加 MouseListener
来模拟按钮的选择侦听器。但是,默认情况下,Label 无法通过 Tab 遍历获得键盘焦点,因此可能无法完全替代 Button
.
您可以将 SWT.FLAT
工具栏与单个 ToolItem 一起使用:
ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setText("Button text"); // Remove if you don't need text
item.setImage(display.getSystemImage(SWT.ICON_INFORMATION)); // Remove if you don't need image
就是这样!
我创建了一个在 SWT 中设置图像的按钮。我想删除按钮的边框。所以它看起来像一个标签。请帮助任何人。
下面的代码片段:
breakNodeButton = new Button(this, SWT.TRANSPARENT);
breakNodeButton.setBackground(new Color(getDisplay(), 204, 204, 204));
Image breakNodeLabelImg = ...
breakNodeButton.setImage(breakNodeLabelImg);
按照 Greg 的建议,您可以使用 PaintListener
覆盖按钮的外观。
例如:
Button button = new Button( shell, SWT.PUSH );
button.addPaintListener( new PaintListener() {
@Override
public void paintControl( PaintEvent event ) {
event.gc.setBackground( event.display.getSystemColor( SWT.COLOR_GREEN ) );
event.gc.fillRectangle( event.x, event.y, event.width, event.height );
Image image = event.display.getSystemImage( SWT.ICON_QUESTION );
event.gc.drawImage( image, 0, 0 );
}
} );
绘制代码将在绿色背景上的左上角绘制问号图像。
请注意,按钮的 computeSize()
方法仍然参考(空)文本和图像来计算按钮的首选大小。
因为这可能不是您想要的尺寸,您应该将按钮图像设置为您在绘制代码中使用的图像,或者建议布局使用预先计算的尺寸,例如通过设置合适的 GridData
或 RowData
或者,您可以使用 Label
并添加 MouseListener
来模拟按钮的选择侦听器。但是,默认情况下,Label 无法通过 Tab 遍历获得键盘焦点,因此可能无法完全替代 Button
.
您可以将 SWT.FLAT
工具栏与单个 ToolItem 一起使用:
ToolBar toolBar = new ToolBar(parent, SWT.FLAT);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setText("Button text"); // Remove if you don't need text
item.setImage(display.getSystemImage(SWT.ICON_INFORMATION)); // Remove if you don't need image
就是这样!