Java 如何使SWT比例组件具有透明的背景?
我试图在绘制图像的画布上使用SWT比例组件(滑块),比例用于在画布上放大和缩小,因此我希望标尺周围的灰色区域继承后面画布上绘制的图像,但我无法,我尝试了来自的建议,但这似乎继承了背景色,但仍在其周围绘制正方形,而不是在该空间上绘制任何东西 以下是此问题示例中的屏幕截图: 此代码用于生成上述窗口:Java 如何使SWT比例组件具有透明的背景?,java,swt,eclipse-rcp,Java,Swt,Eclipse Rcp,我试图在绘制图像的画布上使用SWT比例组件(滑块),比例用于在画布上放大和缩小,因此我希望标尺周围的灰色区域继承后面画布上绘制的图像,但我无法,我尝试了来自的建议,但这似乎继承了背景色,但仍在其周围绘制正方形,而不是在该空间上绘制任何东西 以下是此问题示例中的屏幕截图: 此代码用于生成上述窗口: import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
public class ScaleExample extends Canvas{
public ScaleExample(Composite parent, int style) {
super(parent, style);
addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
ScaleExample.this.paintControl(e);
}
});
this.setBackgroundMode(SWT.INHERIT_FORCE);
Scale zoomRule = new Scale(this, SWT.VERTICAL);
zoomRule.setMinimum(1);
zoomRule.setMaximum(18);
zoomRule.setIncrement(1);
Point p = zoomRule.computeSize(40, 200);
zoomRule.setBounds(10, 10, p.x, p.y);
}
protected void paintControl(PaintEvent e) {
GC gc = e.gc;
Point size = getSize();
int width = size.x, height = size.y;
gc.drawOval(0, 0, width, height);
}
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Scale Example");
shell.setSize(new Point(300, 300));
GridLayout layout = new GridLayout(1, true);
shell.setLayout(layout);
ScaleExample se = new ScaleExample(shell, SWT.BORDER);
se.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
你能发布你的代码和它的屏幕截图吗?我不能发布我的代码,我可以尝试在一个小应用程序中重新创建它,但重要的是,我已经把一个比例组件放在一块画布上,画布上也画过了,比例方块是一个实心块。嗯,我明白了。嗯,我不知道解决办法,但我猜这是不可能的。但也许有人能证明我错了。你知道有没有更好的组件可以用来减轻这个问题的影响?不知道。我个人认为,我会把天平放在画布之外,但我不知道这对你来说是否是一个可行的解决方案。