用JAVA写的小工具(空心线圈电感量计算)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.math.*;
public class Inductance {
	static JTextField resultTextField;
	static JTextField rTextField;
	static JTextField lTextField;
	static JTextField nTextField;
	public static void main(String[] args) {
		JFrame frame = new JFrame("空心线圈电感量");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		frame.add(panel);
		Box mainBox = Box.createVerticalBox();
		panel.add(mainBox);
		//线圈的半径R
		Box rBox = Box.createHorizontalBox();
		mainBox.add(rBox);
		JLabel rLabel = new JLabel("半径(单位米):");
		rTextField = new JTextField(5);
		rBox.add(rLabel);
		rBox.add(rTextField);
		//线圈的长度L
		Box lBox = Box.createHorizontalBox();
		mainBox.add(lBox);
		JLabel lLabel = new JLabel("长度(单位米):");
		lTextField = new JTextField(5);
		lBox.add(lLabel);
		lBox.add(lTextField);
		//线圈的圈数N
		Box nBox = Box.createHorizontalBox();
		mainBox.add(nBox);
		JLabel nLabel = new JLabel("圈数(单位圈):");
		nTextField = new JTextField(5);
		nBox.add(nLabel);
		nBox.add(nTextField);
		//真空磁导率μ0,默认为4π*10^(-7)
		Box μ0Box = Box.createHorizontalBox();
		mainBox.add(μ0Box);
		JLabel μ0Label = new JLabel("真空磁导率μ0");
		JTextField μ0TextField = new JTextField(5);
		μ0TextField.setText("4π*10^(-7)");
		μ0TextField.enable(false);
		μ0Box.add(μ0Label);
		μ0Box.add(μ0TextField);
		//线圈内部磁芯的相对磁导率μs,空心线圈时μs=1
		Box μsBox = Box.createHorizontalBox();
		mainBox.add(μsBox);
		JLabel μsLabel = new JLabel("相对磁导率μs");
		JTextField μsTextField = new JTextField(5);
		μsTextField.setText("1");
		μsTextField.enable(false);
		μsBox.add(μsLabel);
		μsBox.add(μsTextField);
		//空心线圈电感量计算的结果
		Box resultBox = Box.createHorizontalBox();
		mainBox.add(resultBox);
		JLabel resultLabel = new JLabel("线圈电感量(单位微亨)");
		resultTextField = new JTextField(5);
		resultTextField.enable(false);
		resultBox.add(resultLabel);
		resultBox.add(resultTextField);
		//计算按键
		Box lastBox = Box.createHorizontalBox();
		mainBox.add(lastBox);
		JButton calButton = new JButton("计算");
		JLabel edwardLabel = new JLabel("徐方鑫制作");
		lastBox.add(calButton);
		lastBox.add(edwardLabel);
		calButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				double r = 0,l = 0;
				float n = 0,k = 0,rl = 0;
				double pi=3.14,result=0;
				String resultString;
				try{
				r=Double.parseDouble(rTextField.getText());
				l=Double.parseDouble(lTextField.getText());
				n=(float) Double.parseDouble(nTextField.getText());
				rl=(float)(2*r/l);
				if(0<rl&rl<0.1) k=0.96f;
				else if(0.1<rl&rl<0.2) k=0.92f;
				else if(0.2<rl&rl<0.3) k=0.88f;
				else if(0.3<rl&rl<0.4) k=0.85f;
				else if(0.4<rl&rl<0.6) k=0.79f;
				else if(0.6<rl&rl<0.8) k=0.74f;
				else if(0.8<rl&rl<1.0) k=0.69f;
				else if(1.0<rl&rl<1.5) k=0.60f;
				else if(1.5<rl&rl<2.0) k=0.52f;
				else if(2.0<rl&rl<3.0) k=0.43f;
				else if(3.0<rl&rl<4.0) k=0.37f;
				else if(4.0<rl&rl<5.0) k=0.32f;
				else if(5.0<rl&rl<10.0) k=0.20f;
				else if(10.0<rl&rl<20.0) k=0.12f;
				else throw new IOException();
				result = k*4*pi*(Math.pow(10,-7))*1*(Math.pow(n,2))*pi*(Math.pow(r,2))/l;
				result = result*(Math.pow(10, 6));
				}catch(IOException ex)
				{
					JOptionPane.showMessageDialog(null, "半径与长度填写有误");
				}catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "请填写完整数据");
				}
				resultString=String.valueOf(String.format("%.2f",result));
				System.out.println(result);
				System.out.println(resultString);
				resultTextField.setText(resultString);
			}
		});	
		frame.setSize(250,220);
		frame.setVisible(true);
	}

}

你可能感兴趣的:(java)