java啊

来源:互联网 发布:linux c 视频教程 编辑:程序博客网 时间:2024/06/05 03:22

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author student
 */

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.*;

public class Calculator  extends JFrame implements ActionListener {
    private JButton [] p1,p2;
    private JTextField display,result;   //文本框
    private JMenu menu1,menu2,menu3;    // 菜单
    private JMenuBar menubar;
    private JMenuItem item1,item2,item3;  //条目
    private String [] labels1 = {"1","2","3","4","5","6","7","8","9","0",".","=","Mod","CE","Help","About"};  //计算器按键
    private String [] labels2 = {"+","-","*","/"};
    private float number1,end;   // 数据存储
    private boolean flag = false;       //标志器
    static private int op;            // 运算符记录器
    private int year1,year2,month1,month2,day1,day2;  // 年月日记录器
    private int day = 0;        // 相差的天数

    public Calculator(){
        display = new JTextField("Arithmetic");  // 将display文本框的初始值设为Arithmetic
        result = new JTextField(20);
        result.setHorizontalAlignment(JTextField.RIGHT);  //将result文本框内的格式设为右靠齐

   //对界面进行布局
        JPanel JP1 = new JPanel();
        JP1.setLayout(new GridLayout(2,1));
        this.add(JP1,BorderLayout.NORTH);
        JP1.add(display);
        JP1.add(result);
        JPanel JP2 = new JPanel();
        JP2.setLayout(new GridLayout(4,3));
        this.add(JP2,BorderLayout.CENTER);
        JPanel JP3 = new JPanel();
        JP3.setLayout(new GridLayout(4,1));
        this.add(JP3,BorderLayout.WEST);

        menubar = new JMenuBar();
        menu1 = new JMenu("文件");
        menubar.add(menu1);
        menu2 = new JMenu("实数计算");
        menu2.addActionListener(this);
        menubar.add(menu2);

        menu3 = new JMenu("日期计算");
        menubar.add(menu3);
        item1 = new JMenuItem("退出");
        item1.addActionListener(this);
        item2  = new JMenuItem("Date");
        menu1.add(item1);
        item2.addActionListener(this);
        menu3.add(item2);
        item3 = new JMenuItem("实数计算");
        menu2.add(item3);
        item3.addActionListener(this);
        setJMenuBar(menubar);

        p1 = new JButton[16];
        p2 = new JButton[4];
        for(int i=0; i<labels1.length; i++){
            p1[i] = new JButton(labels1[i]);
            JP2.add(p1[i]);
            p1[i].addActionListener(this);
        }

        for (int j=0; j<labels2.length; j++){
            p2[j] = new JButton(labels2[j]);
            JP3.add(p2[j]);
            p2[j].addActionListener(this);
        }

        this.setBounds(60,60,400,350);
        this.setVisible(true);

     this.addWindowListener(new WindowAdapter(){      //设置关闭对话框
     public void windowClosing(WindowEvent e){
     if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Calculator.this,"确定要关闭程序吗?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){
         System.exit(0);
          }
        }
      });
    }

     public void actionPerformed(ActionEvent e) {

         //实数计算
         if(flag == false){
            display.setText("Arithmetic");
           for(int i=0; i<labels1.length-5; i++){
               if(e.getSource() == p1[i]){
            result.setText(result.getText()+e.getActionCommand());
               }
            }

            for(int j=0; j<p2.length; j++){
                if(e.getSource() == p2[j]){
                    op = j;
                    end = Float.parseFloat(result.getText());
                     result.setText(null);
                }

            }

            if(e.getSource() == p1[11]){   //进行相应的运算
                number1 = Float.parseFloat(result.getText());
            switch(op){
                case 0:
                    end = end + number1;result.setText(""+end);break;
                case 1:
                    end =end - number1;result.setText(""+end);break;
                case 2:
                    end = end * number1;result.setText(""+end);break;
                case 3:
                    end = end / number1;result.setText(""+end);break;
                case 4:
                    end = (end % number1); result.setText(""+end);break;
              }
            }

            if(e.getActionCommand() == "Mod"){    //进行取余时的相应操作
                op = 4;
                end = Float.parseFloat(result.getText());
                     result.setText(null);
            }


            if(e.getSource() == p1[13]){    //计算器清零
                result.setText(null);
                end = 0;
                number1 = 0;
            }

            if(e.getSource() == item1){   //退出选项
           JFrame ff = new JFrame();
           String ss = "你确定要退出吗?";
           boolean bb = true;
            new MyDialog(ff,ss,bb);  //调用MyDialog对话框    
                
            }
         }

         if(e.getSource() == item3){
             flag = false;
             display.setText("Arithmetic");
         }

         if(e.getSource() == p1[15]){             //弹出关于本计算器的相关信息
             Object[] options = { "OK", "CANCEL" };
        JOptionPane.showOptionDialog(null, "计算器--《Java课程设计》课程设计程序/n版权归该作者^_^ 蹇阳 ^_^所有", "关于",
        JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
        null, options, options[0]);

         }

//日期计算

         if(e.getSource() == item2 || e.getSource() == p1[14]){   //调用帮助界面

         JFrame ff = new JFrame();
         String ss = "帮助";
         boolean bb = true;
         display.setText("Date");
         flag = true;
         new Help(ff,ss,bb);
         }
         if(flag == true){
                     for(int i=0; i<=9; i++){
                         if(e.getSource() == p1[i])
                         result.setText(result.getText() + e.getActionCommand());
                     }

         if(e.getActionCommand() == "/" || e.getActionCommand() == "-")
             result.setText(result.getText()+ e.getActionCommand());

                     if(e.getActionCommand() == "CE") {//清零
                         result.setText(null);
                         day = 0;
                 }
                     if(e.getActionCommand() == "="){   //计算相差的天数

                         String st0 = result.getText();
                         int n0 = st0.indexOf("-");
                         String st1 = st0.substring(0, n0);  //第一个年月日
                         String st2 = st0.substring(n0+1);   //第二个年月日

                         int n11 = st1.indexOf("/");
                         year1 = Integer.parseInt(st1.substring(0, n11)); // 第一个年
                         int n21 = st2.indexOf("/");
                         year2 = Integer.parseInt(st2.substring(0, n21)); // 第二个年

                         String st11 = st1.substring(n11+1);            //第一个月
                         int n12 = st11.indexOf("/");
                         month1 = Integer.parseInt(st11.substring(0, n12));

                         String st21 = st2.substring(n21+1);    //第二个月
                         int n22 = st21.indexOf("/");
                         month2 = Integer.parseInt(st21.substring(0, n22));


                         String st12 = st11.substring(n12+1);    //第一个日
                         int n13 = st11.indexOf("/");
                        day1 = Integer.parseInt(st12.substring(n13-1));


                        String st22 = st21.substring(n22+1);         //第二个日
                        int n23 = st21.indexOf("/");
                        day2 = Integer.parseInt(st22.substring(n23-1));

                         String t1 = st1.replace('/', '-'); //将所有的'/'替换为'-'
                         String t2 = st2.replace('/', '-');

                        Days d = new Days();  //调用Days中的getDays方法

                        try{
                         day  = d.getDays(t1, t2);

                        } catch(Exception ee){};
                    //输出
                         result.setText(year1+"年"+ month1 +"月"+  day1+"日"+"  与  " +
                                 year2 +"年" +month2 + "月" + day2 +"日之间" +
                                 "  相差  " + day +" 天");
         }
      }
  }
 //主函数
     public static void main(String[] args){
         new Calculator();   //程序入口
     }
}
 


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author student
 */
