现在我们在已有的程序的基础上继续:
- 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");
converterFrame.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(true);
}
private void addWidgets() {
ImageIcon convertIcon = createImageIcon("images/convert.gif",
"Convert temperature");
tempCelsius = new JFormattedTextField(new DecimalFormat("##0.0#"));
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("<html><font Color=blue>" +
tempFahr + "° </font> Fahrenheit</html>");
} else if (tempFahr <= 80) {
fahrenheitLabel.setText("<html><font Color=green>" +
tempFahr + "° </font> Fahrenheit</html>");
} else {
fahrenheitLabel.setText("<html><font Color=red>" +
tempFahr + "° </font> Fahrenheit</html>");
}
}
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.setDefaultLookAndFeelDecorated(true);
CelsiusConverter2 converter = new CelsiusConverter2();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
第一个功能和MSHTML有点相近。我们可以使用setFont()来改变字体,使用setColor()来改变颜色。但是,我们也可以通过HTML 标记来进行同样的设置。(旧版本不支持)。
增加图标:
ImageIcon convertIcon = createImageIcon("images/convert.gif", "Convert temperature"); ... convertTemp = new JButton(icon);
设置默认按钮:
//Set the button to be default button in the frame.
converterFrame.getRootPane().setDefaultButton(convertTemp);
创建格式化文本域:
//Create the format for the text field and the formatted text field tempCelsius = new JFormattedTextField( new java.text.DecimalFormat("##0.0#"));
第一个好像很有点意思,能否加入JScript呢?呆会试试。