ApplicationWindow for SWT (JFace) Experience

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.