郁闷,还得修改刚才的程序。

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

现在我们在已有的程序的基础上继续:

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 + "&#176 </font> Fahrenheit</html>");

} else if (tempFahr <= 80) {

fahrenheitLabel.setText("<html><font Color=green>" +

tempFahr + "&#176 </font> Fahrenheit</html>");

} else {

fahrenheitLabel.setText("<html><font Color=red>" +

tempFahr + "&#176 </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呢?呆会试试。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航