Depressed, have to modify the programs just now.

xiaoxiao2021-03-06  50

Now we continue on the existing procedures:

Adding HTML Adding An Icon Setting The Default Button Creating A Formatted Text Field

Import java.awt. *;

Import java.awt.event. *;

Import javax.swing. *;

Import javax.swing.text.numberformatter;

Import java.text.parseexception;

Import java.text.decimalformat;

Import java.net.URL;

Public Class CelsiusConverter2 Implements ActionListener {

JFrame ConverterFrame;

JPANel ConverterPanel;

JFormattedTextField Tempcelsius;

Jlabel CelsiusLabel, Fahrenheitlabel;

JButton ConvertTemp;

Public CelsiusConverter2 () {

Converterframe = New Jframe ("Convert Celsius to Fahrenheit);

ConvertCrame.SetDefaultCloseOperation (jframe.exit_on_close);

Converterframe.setsize (New Dimension (120, 40));

ConverterPanel = New JPANEL (New GridLayout (2, 2));

AddWidgets ();

ConverterFrame.Getrootpane (). setdefaultButton (ConvertTemp);

Converterframe.getContentPane (). add (converterpanel, borderlayout.center);

Converterframe.PACK ();

Converterframe.setVisible;

}

Private void addwidgets () {

ImageICON Converticon = CreateImageicon ("Images / Convert.gif",

"Convert temperature");

Tempcelsius = new JFORMATTEDTEXTFIELD (New DecimalFormat);

Tempcelsius.SetFocuslostbehavior (JFormattedTextField.commit_or_revert);

Try {

Tempcelsius.setText ("37.0");

Tempcelsius.commitedit ();

} catch (parseexception e) {

E.PrintStackTrace ();

}

CelsiusLabel = New Jlabel ("Celsius", SwingConstants.Left);

CONVERTTEMP = New JButton (Converticon);

Fahrenheitlabel = New Jlabel ("Fahrenheit", SwingConstants.left;

CelsiusLabel.setBorder (BorderFactory.createemptyBorder (5, 5, 5, 5));

Fahrenheitlabel.setBorder (BorderFactory.createemptyBorder (5, 5, 5, 5));

ConvertTemp.AddActionListener (this);

Tempcelsius.AddActionListener (this);

ConverterPanel.Add (Tempcelsius);

ConverterPanel.Add (CelsiusLabel);

ConverterPanel.Add (ConvertTemp);

ConverterPanel.Add (FahrenheitLabel);

}

Public void actionPerformed (ACTIONEvent Event) {

String eventname = event.getActionCommand ();

Int tempfahr = (int) (Double.Parsedouble (Tempcelsius.getText ())))

* 1.8 32);

IF (Tempfahr <= 32) {

Fahrenheitlabel.settext (" "

Tempfahr "& # 176 fahrenheit ");

} else IF (Tempfahr <= 80) {

Fahrenheitlabel.Settext (" "

Tempfahr "& # 176 fahrenheit ");

} else {

Fahrenheitlabel.settext (" "

Tempfahr "& # 176 fahrenheit ");

}

}

Protected Static Imageicon CreateImageicon (String Path,

String description) {

Java.net.url imgurl = Celsiusconverter2.class.getResource (PATH);

IF (imgurl! = null) {

Return New Imageicon (Imgurl, Description);

} else {

System.err.Println ("COULDN't Find File: PATH);

Return NULL;

}

}

Private static void createandshowgui () {

Jframe.SetDefault (TRUE);

Celsiusconvert2 Converter = New CelsiusConverter2 ();

}

Public static void main (String [] args) {

Javax.swing.swingutilities.Invokelater (new runnable () {

Public void run () {

CreateandShowgui ();

}

});

}

}

The first function and MSHTML are a bit similar. We can use setFont () to change the font and use setColor () to change the color. However, we can also make the same settings via HTML tags. (The old version is not supported).

Add icon:

ImageICON Converticon = CreateImageicon ("Images / Convert.gif", "Convert Temperature");

...

CONVERTTEMP = New JButton (icon);

Set the default button:

// set the Button to Be default button.converterframe.getrootpane (). Setdefaultbutton (ConvertTemp);

Create a formatted text domain:

// Create the format for the text field and the formatted text field

Tempcelsius = New JFORMATTEDTEXTFIELD (

New java.text.decimalformat ("## 0.0 #"));

The first seems to be a little bit, can you join JScript? I will try it.

转载请注明原文地址:https://www.9cbs.com/read-114273.html

New Post(0)