SWT(JFace)体验之打开多个Form

时间:2021-11-29 03:19:15 

代码很简单,如下所示:


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 {

public static void main(String[] args) {

Display display = new Display();

Image small = new Image(display, 16, 16);
GC gc = new GC(small);
gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
gc.fillArc(0, 0, 16, 16, 45, 270);
gc.dispose();

Image large = new Image(display, 32, 32);
gc = new GC(large);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
gc.fillArc(0, 0, 32, 32, 45, 270);
gc.dispose();

Shell shell = new Shell(display);
shell.setText("Small and Large icons");
shell.setImages(new Image[] {small, large});
Shell shell2 = new Shell(display);
shell2.setText("Small icon");
shell2.setImage(small);

shell.open();
shell2.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}


下面是打开子画面的代码:


package swt_jface.demo1;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DialogShell {
public DialogShell() {

Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
shell.setSize(500, 200);
final Button openDialog = new Button(shell, SWT.PUSH);
openDialog.setText("Click here to rate this book ...");
openDialog.addSelectionListener(new SelectionListener() {

public void widgetSelected(SelectionEvent e) {
final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
dialog.setLayout(new RowLayout());
final String[] ratings =
new String[] {"Killer!", "Good stuff", "So-so", "Needs work" };
final Button[] radios = new Button[ratings.length];
for (int i = 0; i < ratings.length; i++) {
radios[i] = new Button(dialog, SWT.RADIO);
radios[i].setText(ratings[i]);
}
Button rateButton = new Button(dialog, SWT.PUSH);
rateButton.setText("Rate!");
rateButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < radios.length; i++)
if (radios[i].getSelection()) openDialog.setText("Rating: " + ratings[i]);
dialog.close();
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
dialog.pack();
dialog.open();

Rectangle shellBounds = shell.getBounds();
Point dialogSize = dialog.getSize();
dialog.setLocation(shellBounds.x + (shellBounds.width - dialogSize.x) / 2, shellBounds.y + (shellBounds.height - dialogSize.y) / 2);
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DialogShell();
}
}
标签:SWT,Form
0
投稿

猜你喜欢

  • java 中接口和抽象类的区别与对比

    2022-07-09 04:13:03
  • 详解java模板和回调机制

    2023-08-13 15:33:46
  • SpringBoot整合Activiti工作流框架的使用

    2022-03-02 12:32:44
  • java基础的详细了解第五天

    2023-06-02 16:54:04
  • spring boot RestTemplate 发送get请求的踩坑及解决

    2022-01-19 16:08:09
  • Java Maven依赖传递,可选依赖,排除依赖详解

    2022-06-09 14:24:14
  • 详解Java字节码编程之非常好用的javassist

    2021-08-27 04:54:30
  • CentOS 7系统下配置自定义JDK的教程

    2022-02-27 13:46:26
  • 简单谈谈Struts动态表单(DynamicForm)

    2022-10-07 07:24:29
  • java实现图片写入高清字体及带边框的方法

    2023-11-29 03:43:47
  • Java String对象使用方法详解

    2023-12-14 14:43:52
  • mybatis批量新增、删除、查询和修改方式

    2023-11-23 10:13:01
  • 在Struts2中如何将父类属性序列化为JSON格式的解决方法

    2022-08-01 09:30:34
  • Netty解决 TCP 粘包拆包的方法

    2023-03-09 14:39:06
  • 详解Java利用深度优先遍历解决迷宫问题

    2022-08-20 02:46:54
  • spring中的注解事务演示和添加步骤详情

    2023-03-03 08:32:48
  • java 集合工具类Collections及Comparable和Comparator排序详解

    2023-04-29 03:42:06
  • SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解

    2022-01-31 05:29:10
  • Java CAS操作与Unsafe类详解

    2023-06-15 10:06:49
  • Unity学习之FSM有限状态机

    2022-09-10 06:18:33
  • asp之家 软件编程 m.aspxhome.com