SWT(JFace)体验之Icon任我变
更新时间:2009年06月25日 09:26:25 作者:
SWT(JFace)体验之Icon任我变
代码如下
package swt_jface.demo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class IconSelector {
Display display = new Display();
Shell shell = new Shell(display);
Label labelIconFile;
Text textIconFile;
Button buttonIconBrowse;
Button buttonSetIcon;
Image shellIcon;
Image 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("Select an icon:");
buttonIconBrowse.setText("Browse");
buttonSetIcon.setText("Set Icon");
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();
try {
shellIcon = new Image(display, textIconFile.getText());
shell.setImage(shellIcon);
}catch(Exception ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new IconSelector();
}
}
复制代码 代码如下:
package swt_jface.demo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class IconSelector {
Display display = new Display();
Shell shell = new Shell(display);
Label labelIconFile;
Text textIconFile;
Button buttonIconBrowse;
Button buttonSetIcon;
Image shellIcon;
Image 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("Select an icon:");
buttonIconBrowse.setText("Browse");
buttonSetIcon.setText("Set Icon");
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();
try {
shellIcon = new Image(display, textIconFile.getText());
shell.setImage(shellIcon);
}catch(Exception ex) {
ex.printStackTrace();
}
}
});
}
public static void main(String[] args) {
new IconSelector();
}
}
相关文章
IDEA 2020.1打开时闪退的问题及解决方法(完美解决方法)
这篇文章主要介绍了IDEA 2020.1打开时闪退问题及解决方法,本文给大家分享我的处理方案,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-04-04Spring Boot 中的自动配置autoconfigure详解
这篇文章主要介绍了Spring Boot 中的自动配置autoconfigure详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2024-01-01intellij idea设置统一JavaDoc模板的方法详解
这篇文章主要介绍了intellij idea设置统一JavaDoc模板的方法详解,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-04-04
最新评论