Jface的Hello world引出的问题

ace的hello World网上到处都是,但简单的Hello world能引出很多需要注意的问题.
首先大部分网上的jface helloworld如下:
import  org.eclipse.jface.window.ApplicationWindow;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.graphics.Point;
import  org.eclipse.swt.widgets.Composite;
import  org.eclipse.swt.widgets.Control;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Text;


public   class  TestWindow  extends  ApplicationWindow  {

    
public TestWindow() {
        
super(null);
    }

    
protected Control createContents(Composite parent) {
        Text text 
= new Text(parent,SWT.NONE);
        text.setText(
"hello world");
        
return parent;
    }

    
public static void main(String args[]) {
        
try {
            TestWindow window 
= new TestWindow();
            window.setBlockOnOpen(
true);
            window.open();
            Display.getCurrent().dispose();
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

}


这个代码是可以运行的,而且运行的结果也看不出什么问题。但看不出来并不代表没有问题。下边我们来让问题显现
在createContents()函数中再加入一个Text代码变成
import  org.eclipse.jface.window.ApplicationWindow;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.widgets.Composite;
import  org.eclipse.swt.widgets.Control;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Text;


public   class  TestWindow  extends  ApplicationWindow  {

    
public TestWindow() {
        
super(null);
    }

    
protected Control createContents(Composite parent) {
        Text text 
= new Text(parent,SWT.NONE);
        text.setText(
"hello world");
        Text text1 
= new Text(parent,SWT.NONE);
        text1.setText(
"it's me");
        
return parent;
    }

    
public static void main(String args[]) {
        
try {
            TestWindow window 
= new TestWindow();
            window.setBlockOnOpen(
true);
            window.open();
            Display.getCurrent().dispose();
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

}

运行,并没有看到第二个Text,为什么?
是否没有设置text的Bounds?好设置一下
import  org.eclipse.jface.window.ApplicationWindow;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.graphics.Point;
import  org.eclipse.swt.widgets.Composite;
import  org.eclipse.swt.widgets.Control;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Text;


public   class  TestWindow  extends  ApplicationWindow  {

    
public TestWindow() {
        
super(null);
    }

    
protected Control createContents(Composite parent) {

        Text text 
= new Text(parent, SWT.BORDER);
        text.setText(
"hello world");
        text.setBounds(
591128025);
        
        Text text_1 
= new Text(parent, SWT.BORDER);
        text_1.setText(
"it's me");
        text_1.setBounds(
722218025);
        
return parent;
    }

    
public static void main(String args[]) {
        
try {
            TestWindow window 
= new TestWindow();
            window.setBlockOnOpen(
true);
            window.open();
            Display.getCurrent().dispose();
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

    
private void createActions() {
    }

}

效果依旧,那是为什么呢?
这是因为在createContents()方法中直接使用了参数中的parent,造成了布局(layout)的混乱,在只有一个的text的情况下看不出来,现在就看出来了。
解决办法:再构造一个composite,在我们平时使用的时候记得一定要构造一个自己的composite,设置自己的布局,不要直接使用参数中的composite
import  org.eclipse.jface.window.ApplicationWindow;
import  org.eclipse.swt.SWT;
import  org.eclipse.swt.widgets.Composite;
import  org.eclipse.swt.widgets.Control;
import  org.eclipse.swt.widgets.Display;
import  org.eclipse.swt.widgets.Text;


public   class  TestWindow  extends  ApplicationWindow  {

    
public TestWindow() {
        
super(null);
    }

    
protected Control createContents(Composite parent) {
        Composite container 
= new Composite(parent, SWT.NONE);

        Text text 
= new Text(container, SWT.BORDER);
        text.setText(
"hello world");
        text.setBounds(
591128025);
        
        Text text_1 
= new Text(container, SWT.BORDER);
        text_1.setText(
"it's me");
        text_1.setBounds(
722218025);
        
return container;
    }

    
public static void main(String args[]) {
        
try {
            TestWindow window 
= new TestWindow();
            window.setBlockOnOpen(
true);
            window.open();
            Display.getCurrent().dispose();
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

    
private void createActions() {
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值