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;
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TestWindow extends ApplicationWindow
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TestWindow()
{
super(null);
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected Control createContents(Composite parent)
{
Text text = new Text(parent,SWT.NONE);
text.setText("hello world");
return parent;
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String args[])
{
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
TestWindow window = new TestWindow();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
e.printStackTrace();
}
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
这个代码是可以运行的,而且运行的结果也看不出什么问题。但看不出来并不代表没有问题。下边我们来让问题显现
在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;
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TestWindow extends ApplicationWindow
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TestWindow()
{
super(null);
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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;
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String args[])
{
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
TestWindow window = new TestWindow();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
e.printStackTrace();
}
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
运行,并没有看到第二个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;
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TestWindow extends ApplicationWindow
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TestWindow()
{
super(null);
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected Control createContents(Composite parent)
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
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;
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String args[])
{
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
TestWindow window = new TestWindow();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
e.printStackTrace();
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void createActions()
{
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
效果依旧,那是为什么呢?
这是因为在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;
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TestWindow extends ApplicationWindow
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TestWindow()
{
super(null);
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected Control createContents(Composite parent)
{
Composite container = new Composite(parent, SWT.NONE);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
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;
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String args[])
{
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
TestWindow window = new TestWindow();
window.setBlockOnOpen(true);
window.open();
Display.getCurrent().dispose();
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
e.printStackTrace();
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void createActions()
{
}
}
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)