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() {
}
}