SWT(JFace)体验之Icon任我变_java

代码如下

复制代码 代码如下:

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

时间: 2024-09-09 15:33:29

SWT(JFace)体验之Icon任我变_java的相关文章

SWT(JFace)体验之圆环状(戒指型)_java

演示代码: 复制代码 代码如下: package swt_jface.demo1; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.graphics.Point; impor

SWT(JFace)体验之图片的动态渐变效果_Java编程

1.渐变: 复制代码 代码如下: package swt_jface.demo10; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org

SWT(JFace)体验之Sash(活动控件)_Java编程

演示代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Sash; import org.eclipse.swt

SWT(JFace)体验之GridLayout布局_Java编程

GridLayout布局 GridLayout 布局的功能非常强大,也是笔者常用的一种布局方式.GridLayout是网格式布局,它把父组件分成一个表格,默认情况下每个子组件占据一个单元格的空间,每个子组件按添加到父组件的顺序排列在表格中.GridLayout提供了很多的属性,可以灵活设置网格的信息.另外,GridLayout 布局提供了GridData类,子组件可以设置相应的GridData,例如 "dogPhoto.setLayoutData(gridData)",GridData

SWT(JFace)体验之ViewForm的使用_Java编程

代码如下: 复制代码 代码如下: package swt_jface.demo9; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ViewForm; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.s

SWT(JFace) 体验之FontRegistry_Java编程

复制代码 代码如下: package swt_jface.demo; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.ecli

SWT(JFace)体验之打开多个Form_Java编程

代码很简单,如下所示: 复制代码 代码如下: package swt_jface.demo1; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Main { p

SWT(JFace)体验之StyledText类_Java编程

WrapLines.java 复制代码 代码如下: package swt_jface.demo4; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.

SWT(JFace)体验之StackLayout布局_Java编程

测试代码如下: 复制代码 代码如下: package swt_jface.demo2; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.widgets.Button; i