Jface的Hello world引出的问题
2011-09-11 13:19
141 查看
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(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()
{
}
}
首先大部分网上的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()
{
}
}
相关文章推荐
- 对象迁移表空间引出的三个小问题
- C语言之编译器引出的问题
- Lind.DDD.Utils.HttpHelper里静态对象引出的Http超时问题
- MySQL学习笔记:join速度特别慢引出的问题
- 由问题引出的fsck命令
- 由一道程序员面试宝典引出的问题-printf如何读入浮点数!
- 瞎折腾引出的一个windows系统管理问题
- log4j 相对路径 引出的问题及解决办法
- Android系统篇之----Android中的run-as命令引出升降权限的安全问题(Linux中的setuid和setgid)
- 软件技术积累-问题引出之二
- 由float类型转CString类型引出的问题及分析
- SELinux 引出了vsftp的问题。FTP报错:500 OOPS: cannot change directory
- z-order引出的问题
- Sprintf()的思考和引出的相关问题
- 查看android-support-v4.jar引出的问题
- 有final引出的一个问题
- swap(a,b)引出的问题
- 由自定义控件引出的IL代码的问题
- 由"猫,老鼠和主人"引出的委托,事件及观察者模型问题
- Java入门攻略之Hello World问题