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

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

  1. Adding HTML
  2. Adding an Icon
  3. Setting the Default Button
  4. 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呢?呆会试试。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值