刚学 SWT,本以为很简单的,但是运行程序的时候老是有问题,明明没有错误显示,但是运行下面的程序以后只显示了主窗口,不知道怎么回事,心都凉了public class FenZuKuang { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
final Display display=Display.getDefault();
final Shell shell=new Shell();
shell.setText("服务器管理");
final Composite composite=new Composite(shell,SWT.NONE);
composite.setBounds(20,30,800,500);
//建立三个分组框
// 第一个分组框
final Group group1=new Group(composite,SWT.NONE);
group1.setText("有效更新");
final Button button11=new Button(group1,SWT.RADIO);
button11.setText("等价版本(1.0.1 -> 1.0.2 - 仅服务版本增加) (Q)");
final Button button12=new Button(group1,SWT.RADIO);
button12.setText("兼容版本(1.0.9 -> 1.1.0 - 服务版本和次版本增加) (C)");
//第二个分组框
final Group group2=new Group(composite,SWT.NONE);
group2.setText("更新策略 (U)");
final Label label21=new Label(group2,SWT.NONE);
label21.setText("策略 URL:");
new Text(group2,SWT.NONE);
//第三个分组框
final Group group3=new Group(composite,SWT.NONE);
group3.setText("代理设置");
final Button button3=new Button(group3,SWT.CHECK);
button3.setText("启用 HTTP 代理连接(N)");
final Label label31=new Label(group3,SWT.NONE);
label31.setText("HTTP 代理主机地址: ");
final Text text31=new Text(group3,SWT.NONE);
final Label label32=new Label(group3,SWT.NONE);
label32.setText("HTTP 代理主机端口: ");
final Text text32=new Text(group3,SWT.NONE);
button3.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
if(button3.getSelection())
{
label31.setEnabled(true);
text31.setEnabled(true);
label32.setEnabled(true);
text32.setEnabled(true);
}
else
{
label31.setEnabled(false);
text31.setEnabled(false);
label32.setEnabled(true);
text32.setEnabled(true);
}
}
});
shell.layout();
shell.open();
while(!composite.isDisposed()){//如果主窗口没有关闭,则一直循环
if(!display.readAndDispatch())//如果主窗口不忙,就让Display处在休眠状态
display.sleep();
}
display.dispose();//释放display资源
}
}
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
final Display display=Display.getDefault();
final Shell shell=new Shell();
shell.setText("服务器管理");
final Composite composite=new Composite(shell,SWT.NONE);
composite.setBounds(20,30,800,500);
//建立三个分组框
// 第一个分组框
final Group group1=new Group(composite,SWT.NONE);
group1.setText("有效更新");
final Button button11=new Button(group1,SWT.RADIO);
button11.setText("等价版本(1.0.1 -> 1.0.2 - 仅服务版本增加) (Q)");
final Button button12=new Button(group1,SWT.RADIO);
button12.setText("兼容版本(1.0.9 -> 1.1.0 - 服务版本和次版本增加) (C)");
//第二个分组框
final Group group2=new Group(composite,SWT.NONE);
group2.setText("更新策略 (U)");
final Label label21=new Label(group2,SWT.NONE);
label21.setText("策略 URL:");
new Text(group2,SWT.NONE);
//第三个分组框
final Group group3=new Group(composite,SWT.NONE);
group3.setText("代理设置");
final Button button3=new Button(group3,SWT.CHECK);
button3.setText("启用 HTTP 代理连接(N)");
final Label label31=new Label(group3,SWT.NONE);
label31.setText("HTTP 代理主机地址: ");
final Text text31=new Text(group3,SWT.NONE);
final Label label32=new Label(group3,SWT.NONE);
label32.setText("HTTP 代理主机端口: ");
final Text text32=new Text(group3,SWT.NONE);
button3.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
if(button3.getSelection())
{
label31.setEnabled(true);
text31.setEnabled(true);
label32.setEnabled(true);
text32.setEnabled(true);
}
else
{
label31.setEnabled(false);
text31.setEnabled(false);
label32.setEnabled(true);
text32.setEnabled(true);
}
}
});
shell.layout();
shell.open();
while(!composite.isDisposed()){//如果主窗口没有关闭,则一直循环
if(!display.readAndDispatch())//如果主窗口不忙,就让Display处在休眠状态
display.sleep();
}
display.dispose();//释放display资源
}
}
解决方案 »
- 利用 java.util.ArrayList 和 java.util.Hashtable,完成如下功能
- 刚学java.这道课堂作业题不会求助大家了.(1小题都行啊)谢谢拉
- 一个执行编译的问题 verbose
- Tomcat+mysql,在不修改server.xml怎么才能配置连接池?????
- 面视题:下面代码将输出什么结果
- 问一个美国cs研究生作业
- 一个JSP出错问题,在线等
- 在java里面怎么实现象windows文件查找那样的功能?
- 用netty3还是netty4?
- 关于SQL Server 2000 Driver for JDBC
- 类与接口转型的问题
- netbeans及mysql数据库的高手请进
final Group group1=new Group(composite,SWT.NONE);
group1.setText("有效更新");
在这个后面添加group1.setBounds(50,50,100,100);就看得到了
你需要明白几点:
1,Layout:用来表示当前这个容器是怎么划分的
2,LayoutData:表示当前这个对象在它父对象上是怎么占据的!
3,如果空间Composite里面需要添加组件的话,你必须设置它的Layout
4,必须给没一个Composite设置LayoutData说明其在父中怎么占据
5,父上使用了某个layout,则子控件只能使用相应的data。如:GridLayout上只能使用GridData
你的代码解决办法是:
1,在[shell.setText("服务器管理"); ]后添加如下代码
GridLayout gridLayout = new GridLayout();
gridLayout.marginHeight = gridLayout.marginWidth = 1;
shell.setLayout(gridLayout);
2,在[final Composite composite=new Composite(shell,SWT.NONE); ]后添加如下代码:
composite.setLayout(new GridLayout(1,true));
3,然后你自己领会一下,现在能看到东西了,怎么美化他就是你的事情了!有问题继续联络!
其他组件的分布完全可以用Layout和LayoutData去控制,不要误解!!!!!
setBounds是一种比较死板的固定显示方式,不推荐,建议使用Layout LayoutData qingkangxu的解释很详细```
要真正去理解