SWT(JFace)體驗(yàn)之ApplicationWindow
測(cè)試代碼如下:
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();
}
}
上一篇:SWT(JFace)體驗(yàn)之復(fù)制粘貼
欄 目:Java編程
下一篇:跟我學(xué)Java Swing之游戲設(shè)計(jì)(1)
本文標(biāo)題:SWT(JFace)體驗(yàn)之ApplicationWindow
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/8527.html
您可能感興趣的文章
- 01-10SWT(JFace)體驗(yàn)之FormLayout布局
- 01-10SWT(JFace)體驗(yàn)之RowLayout布局
- 01-10SWT JFace Bookmark 制作
- 01-10SWT(JFace)小制作 BugTracker
- 01-10SWT(JFace)體驗(yàn)之StyledText類
- 01-10SWT(JFace)體驗(yàn)之GridLayout布局
- 01-10SWT JFace 拖曳效果
- 01-10SWT(JFace)體驗(yàn)之復(fù)制粘貼
- 01-10SWT(JFace)體驗(yàn)之打開多個(gè)Form
- 01-10SWT(JFace)體驗(yàn)之Sash(活動(dòng)控件)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java咖啡館(1)——嘆咖啡
- 01-10JVM的垃圾回收機(jī)制詳解和調(diào)優(yōu)
- 01-10Java Socket編程(三) 服務(wù)器Sockets
- 01-10Java進(jìn)階:Struts多模塊的技巧
- 01-10J2SE 1.5版本的新特性一覽
- 01-10Java Socket編程(一) Socket傳輸模式
- 01-10Java運(yùn)行時(shí)多態(tài)性的實(shí)現(xiàn)
- 01-10Java Socket編程(二) Java面向連接的類
- 01-10Java Socket編程(四) 重復(fù)和并發(fā)服務(wù)
- 01-10Java經(jīng)驗(yàn)點(diǎn)滴:處理沒有被捕獲的異常
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置