SWT Jface formlayout 完美布局

  

其实刚刚学习SWT/Jface 不久,之前对AWT有所了解,所以很容易上手。


刚开始我要做一个界面设计,需要用布局完成界面框架,然而这个SWT的布局让我迷惑不已,

我基本上把其中的所有的layout用完了,Filllayout、GridLayout、甚至连sashform都用上了各种组合,

搞得页面那个不堪不已啊,自己都无法忍受,最后实在没办法了而且也就剩下formlayout没有尝试过了。


因为在学习的时候我真的没有发现formlayout 有什么用处、有什么特色,真是失算... 后来仔细研究了一把结果恍然大悟,其实那么多布局,两个就够了,

Filllayout和formlayout。 两个的共同特点就是可以自适应屏,formlayout 就是他提供一中类似于html中百分比一样的东东,

而filllayout呢他提是提供填满效果,两者互不。基本上可以解决大多数情况。


强行插入图片:



下面给出代码:


package export;

import org.eclipse.swt.SWT;

public class TestFormLayout extends Shell {

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			TestFormLayout shell = new TestFormLayout(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	Composite composite_left;
	Composite composite_right;
	Composite composite_header;
	Composite composite_bottom;
	/**
	 * Create the shell.
	 * @param display
	 */
	public TestFormLayout(Display display) {
		super(display, SWT.SHELL_TRIM);
		setSize(571, 421);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite_main = new Composite(this, SWT.NONE);
		composite_main.setLayout(new FormLayout());
		
		composite_header = new Composite(composite_main, SWT.BORDER);
		FormData fd_composite_header = new FormData();
		fd_composite_header.bottom = new FormAttachment(0, 50);
		fd_composite_header.right = new FormAttachment(100);
		fd_composite_header.top = new FormAttachment(0);
		fd_composite_header.left = new FormAttachment(0);
		composite_header.setLayoutData(fd_composite_header);
		
		Button btnSsss = new Button(composite_header, SWT.NONE);
		btnSsss.setBounds(107, 10, 80, 27);
		btnSsss.setText("TT2");
		
		Button btnSdfsdf = new Button(composite_header, SWT.NONE);
		btnSdfsdf.setBounds(212, 10, 80, 27);
		btnSdfsdf.setText("TT3");
		
		Button eeee = new Button(composite_header, SWT.NONE);
		eeee.setBounds(10, 10, 80, 27);
		eeee.setText("TT1");
		
		composite_bottom = new Composite(composite_main, SWT.BORDER);
		FormData fd_composite_bottom = new FormData();
		fd_composite_bottom.top = new FormAttachment(100, -20);
		fd_composite_bottom.right = new FormAttachment(100);
		fd_composite_bottom.bottom = new FormAttachment(100);
		fd_composite_bottom.left = new FormAttachment(0);
		composite_bottom.setLayoutData(fd_composite_bottom);
		
		composite_right = new Composite(composite_main, SWT.NONE);
		FormData fd_composite_right = new FormData();
		fd_composite_right.bottom = new FormAttachment(composite_bottom);
		fd_composite_right.top = new FormAttachment(composite_header, 0);
		fd_composite_right.left = new FormAttachment(100, -130);
		
		fd_composite_right.right = new FormAttachment(composite_header, 0, SWT.RIGHT);
		composite_right.setLayoutData(fd_composite_right);
		
		Label lblNewLabel_1 = new Label(composite_right, SWT.NONE);
		lblNewLabel_1.setBounds(10, 94, 61, 17);
		lblNewLabel_1.setText("New Label");
		
		//left
		composite_left = new Composite(composite_main, SWT.NONE);
		FormData fd_composite_left = new FormData();
		fd_composite_left.bottom = new FormAttachment(composite_bottom,0);
		
		Label label = new Label(composite_bottom, SWT.NONE);
		label.setText("   \u4E3B\u7A0B\u5E8F\u7248\u672C:");
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		label.setAlignment(SWT.RIGHT);
		label.setBounds(0, 0, 99, 17);
		
		Link link = new Link(composite_bottom, 0);
		link.setText("\u68C0\u67E5\u66F4\u65B0");
		link.setBounds(296, 0, 99, 17);
		
		Label label_1 = new Label(composite_bottom, SWT.NONE);
		label_1.setText(" 13.3.7");
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		label_1.setBounds(105, 0, 86, 17);
		
		Label label_2 = new Label(composite_bottom, SWT.NONE);
		label_2.setText("\u5DF2\u7ECF\u8FDE\u63A5\u5230\u670D\u52A1\u5668");
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		label_2.setBounds(424, 0, 117, 17);
		fd_composite_left.right = new FormAttachment(composite_right,0);
		fd_composite_left.top = new FormAttachment(composite_header, 0);
		fd_composite_left.left = new FormAttachment(0, 0);
		composite_left.setLayoutData(fd_composite_left);
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	
}



你可能感兴趣的:(SWT/JFACE)