Applicationwindow_java Programming of SWT (JFace) Experience

Source: Internet
Author: User
Tags gettext
The test code is as follows:
Copy Code code 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 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 ();
}
}

Solutions that do not use Applicationwindow (that is, just SWT classes):
Copy Code code 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 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 ();
}
}

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.