代码如下
按钮buttonIcon浏览;
按钮buttonSetIcon;
图像 shellIcon;
图片按钮Icon;
公共图标选择器(){
初始化UI();
shell.pack();
shell.open();
while (!shell.isDispose()) {
if (!display.readAndDispatch()) {
显示.睡眠();
}
}
显示.dispose();
}
私有无效initializeUI(){
GridLayout gridLayout = new GridLayout(3, false);
shell.setLayout(gridLayout);
labelIconFile = 新标签(shell, SWT.NULL);
textIconFile = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.grabExcessHorizontalSpace = true;
textIconFile.setLayoutData(gridData);
ButtonIconBrowse = 新按钮(shell, SWT.PUSH);
网格数据=新的网格数据();
gridData.horizontalSpan = 3;
gridData.horizontalAlignment = GridData.CENTER;
buttonSetIcon = new Button(shell, SWT.PUSH);
buttonSetIcon.setLayoutData(gridData);
shell.setText("图标选择器");
labelIconFile.setText("选择一个图标:");
buttonIconBrowse.setText("浏览");
buttonSetIcon.setText("设置图标");
buttonIconBrowse.addSelectionListener(new SelectionAdapter() {
公共无效widgetSelected(SelectionEvent e){
FileDialog 对话框 = new FileDialog(shell, SWT.OPEN);
字符串文件=dialog.open();
如果(文件!=空){
textIconFile.setText(文件);
}
}
});
buttonSetIcon.addSelectionListener(new SelectionAdapter() {
公共无效widgetSelected(SelectionEvent e){
if(shellIcon != null)
shellIcon.dispose();
尝试 {
shellIcon = new Image(显示, textIconFile.getText());
shell.setImage(shellIcon);
}catch(异常前){
例如:printStackTrace();
}
}
});
}
公共静态无效主(字符串[] args){
新的图标选择器();
}
}