程式碼如下
按鈕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){
新的圖標選擇器();
}
}