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();
}
}
復(fù)制代碼 代碼如下:
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();
}
}
相關(guān)文章
IDEA 2020.1打開時閃退的問題及解決方法(完美解決方法)
這篇文章主要介紹了IDEA 2020.1打開時閃退問題及解決方法,本文給大家分享我的處理方案,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04JVM入門之類加載與字節(jié)碼技術(shù)(類加載與類的加載器)
Java字節(jié)碼增強指的是在Java字節(jié)碼生成之后,對其進行修改,增強其功能,這種方式相當于對應(yīng)用程序的二進制文件進行修改。Java字節(jié)碼增強主要是為了減少冗余代碼,提高性能等2021-06-06Spring Boot 中的自動配置autoconfigure詳解
這篇文章主要介紹了Spring Boot 中的自動配置autoconfigure詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01Java根據(jù)實體生成SQL數(shù)據(jù)庫表的示例代碼
這篇文章主要來和大家分享一個Java實現(xiàn)根據(jù)實體生成SQL數(shù)據(jù)庫表的代碼,文中的實現(xiàn)代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-07-07intellij idea設(shè)置統(tǒng)一JavaDoc模板的方法詳解
這篇文章主要介紹了intellij idea設(shè)置統(tǒng)一JavaDoc模板的方法詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04