求大神帮忙编个JAVA的GUI程序,将摄氏温度和华氏温度互相转换,十分感谢!

从网上复制粘贴的就不用了...
2026年09月24日 08:26
有2个网友回答
网友(1):



import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;

public class A {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
A window = new A();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
 * Create the application.
 */
public A() {
initialize();
}

/**
 * Initialize the contents of the frame.
 */
int count = 0;
private JTextField txt1;
private JTextField txt2;
private JLabel label;
private JLabel label_1;


private void initialize() {
frame = new JFrame();
frame.setTitle("温度转换");
frame.getContentPane().setFont(new Font("华文楷体", Font.PLAIN, 18));
frame.setBounds(100, 100, 450, 108);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

label_1 = new JLabel("华氏温度 :");
label = new JLabel("摄氏温度");
label.setFont(new Font("宋体", Font.PLAIN, 14));
label.setForeground(Color.BLACK);
label.setBounds(22, 20, 77, 29);
frame.getContentPane().add(label);

txt1 = new JTextField();
txt1.setBounds(111, 24, 66, 21);
frame.getContentPane().add(txt1);
txt1.setColumns(10);


JButton button = new JButton(">>");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Double d = Double.valueOf(txt1.getText());
d = d*1.8 + 32;
txt2.setText(d + "");
}
});
JButton button2 = new JButton("<<");
button2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Double d = Double.valueOf(txt2.getText());
d = (int)((d- 32)/1.8*100)/100.0;
txt1.setText(d + "");
}
});
button.setBounds(210, 23, 33, 23);
frame.getContentPane().add(button);
button2.setBounds(180, 23, 33, 23);
frame.getContentPane().add(button2);

txt2 = new JTextField();
txt2.setBounds(264, 24, 66, 21);
frame.getContentPane().add(txt2);
txt2.setColumns(10);


label_1.setForeground(Color.BLACK);
label_1.setFont(new Font("宋体", Font.PLAIN, 14));
label_1.setBounds(343, 20, 77, 29);
frame.getContentPane().add(label_1);
}
}

网友(2):

package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CelsiusConverterGUI2 extends javax.swing.JFrame {
    private JLabel celsiusLabel;
    private JButton convertButton;
    private JLabel fahrenheitLabel;
    private JTextField tempTextField;
    public CelsiusConverterGUI2() {
        tempTextField = new JTextField();
        celsiusLabel = new JLabel();
        convertButton = new JButton();
        fahrenheitLabel = new JLabel();
        setDefaultCloseOperation(3);
        setTitle("Celsius Converter");
        celsiusLabel.setText("Celsius");
        convertButton.setText("Convert");
        convertButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });
        fahrenheitLabel.setText("Fahrenheit");
        setLayout(new GridLayout(0, 2));
        this.add(tempTextField);
        this.add(celsiusLabel);
        this.add(convertButton);
        this.add(fahrenheitLabel);
        pack();
    }
    private void convertButtonActionPerformed(ActionEvent evt) {
        double tempFahr =((Double.parseDouble(tempTextField.getText()))
                * 1.8 + 32);
        fahrenheitLabel.setText(tempFahr + " Fahrenheit");
    }
    public static void main(String args[]) {
        new CelsiusConverterGUI2().setVisible(true);
    }
}