SWT(JFace)体验之ApplicationWindow

 更新时间:2009年06月25日 09:15:33   作者:  
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();
}
}

相关文章

  • Java实现一个简单的文件上传案例示例代码

    Java实现一个简单的文件上传案例示例代码

    这篇文章主要介绍了Java实现一个简单的文件上传案例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • spring声明式事务@Transactional底层工作原理

    spring声明式事务@Transactional底层工作原理

    这篇文章主要为大家介绍分析spring声明式事务@Transactional底层工作原理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪
    2022-02-02
  • SpringBoot中的@EnableConfigurationProperties注解详细解析

    SpringBoot中的@EnableConfigurationProperties注解详细解析

    这篇文章主要介绍了SpringBoot中的@EnableConfigurationProperties注解详细解析,如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component或者实现了@Component的其他注解,那么在IOC容器中是获取不到properties 配置文件转化的bean,需要的朋友可以参考下
    2024-01-01
  • Spring Boot Admin 进行项目监控管理的方法

    Spring Boot Admin 进行项目监控管理的方法

    Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 这篇文章主要介绍了 Spring Boot Admin 进行项目监控管理的方法,需要的朋友可以参考下
    2020-07-07
  • Java多线程按指定顺序同步执行

    Java多线程按指定顺序同步执行

    这篇文章主要介绍了java多线程如何按指定顺序同步执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • @Data注解在Boolean类型属性上的大坑及解决

    @Data注解在Boolean类型属性上的大坑及解决

    在使用@Data注解时,如果类中存在Boolean类型的属性,且属性名不是以"is"开头,那么@Data注解生成的get方法名会默认加上"is",导致属性值无法成功拷贝,解决方法是手动添加get方法,覆盖@Data注解生成的方法
    2024-10-10
  • 完全解析Java编程中finally语句的执行原理

    完全解析Java编程中finally语句的执行原理

    这篇文章主要深度介绍了Java编程中finally语句的执行原理,细致讲解了finally在异常处理中的流程控制作用,需要的朋友可以参考下
    2015-11-11
  • Java Flink与kafka实现实时告警功能过程

    Java Flink与kafka实现实时告警功能过程

    这篇文章主要介绍了Java Flink与kafka实现实时告警功能,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • JAVA Stack详细介绍和示例学习

    JAVA Stack详细介绍和示例学习

    JAVA Stack是栈。它的特性是:先进后出(FILO, First In Last Out)。
    2013-11-11
  • Java8如何优雅的记录代码运行时间

    Java8如何优雅的记录代码运行时间

    这篇文章主要为大家详细介绍了 Java 8 中几种记录代码运行时间的优雅方式,并附上实用工具类与建议,希望可以帮助大家提高大家的代码可读性与复用性
    2025-04-04

最新评论