The test code is as follows:
Copy codeThe Code is as follows: 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 saved med 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 saved med 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 ();
}
}
Solution without using ApplicationWindow (that is, just using the SWT class:Copy codeThe Code is as follows: 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 saved med 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 saved med successfully .");
} Catch (NumberFormatException e ){
FahrenheitValue. setText ("");
MessageLabel. setText ("Invalid number format:" + text. getText ());
}
}
}
Public static void main (String [] args ){
New TemperatureConverter ();
}
}