欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SWT(JFace)体验之Icon任我变

程序员文章站 2023-11-25 10:41:16
代码如下复制代码 代码如下:package swt_jface.demo; import org.eclipse.swt.swt; import org.eclipse.s...
代码如下
复制代码 代码如下:

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();
}
}