SWT(JFace)体验之Icon任我变
程序员文章站
2022-10-15 10:40:24
代码如下复制代码 代码如下: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();
}
}
复制代码 代码如下:
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();
}
}
上一篇: Java 正则表达式功能及应用
下一篇: 红糖柠檬姜茶的功效,了解过吗?
推荐阅读