Java GUI编程之(四) 一个温度转换程序

37 篇文章 0 订阅

运行界面:

 

 

代码:

package com.sxpi;

import java.awt.Button;

public class WenDuChang extends JFrame implements ActionListener{
	private JTextField textField;
	private JTextField textField_1;
    private JLabel label;
    private JLabel label_1;
    private JLabel label_2;
    private JRadioButton[] radioButton;   
    private JButton button;
    private JButton button_1;
    private ButtonGroup bg;
    
	
	
	public WenDuChang() {
		//获取你的屏幕的宽和高
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
	    int height = Toolkit.getDefaultToolkit().getScreenSize().height;
	    setLocation(width/2-200, height/2-150);
	    setTitle("\u6E29\u5EA6\u8F6C\u6362\u5668");
		getContentPane().setLayout(null);
		
		textField = new JTextField();//摄氏温度
		textField.setText("0.0");
		textField.setBounds(50, 63, 84, 27);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		label = new JLabel("\u6444\u6C0F\u6E29\u5EA6");
		label.setBounds(137, 69, 54, 15);
		getContentPane().add(label);
		
		label_1 = new JLabel("=");
		label_1.setBounds(201, 69, 54, 15);
		getContentPane().add(label_1);
		
		textField_1 = new JTextField();//华氏温度
		textField_1.setText("0.0");
		textField_1.setBounds(223, 66, 90, 24);
		getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		label_2 = new JLabel("\u534E\u6C0F\u6E29\u5EA6");
		label_2.setBounds(323, 69, 54, 15);
		getContentPane().add(label_2);
		
		radioButton = new JRadioButton[2];
		radioButton[0] = new JRadioButton("\u6444\u6C0F\u8F6C\u534E\u6C0F",true);
		radioButton[0].setBounds(100, 142, 121, 23);
		getContentPane().add(radioButton[0]);
		
		radioButton[1] = new JRadioButton("\u534E\u6C0F\u8F6C\u6444\u6C0F");
		radioButton[1].setBounds(223, 142, 121, 23);
		getContentPane().add(radioButton[1]);
		
		
		bg=new ButtonGroup(); 
		bg.add(radioButton[0]);
		bg.add(radioButton[1]);

		
		button = new JButton("\u6E29\u5EA6\u8F6C\u5316");
		button.setBounds(100, 196, 93, 23);
		button.addActionListener(this);
		getContentPane().add(button);
		
		button_1 = new JButton("\u9000\u51FA");
		button_1.setBounds(237, 196, 93, 23);
		button_1.addActionListener(this);
		getContentPane().add(button_1);
		
		setSize(450, 300);
		setVisible(true);
	}

	
	//用于温度转换的方法-----业务处理
	public String WenDuChang(final double tc,final double tf,boolean flag){
		double wdc=0.0;
		//小数格式化,引号中的0.000表示保留小数点后二位(第四位四舍五入)
		DecimalFormat df = new DecimalFormat("0.00");
		String str;
		if(flag){			
			wdc=9*(tc-10)/5+50;	//摄氏温度转化为华氏温度		
		}
		else{
			wdc=5*(tf-50)/9+10;	//华氏温度转化为摄氏温度		
		}
		str=df.format(wdc);
		return str;		
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		double tc = Double.parseDouble(textField.getText().trim());
		double tf = Double.parseDouble(textField_1.getText().trim());
		
		if(e.getSource()==button){
			//System.out.println("按钮被选中.....");
			if(radioButton[0].isSelected()){//判断radioButton是否被选中
				textField_1.setText(WenDuChang(tc,tf,true));
			}
			if(radioButton[1].isSelected()){
				textField.setText(WenDuChang(tc,tf,false));			
				
			}
			
		}
		if(e.getSource()==button_1){
			System.exit(0);
		}
		
	}
	
	
	
	
	
}


 

好的,下面是一个简单的Java GUI程序,可以将摄氏度转换为华氏度,以及将华氏度转换为摄氏度。代码如下: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TemperatureConverter extends JFrame implements ActionListener { private JLabel celsiusLabel; private JLabel fahrenheitLabel; private JTextField celsiusField; private JTextField fahrenheitField; private JButton convertCelsiusToFahrenheitButton; private JButton convertFahrenheitToCelsiusButton; public TemperatureConverter() { super("Temperature Converter"); // Create the components celsiusLabel = new JLabel("Celsius:"); fahrenheitLabel = new JLabel("Fahrenheit:"); celsiusField = new JTextField(10); fahrenheitField = new JTextField(10); convertCelsiusToFahrenheitButton = new JButton("Convert C to F"); convertFahrenheitToCelsiusButton = new JButton("Convert F to C"); // Add action listeners to the buttons convertCelsiusToFahrenheitButton.addActionListener(this); convertFahrenheitToCelsiusButton.addActionListener(this); // Create a panel for the input fields JPanel inputPanel = new JPanel(new GridLayout(2, 2)); inputPanel.add(celsiusLabel); inputPanel.add(celsiusField); inputPanel.add(fahrenheitLabel); inputPanel.add(fahrenheitField); // Create a panel for the buttons JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(convertCelsiusToFahrenheitButton); buttonPanel.add(convertFahrenheitToCelsiusButton); // Add the panels to the frame add(inputPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); // Set the frame properties setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 120); setLocationRelativeTo(null); // Center the window setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == convertCelsiusToFahrenheitButton) { double celsius = Double.parseDouble(celsiusField.getText()); double fahrenheit = celsius * 9 / 5 + 32; fahrenheitField.setText(String.format("%.2f", fahrenheit)); } else if (e.getSource() == convertFahrenheitToCelsiusButton) { double fahrenheit = Double.parseDouble(fahrenheitField.getText()); double celsius = (fahrenheit - 32) * 5 / 9; celsiusField.setText(String.format("%.2f", celsius)); } } public static void main(String[] args) { new TemperatureConverter(); } } ``` 运行程序后,将会看到一个包含两个文本输入框和两个按钮的窗口。在输入框中输入摄氏度或华氏度,然后单击相应的按钮,即可将温度转换为另一种单位并显示在另一个文本输入框中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值