随笔 - 119  文章 - 3173  trackbacks - 0
<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

交友莫独酒,茅台西凤游。
口干古井贡,心徜洋河流。
称多情杜康,趟无量双沟。
赞中华巍巍,无此不销愁。

常用链接

留言簿(68)

随笔分类(136)

随笔档案(122)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 520465
  • 排名 - 93

最新评论

    作SWT程序界面时很多窗体的大小是可变。在大小不固定的窗体里怎么使里面的控件在任意大小下都能保持美观是将让人头疼的事情。FormLayout的布局方式就是解决这一问题的灵丹妙药。上图是王道,看图先:
我胖


我瘦



我标准



以上就是一个窗体在不同大小下的效果,控件随着窗体的大小改变大小和位置,这就是FormLayout的布局方式的的功劳。
FormLayout的布局方式的精髓就两个字“相对”。

下面先看看设计界面:




在设计器里面点击文本框[name_text]可以看到上下左右各出现一个小箭头,这四个箭头就是用来设置该控件在四个方向上的相对位置。
先看看上:
点击上箭头会出现五个小按钮
第一个是设置控件[上边]相对容器
[上边],偏移量为具体数字。
第二个是
置控件[上边]相对容器[底边],偏移量为具体数字。
第三个是设置控件[上边]相对容器[上边],偏移量为百分比。
第四个是设置控件[上边]相对容器内某控件的位置偏移量为具体数字。
第五个是取消设置。

箭头、箭头和下箭头点击以后与此类似。

举例说明:
对于文本框[name_text],我们这样设置:
[上边]相对容器[上边],偏移量为具体数字。
[左边]相对容器[左边],偏移量为具体数字。
[右边]相对容器[右边],偏移量为具体数字。(当容器改变宽度的时候,控件宽度跟着改变)
[下边]相对容器[上边],偏移量为具体数字。(当容器高度变化时,高度不变)

对于
文本框[name_remark],我们这样设置:
[上边]相对容器[上边],偏移量为具体数字。
[左边]相对容器[左边],偏移量为具体数字。
[右边]相对容器[右边],偏移量为具体数字。(当容器改变宽度的时候,控件宽度跟着改变)
[下边]相对容器[下边],偏移量为具体数字。(当容器高度变化时,控件高度跟着改变

通过A相对B这种方式。我们能制作出非常实用美观的界面。
以下是代码:
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class TestFormLayout {

    
private Text text_remark;
    
private Text text_name;
    
protected Shell shell;

    
/**
     * Launch the application
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
try {
            TestFormLayout window 
= new TestFormLayout();
            window.open();
        } 
catch (Exception e) {
            e.printStackTrace();
        }
    }

    
/**
     * Open the window
     
*/
    
public void open() {
        
final Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        
while (!shell.isDisposed()) {
            
if (!display.readAndDispatch())
                display.sleep();
        }
    }

    
/**
     * Create contents of the window
     
*/
    
protected void createContents() {
        shell 
= new Shell();
        shell.setLayout(
new FillLayout());
        shell.setSize(
500375);
        shell.setText(
"SWT Application");

        Label label_remark;

        
final Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayout(
new FormLayout());

        
final Label label_name = new Label(composite, SWT.NONE);
        
final FormData formData = new FormData();
        formData.top 
= new FormAttachment(025);
        formData.left 
= new FormAttachment(030);
        formData.right 
= new FormAttachment(060);
        label_name.setLayoutData(formData);
        label_name.setText(
"姓名");

        text_name 
= new Text(composite, SWT.BORDER);
        formData.bottom 
= new FormAttachment(text_name, 0, SWT.BOTTOM);
        
final FormData formData_1 = new FormData();
        formData_1.top 
= new FormAttachment(025);
        formData_1.right 
= new FormAttachment(100-32);
        formData_1.bottom 
= new FormAttachment(043);
        formData_1.left 
= new FormAttachment(label_name, 5, SWT.DEFAULT);
        text_name.setLayoutData(formData_1);

        text_remark 
= new Text(composite, SWT.BORDER);
        
final FormData formData_2 = new FormData();
        formData_2.bottom 
= new FormAttachment(100-16);
        formData_2.right 
= new FormAttachment(100-32);
        formData_2.top 
= new FormAttachment(062);
        formData_2.left 
= new FormAttachment(065);
        text_remark.setLayoutData(formData_2);
        label_remark 
= new Label(composite, SWT.NONE);
        
final FormData formData_3 = new FormData();
        formData_3.top 
= new FormAttachment(440);
        formData_3.bottom 
= new FormAttachment(510);
        formData_3.right 
= new FormAttachment(060);
        formData_3.left 
= new FormAttachment(030);
        label_remark.setLayoutData(formData_3);
        label_remark.setText(
"说明");
        
posted on 2006-12-25 13:23 交口称赞 阅读(5036) 评论(6)  编辑  收藏 所属分类: Eclipse RCP SWT

FeedBack:
# re: 跟我学之用FormLayout打造自适应窗体大小的控件布局 2006-12-26 08:37 Coffee and Tea
从美观上讲,对于“说明”框的高度应当有个最大值,比如300个象素。  回复  更多评论
  
# re: 跟我学之用FormLayout打造自适应窗体大小的控件布局 2006-12-26 09:11 交口称赞
......................................
这个好像只能按比例,最大最小值这种布局方式好像不行。。。。。。。。
  回复  更多评论
  
# re: 跟我学之用FormLayout打造自适应窗体大小的控件布局 2007-01-31 15:34 caid
兄弟,这种拖拖拽拽的没啥意思
基本不需要介绍  回复  更多评论
  
# re: 用FormLayout打造自适应窗体大小的控件布局[未登录] 2009-02-27 08:37 jessica
如果我没使用swt在panel中显示图像,没使用托拽,纯粹是代码,那我想实现图像随最大化,最小化按钮而改变大小的话该如何做?  回复  更多评论
  
# re: 用FormLayout打造自适应窗体大小的控件布局[未登录] 2009-12-17 10:09 test
@caid
你就一傻逼  回复  更多评论
  
# re: 用FormLayout打造自适应窗体大小的控件布局[未登录] 2012-07-24 13:28 hero
@jessica
FillLayout  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: