⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myconverter.java

📁 本人写的一个非常简单的度量单位换算程序
💻 JAVA
字号:
/* * MyConverter.java * * Created on 03/11/2004 */import java.text.DecimalFormat;import java.text.ParseException;/** * * @author  Ben Lin */public class MyConverter extends javax.swing.JFrame {        /** Creates new form MyConverter */    public MyConverter() {        conversionFactor = 1.609;        initComponents();    }        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    private void initComponents() {//GEN-BEGIN:initComponents        myButtonGroup = new javax.swing.ButtonGroup();        textInput = new javax.swing.JFormattedTextField(new DecimalFormat("###.##"));        try {            textInput.setText("0.0");            textInput.commitEdit();        } catch(ParseException e) {            //Shouldn't get here unless the setText value doesn't agree            //with the format set above.            e.printStackTrace();        }        Convert = new javax.swing.JButton();        Input = new javax.swing.JLabel();        Output = new javax.swing.JLabel();        Result = new javax.swing.JLabel();        Menubar = new javax.swing.JMenuBar();        File = new javax.swing.JMenu();        Exit = new javax.swing.JMenuItem();        Mode = new javax.swing.JMenu();        Distance = new javax.swing.JRadioButtonMenuItem();        Volumn = new javax.swing.JRadioButtonMenuItem();        Weight = new javax.swing.JRadioButtonMenuItem();        Help = new javax.swing.JMenu();        About = new javax.swing.JMenuItem();        getContentPane().setLayout(null);        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setTitle("My Converter");        setName("frameMainWindow");        setResizable(false);        textInput.setToolTipText("Enter the value you want to convert heref");        getContentPane().add(textInput);        textInput.setBounds(170, 70, 140, 23);        Convert.setFont(new java.awt.Font("Times New Roman", 0, 12));        Convert.setMnemonic('c');        Convert.setText("Convert");        Convert.setToolTipText("Click this button to convert");        Convert.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                ConvertActionPerformed(evt);            }        });        getContentPane().add(Convert);        Convert.setBounds(160, 180, 81, 23);        Input.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);        Input.setText("Miles:");        getContentPane().add(Input);        Input.setBounds(70, 70, 60, 20);        Output.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);        Output.setText("Kms:");        getContentPane().add(Output);        Output.setBounds(70, 110, 60, 20);        getContentPane().add(Result);        Result.setBounds(170, 110, 140, 20);        Menubar.setFont(new java.awt.Font("Times New Roman", 0, 12));        Menubar.setName("null");        File.setMnemonic('f');        File.setText("File");        Exit.setFont(new java.awt.Font("Times New Roman", 0, 12));        Exit.setMnemonic('e');        Exit.setText("Exit");        Exit.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                ExitActionPerformed(evt);            }        });        File.add(Exit);        Menubar.add(File);        Mode.setMnemonic('m');        Mode.setText("Mode");        myButtonGroup.add(Distance);        Distance.setFont(new java.awt.Font("Times New Roman", 0, 12));        Distance.setMnemonic('d');        Distance.setSelected(true);        Distance.setText("Miles to Kms");        Distance.setToolTipText("Convert miles to kms");        Distance.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                DistanceActionPerformed(evt);            }        });        Mode.add(Distance);        myButtonGroup.add(Volumn);        Volumn.setFont(new java.awt.Font("Times New Roman", 0, 12));        Volumn.setMnemonic('v');        Volumn.setText("Pints to Litres");        Volumn.setToolTipText("Convert pints to litres");        Volumn.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                VolumnActionPerformed(evt);            }        });        Mode.add(Volumn);        myButtonGroup.add(Weight);        Weight.setFont(new java.awt.Font("Times New Roman", 0, 12));        Weight.setMnemonic('w');        Weight.setText("Lbs to Kgs");        Weight.setToolTipText("Convert pounds to kgs");        Weight.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                WeightActionPerformed(evt);            }        });        Mode.add(Weight);        Menubar.add(Mode);        Help.setMnemonic('h');        Help.setText("Help");        About.setFont(new java.awt.Font("Times New Roman", 0, 12));        About.setMnemonic('a');        About.setText("About");        About.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                AboutActionPerformed(evt);            }        });        Help.add(About);        Menubar.add(Help);        setJMenuBar(Menubar);        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);    }//GEN-END:initComponents    private void AboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AboutActionPerformed        About dlgAbout = new About(this, true);        dlgAbout.setVisible(true);     // TODO add your handling code here:    }//GEN-LAST:event_AboutActionPerformed    private void ConvertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConvertActionPerformed        double inputValue;	    double outputValue;	        inputValue = Double.parseDouble(textInput.getText());        outputValue = conversionFactor * inputValue;	    Result.setText(String.valueOf(outputValue));        // TODO add your handling code here:    }//GEN-LAST:event_ConvertActionPerformed    private void VolumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_VolumnActionPerformed        Input.setText("Pint:");        Output.setText("Litres:");        conversionFactor = 0.4732;        // TODO add your handling code here:    }//GEN-LAST:event_VolumnActionPerformed    private void DistanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DistanceActionPerformed        Input.setText("Miles:");        Output.setText("Kms:");        conversionFactor = 1.609;        // TODO add your handling code here:    }//GEN-LAST:event_DistanceActionPerformed    private void WeightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_WeightActionPerformed        Input.setText("Lbs:");        Output.setText("Kgs:");        conversionFactor = 0.4536;        // TODO add your handling code here:    }//GEN-LAST:event_WeightActionPerformed    private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitActionPerformed        exitApplication();        // TODO add your handling code here:    }//GEN-LAST:event_ExitActionPerformed        public void exitApplication(){	setVisible(false);	dispose();	System.exit(0);		    }        /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new MyConverter().setVisible(true);            }        });    }        // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JMenuItem About;    private javax.swing.JButton Convert;    private javax.swing.JRadioButtonMenuItem Distance;    private javax.swing.JMenuItem Exit;    private javax.swing.JMenu File;    private javax.swing.JMenu Help;    private javax.swing.JLabel Input;    private javax.swing.JMenuBar Menubar;    private javax.swing.JMenu Mode;    private javax.swing.JLabel Output;    private javax.swing.JLabel Result;    private javax.swing.JRadioButtonMenuItem Volumn;    private javax.swing.JRadioButtonMenuItem Weight;    private javax.swing.ButtonGroup myButtonGroup;    private javax.swing.JFormattedTextField textInput;    // End of variables declaration//GEN-END:variables        private double conversionFactor;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -