SWT 在linux下 Combo出现异常

本文探讨了在SUSE与Windows XP环境下开发Eclipse Plug-in时遇到的问题,即相同的代码在不同操作系统中表现出的行为差异,特别关注了Combo控件在两种系统下的表现区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为同时在suse和windows xp 下开发Eclipse Plug-in.发现一样的代码,在不同的操作系统中的表现也不一样。最后发现是Combo控件的表现不一致。以下是发现问题的代码:
importorg.eclipse.jface.action.Action;
importorg.eclipse.jface.action.ControlContribution;
importorg.eclipse.jface.action.IAction;
importorg.eclipse.jface.action.ToolBarManager;
importorg.eclipse.swt.SWT;
importorg.eclipse.swt.events.ModifyEvent;
importorg.eclipse.swt.events.ModifyListener;
importorg.eclipse.swt.layout.GridData;
importorg.eclipse.swt.layout.GridLayout;
importorg.eclipse.swt.widgets.Combo;
importorg.eclipse.swt.widgets.Composite;
importorg.eclipse.swt.widgets.Control;
importorg.eclipse.swt.widgets.Display;
importorg.eclipse.swt.widgets.Shell;

classComboContributionextendsControlContribution...{

publicComboContribution(Stringid)...{
super(id);
}


@Override
protectedControlcreateControl(Compositeparent)...{
finalCombocombo=newCombo(parent,SWT.NONE);
combo.setItems(
newString[]...{"JurassicPark","E.T.","JAW"});
combo.addModifyListener(
newModifyListener()...{

publicvoidmodifyText(ModifyEvente)...{
Stringtext
=combo.getText();
System.out.println(
"=========="+text);
}

}
);
//combo.addSelectionListener(newSelectionAdapter(){
//publicvoidwidgetSelected(SelectionEvente){
//Stringtext=combo.getText();
//System.out.println("=========="+text);
//}
//});
returncombo;
}

}


publicclassTestToolBar...{

publicTestToolBar(Shellshell)...{
IActionrunAction
=newAction("Run")...{

publicvoidrun()...{

}

}
;

ToolBarManagerbarManager
=newToolBarManager(SWT.NONE);
barManager.add(runAction);

ComboContributioncombo
=newComboContribution("Combo.contribution");
barManager.add(combo);

barManager.createControl(shell);

GridDatagd
=newGridData(GridData.FILL_HORIZONTAL);
barManager.getControl().setLayoutData(gd);
}


/***//**
*DOCqianbingCommentmethod"main".
*
*
@paramargs
*/

publicstaticvoidmain(String[]args)...{
Displaydisplay
=newDisplay();

finalShellshell=newShell(display);
shell.setLayout(
newGridLayout());

newTestToolBar(shell);

shell.setSize(
300,300);
shell.open();
while(!shell.isDisposed())...{
if(!display.readAndDispatch())
display.sleep();
}

display.dispose();
}

}

Combo加入ModifyListener后,在Windows下是使用正常,但是在Suse下,监听器被调用了2次。而且第一次调用取得的Combo.getText()是“”,第二次才正常。严重影响了程序的流程。后来决定使用SelectionListener, 在在Windows下每次选择都会调用监听器,但是在Suse下,只有选择不同的选项,才会调用监听器。因为SWT是调用操作系统本地控件,所以各个操作系统的表现很可能有出入,其可移植性还是有待提高。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值