Java学习笔记(4) 项目搭建实例

来源:互联网 发布:中小学生教育软件 编辑:程序博客网 时间:2024/05/08 04:59

一个小的图形界面程序,进行摄氏度和华氏度的转换。还有输入空的异常抛出问题未解决,过后再修改。

1.Temp.java – –算法部分:温度类的创建,转换公式

interface Temp{     void calcTemp();    double output();}class CTemp implements Temp{    private double cTemp, fTemp;    public CTemp(double c){        cTemp = c;    }    public void calcTemp(){        fTemp = cTemp*1.8+32;    }    public double output(){        return fTemp;    }}class FTemp implements Temp{    private double cTemp, fTemp;    public FTemp(double f){        fTemp = f;    }    public void calcTemp(){        cTemp = (fTemp-32)/1.8;    }    public double output(){        return cTemp;    }}

2.MyException.java – – 自定义的异常类,从抽象的异常类中继承

public class MyException extends Exception{    private String message;    //存    public MyException(String m){        message=m;    }    //取    public String outMessage(){        return message;    }}

3.MyFrame.java – – 图形界面

import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class MyFrame extends JFrame implements ActionListener{    //面板    JPanel pnlMain=new JPanel();    //标签    JLabel lblInputc=new JLabel("请输入摄氏温度:");    JLabel lblInputf=new JLabel("请输入华氏温度:");    JLabel lblOutputc=new JLabel("转换为摄氏温度:");    JLabel lblOutputf=new JLabel("转换为华氏温度:");    JLabel lblError=new JLabel("错误信息:");    //文本框(参数:列宽)    JTextField txtInputc=new JTextField(10);    JTextField txtInputf=new JTextField(10);    JTextField txtResultc=new JTextField(10);    JTextField txtResultf=new JTextField(10);    JTextField txtError=new JTextField(10);     //按钮    JButton btnCalc=new JButton("计算");    JButton btnCancle=new JButton("取消");        //新建MyException的对象,传入参数    MyException mex=new MyException("输入温度越界");    //构造函数    public MyFrame(){        //设置面板布局(网格布局:6行2列,行、列间距)        pnlMain.setLayout(new GridLayout(6,2,5,5));        pnlMain.add(lblInputc);        pnlMain.add(lblInputf);        pnlMain.add(txtInputc);        pnlMain.add(txtInputf);        pnlMain.add(lblOutputf);        pnlMain.add(lblOutputc);        pnlMain.add(txtResultf);        pnlMain.add(txtResultc);        pnlMain.add(lblError);        pnlMain.add(txtError);        pnlMain.add(btnCalc);        pnlMain.add(btnCancle);        //在按钮上加监听器,检测自己是否被点击        btnCalc.addActionListener(this);        btnCancle.addActionListener(this);        //把面板放到界面上        setContentPane(pnlMain);    }       //获取文本输入    //异常方法:抛出自己    public String inputTextc() throws MyException{        String str=new String();        str=txtInputc.getText();        if(Double.parseDouble(str)<-273.15){            throw mex;        }        return str;    }    public String inputTextf() throws MyException{        String str=new String();        str=txtInputf.getText();        if(Double.parseDouble(str)<-459.67){            throw mex;        }        return str;    }    //构造函数之后加入事件方法,包括抽象方法(ActionListener监听器中)的实现    //抛出异常:数字格式异常 throws NumberFormatException    public void actionPerformed(ActionEvent ae){        //调用inputText        try{            if(ae.getSource()==btnCalc){                //捕获文本框内容并转换为double类型                CTemp cobj=new CTemp(Double.parseDouble(inputTextc()));                cobj.calcTemp();                    //将计算结果转换为字符串并输出,""+ :强制转换                txtResultf.setText(""+cobj.output());                FTemp fobj=new FTemp(Double.parseDouble(inputTextf()));                fobj.calcTemp();                txtResultc.setText(""+fobj.output());                txtError.setText("");            }else if(ae.getSource()==btnCancle){                //取消:清空文本框                txtInputc.setText("");                txtInputf.setText("");                txtResultc.setText("");                txtResultf.setText("");                txtError.setText("");            }        }catch(MyException me){            txtError.setText("异常:"+ me.outMessage());        }catch(NumberFormatException nfe){            txtError.setText("数字格式异常:"+ nfe.getMessage());        }catch(Exception e){            txtError.setText("异常:"+ e.getMessage());        }    }}

4.MainProg.java – – 主程序

import java.lang.*;import java.io.*;import java.util.*;import javax.swing.*;public class MainProg{    public static void main(String[] args) throws IOException{        MyFrame mf=new MyFrame();        //标题        mf.setTitle("温度转换");        //大小        mf.setSize(400,200);        //关闭        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //可见:显示界面        mf.setVisible(true);    }}
0 0