分享

(DM)关于几种控件位置方法代码说明

 jinzq 2007-07-03
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SWTTest {

    public static void main(String[] args) {
        try {
            SWTTest window = new SWTTest();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setSize(500, 400);
        shell.setText("SWT Application");
        //
        //shell.setLayout(new FillLayout());
        Composite parent = new Composite(shell,SWT.NONE);
        parent.setBounds(10,10,400,300);
        parent.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));
        
        Composite com = new Composite(parent,SWT.NONE);
        com.setBounds(10,10,300,200);
        com.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
        
        //com.setLayout(new GridLayout());
        Button button = new Button(com,SWT.NONE);
        button.setBounds(10,10,200,100);
        button.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_GREEN));
        //只有容器面板有此方法,普通控件没有,实际返回容器面板的宽和高,x,y位置点都默认为0;
        Rectangle parentbounds = parent.getClientArea();//parentbounds(0,0,400,300)
        Rectangle combounds = com.getClientArea();//combounds(0,0,300,200)
        //最佳最适合控件大小
        //parentsize(310,210)等于parent包含控件com的宽与x点的和(300+10)与高与y点的和(200+10)
        Point parentsize = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);//parentsize(310,210)
        //comsize(210,110)等于com包含控件button的宽与x点的和(200+10)与高与y点的和(100+10)
        Point comsize = com.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);//comsize(210,110)
        Point buttonsize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);//buttonsize(12,22)
        //控件的x,y位置与宽和高
        Rectangle parentr = parent.getBounds();//parentr(10,10,400,300)
        Rectangle comr = com.getBounds();//comr(10,10,300,200)
        Rectangle buttonr = button.getBounds();//buttonr(10,10,200,100)
        //控件的x,y位置
        Point parentl = parent.getLocation();//parentl(10,10)
        Point coml = com.getLocation();//coml(10,10)
        Point buttonl = button.getLocation();//buttonl(10,10)
        //控件的宽和高
        Point parents = parent.getSize();//parents(400,300)
        Point coms = com.getSize();//coms(300,200)
        Point buttons = button.getSize();//buttons(200,100)

        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多