代码如下
Schaltfläche buttonIconBrowse;
Schaltfläche buttonSetIcon;
Bild-ShellIcon;
Bild-ButtonIcon;
public IconSelector() {
initializeUI();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void initializeUI() {
GridLayout gridLayout = new GridLayout(3, false);
shell.setLayout(gridLayout);
labelIconFile = new Label(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 = new Button(shell, SWT.PUSH);
GridData = new GridData();
gridData.horizontalSpan = 3;
gridData.horizontalAlignment = GridData.CENTER;
buttonSetIcon = new Button(shell, SWT.PUSH);
buttonSetIcon.setLayoutData(gridData);
shell.setText("Icon Selector");
labelIconFile.setText("Wählen Sie ein Symbol aus:");
buttonIconBrowse.setText("Durchsuchen");
buttonSetIcon.setText("Symbol festlegen");
buttonIconBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
String file = dialog.open();
if (file != null) {
textIconFile.setText(file);
}
}
});
buttonSetIcon.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(shellIcon != null)
shellIcon.dispose();
versuchen {
shellIcon = new Image(display, textIconFile.getText());
shell.setImage(shellIcon);
}catch(Ausnahme ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
neuer IconSelector();
}
}