Jface的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(59, 112, 80, 25);
Text text_1 = new Text(parent, SWT.BORDER);
text_1.setText("it's me");
text_1.setBounds(72, 221, 80, 25);
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(59, 112, 80, 25);
Text text_1 = new Text(container, SWT.BORDER);
text_1.setText("it's me");
text_1.setBounds(72, 221, 80, 25);
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()
{
}
}