public class Days{
    Date begin,end;
    long time0,time1;
    public int getDays(String begin_day, String end_day) throws ParseException{
       
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        try{
         begin = df.parse(begin_day);
         end = df.parse(end_day);  
         time0=begin.getTime();
         time1=end.getTime();
        }catch(NumberFormatException e){
            JOptionPane.showMessageDialog(null, "格式错误", "你输入的日期有误", JOptionPane.ERROR_MESSAGE);
        };
         return  (int) Math.abs((time1 - time0)/(1000*60*60*24));
    }
}
 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Lenovo
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.*;
public class Help extends JFrame implements ActionListener{
   private  JButton b1;
   private JTextArea ta;
   private JPanel panel;
   public Help(JFrame f, String s, boolean b){
      //super(f,s,b);
     this.setLayout(new FlowLayout());
     b1= new JButton("取消");
     ta = new JTextArea(5,5);
     ta.setText("本计算器的功能是计算减乘除/n取余和两个日期之间相差的天数,/n" +
             "程序规定的日期格式是yyyy/mm/dd,例如/n2001/02/5," +
             "其他任何形式的输入格式都是不正确的");
     this.add(ta,"North");
     this.add(b1,"Center");
     b1.addActionListener(this);
     panel = new JPanel();
     this.add(panel);
     panel.setLayout(new BorderLayout());
     panel.add(b1,BorderLayout.EAST);
     setVisible(true);
     setBounds(100,100,300,180);

   this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
        dispose();
         //System.exit(0);
     }
    });
   }

    public void actionPerformed(ActionEvent e) {
               if(e.getSource() == b1){
                   setVisible(false);
               }
    }

}
 


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Lenovo
 */
public class MyDialog extends JDialog implements ActionListener{
    JButton b1,b2;
    public MyDialog (JFrame f, String s,boolean b){
         super(f,s,b);
        setLayout(new GridLayout(1,2));
         b1 = new JButton("yes");
         b2 = new JButton("no");
        add(b1);
        add(b2);
        b1.addActionListener(this);
        b2.addActionListener(this);
        setBounds(100,90,200,60);
        this.setVisible(true);

        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() ==b1){
            System.exit(0);
        }
        else if(e.getSource()==b2){
            setVisible(false);
        }

    }

}

 

原创粉丝点击