SWT(JFace)体验之ApplicationWindow
发布时间:2023-01-02 09:59:43
标签:SWT,JFace,ApplicationWindow
测试代码如下:
package swt_jface.demo;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
public class TemperatureConverterJFace extends ApplicationWindow {
Label fahrenheitLabel;
Label celsiusLabel;
Text fahrenheitValue;
Text celsiusValue;
public TemperatureConverterJFace() {
super(null);
addStatusLine();
}
protected Control createContents(Composite parent) {
getShell().setText("JFace Temperature Converter");
Composite converterComposite = new Composite(parent, SWT.NULL);
converterComposite.setLayout(new GridLayout(4, false));
fahrenheitLabel = new Label(converterComposite, SWT.NULL);
fahrenheitLabel.setText("Fahrenheit: ");
fahrenheitValue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);
celsiusLabel = new Label(converterComposite, SWT.NULL);
celsiusLabel.setText("Celsius: ");
celsiusValue = new Text(converterComposite, SWT.SINGLE | SWT.BORDER);
ModifyListener listener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
valueChanged((Text) e.widget);
}
};
fahrenheitValue.addModifyListener(listener);
celsiusValue.addModifyListener(listener);
return converterComposite;
}
public void valueChanged(Text text) {
if (!text.isFocusControl())
return;
if (text == fahrenheitValue) {
try {
double fValue = Double.parseDouble(text.getText());
double cValue = (fValue - 32) / 1.8;
celsiusValue.setText(Double.toString(cValue));
System.out.println("F -> C: " + cValue);
setStatus("Conversion performed successfully.");
} catch (NumberFormatException e) {
celsiusValue.setText("");
setStatus("Invalid number format: " + text.getText());
}
} else {
try {
double cValue = Double.parseDouble(text.getText());
double fValue = cValue * 1.8 + 32;
fahrenheitValue.setText(Double.toString(fValue));
System.out.println("C -> F: " + fValue);
setStatus("Conversion performed successfully.");
} catch (NumberFormatException e) {
fahrenheitValue.setText("");
setStatus("Invalid number format: " + text.getText());
}
}
}
public static void main(String[] args) {
TemperatureConverterJFace converter = new TemperatureConverterJFace();
converter.setBlockOnOpen(true);
converter.open();
Display.getCurrent().dispose();
}
}
不使用ApplicationWindow(即只是用SWT类)的解决方案:
package swt_jface.demo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TemperatureConverter {
Display display = new Display();
Shell shell = new Shell(display);
Label fahrenheitLabel;
Label celsiusLabel;
Label messageLabel;
Text fahrenheitValue;
Text celsiusValue;
public TemperatureConverter() {
shell.setText("SWT Temperature Converter");
shell.setLayout(new GridLayout(4, false));
fahrenheitLabel = new Label(shell, SWT.NULL);
fahrenheitLabel.setText("Fahrenheit: ");
fahrenheitValue = new Text(shell, SWT.SINGLE | SWT.BORDER);
celsiusLabel = new Label(shell, SWT.NULL);
celsiusLabel.setText("Celsius: ");
celsiusValue = new Text(shell, SWT.SINGLE | SWT.BORDER);
messageLabel = new Label(shell, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 4;
messageLabel.setLayoutData(gridData);
ModifyListener listener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
valueChanged((Text) e.widget);
}
};
fahrenheitValue.addModifyListener(listener);
celsiusValue.addModifyListener(listener);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public void valueChanged(Text text) {
if (!text.isFocusControl())
return;
if (text == fahrenheitValue) {
try {
double fValue = Double.parseDouble(text.getText());
double cValue = (fValue - 32) / 1.8;
celsiusValue.setText(Double.toString(cValue));
System.out.println("F -> C: " + cValue);
messageLabel.setText("Conversion performed successfully.");
} catch (NumberFormatException e) {
celsiusValue.setText("");
messageLabel.setText("Invalid number format: " + text.getText());
}
} else {
try {
double cValue = Double.parseDouble(text.getText());
double fValue = cValue * 1.8 + 32;
fahrenheitValue.setText(Double.toString(fValue));
System.out.println("C -> F: " + fValue);
messageLabel.setText("Conversion performed successfully.");
} catch (NumberFormatException e) {
fahrenheitValue.setText("");
messageLabel.setText("Invalid number format: " + text.getText());
}
}
}
public static void main(String[] args) {
new TemperatureConverter();
}
}


猜你喜欢
- 今晚上在编写udp传输文件的时候发现无法用JSON传输字节数组,试了很多种办法都会报错,最后查资料找到了Base64这个类,这个类可以将字节
- 类加载器的分类。试验:使用maven打包<build> <plugins> <plu
- 一、前言让我们先理一下springfox与swagger的关系。swagger是一个流行的API开发框架,这个框架以“开放API声明”(Op
- 随着使用Spring进行开发的个人和企业越来越多,Spring从一个单一简介的框架变成了一个大而全的开源软件,最直观的变化就是Spring需
- 最近在用ssm框架做一个管理系统,做到登录验证时,使用了下面的代码生成图片验证码,最终的效果如下图。Java类public class Ra
- 应用场景假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库中产品取走消费如果仓库中没有产品,则生产者将产品放入仓库,否
- 我遇到一个重复性操作,为了能偷懒发现idea的功能还比较实用纵列选择:Alt+鼠标左键大小写转换:Ctrl+Shirt+u使用小技巧:像这样
- RESTful 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设
- 功能介绍大家都知道在Spring boot开发过程中,需要在配置文件里配置许多信息,如数据库的连接信息等,如果不加密,传明文,数据库就直接暴
- jar包运行时提示jar中没有主清单属性解决办法在pom文件中添加<build> &n
- 前言作为一个新手,最近在学习C#,自己折腾弄了个简单的小说爬虫,实现了把小说内容爬下来写入txt,还只能爬指定网站。第一次搞爬虫,涉及到了网
- C#是托管型代码,创建的对象会自动回收。C++是非托管型代码,创建的对象需要手动回收(有时不手动回收,可能出现内存溢出的问题)。C#调用C+
- 1、LongAdder由来LongAdder类是JDK1.8新增的一个原子性操作类。AtomicLong通过CAS算法提供了非阻塞的原子性操
- 其中第二级目录包含了五个预定义主键分别是:HKEY_CLASSES_ROOT,HKEY_CURRENT_USER,HKEY_LOCAL_MA
- springboot service内组件加载顺序先加载自身构造器,所以在构造器中初始化时若使用需要注入的(即@Autowired注解的)组
- asp.net是没有直接选取文件夹的控件的,我也不知道,如果大家有的话可以一起交流下。后来我想着应该有三种方法:①先将文件夹压缩后上传服务器
- 前言什么是AptAPT从原理上讲是一个编译期的注解处理工具(Annotation Processing Tool)。一些主流的三方库(But
- 前言基于安卓平台的连续滚动图像组件ContinuousScrollableImageView(https://github.com/Cutt
- 本文实例为大家分享了unity实现按页码翻页效果的具体代码,供大家参考,具体内容如下用来做背包 商店的按页翻页功能,先上效果图其中,drag
- 今天在接手别人的一个项目的时候遇到一个坑,坑死我了;是一个打包的问题,好不容易我把代码写完了准备打包测试了,结果java -jar xxx.