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

2019-04-14 16:38发布

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