起床后,逛了一会儿论坛,找不到什么特别有意思的事情做,继续吧!

类别:Java 点击:0 评论:0 推荐:

这是一个摄氏的转化。

 

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class CelsiusConverter implements ActionListener {

    JFrame converterFrame;

    JPanel converterPanel;

    JTextField tempCelsius;

    JLabel celsiusLabel, fahrenheitLabel;

    JButton convertTemp;

 

    public CelsiusConverter() {

        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() {

 

        tempCelsius = new JTextField(2);

        celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT);

        convertTemp = new JButton("Convert");

        fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);

 

        convertTemp.addActionListener(this);

 

        converterPanel.add(tempCelsius);

        converterPanel.add(celsiusLabel);

        converterPanel.add(convertTemp);

        converterPanel.add(fahrenheitLabel);

       

        celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

    }

 

    public void actionPerformed(ActionEvent event) {

 

        int tempFahr = (int)((Double.parseDouble(tempCelsius.getText()))

                             * 1.8 + 32);

        fahrenheitLabel.setText(tempFahr + " Fahrenheit");

    }

 

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);

 

        CelsiusConverter converter = new CelsiusConverter();

    }

 

    public static void main(String[] args) {

 

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();

            }

        });

    }

}

 

这里和以前的还是没有太多的变化,就是复杂了一点。

 

其他就是这行代码了:

 

int tempFahr = (int)((Double.parseDouble(tempCelsius.getText()))

                             * 1.8 + 32);

本文地址:http://com.8s8s.com/it/it15291.htm