我想在 composite 上画一个框,根据composite 大小来动态变动。代码如下,大神们帮忙看看是怎么回事~
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class Test {
public static void main( String[] args ) throws Exception
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello World");
shell.setBounds(100,100,800,600);
shell.setLayout(new FillLayout());
Composite comp = new Composite(shell, SWT.NONE);
Rectangle rect = comp.getClientArea();
final int x = rect.x;
final int y = rect.y;
final int width = rect.width;
final int height = rect.height;
comp.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
e.gc.drawString("a",10,10);
//这里想要画一个根据comp 大小调整的框,但是并没有按照我想的画出来
e.gc.drawRectangle(x+5, y+5, width-10, height-10);
}
});
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class Test {
public static void main( String[] args ) throws Exception
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello World");
shell.setBounds(100,100,800,600);
shell.setLayout(new FillLayout());
Composite comp = new Composite(shell, SWT.NONE);
Rectangle rect = comp.getClientArea();
final int x = rect.x;
final int y = rect.y;
final int width = rect.width;
final int height = rect.height;
comp.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e){
e.gc.drawString("a",10,10);
//这里想要画一个根据comp 大小调整的框,但是并没有按照我想的画出来
e.gc.drawRectangle(x+5, y+5, width-10, height-10);
}
});
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货