java 简易计算器Swing


package com.itsoo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * 计算器界面
 */
public class GuiCalculator  extends JFrame implements ActionListener{
    JScrollPane jscro;
    JMenuBar bar ;
    JMenuItem jmb,jmb1,jmb2,jmb3;
    JMenu jm,jm1,jm2;
    JTextField Jtext;
    JPanel list,list2,list3;
    JTextArea tl;        //定义文本框
    JButton bc,   c,   ce, ad,  jia,jian,cheng, chu, equ, point,  sqrt,   ds,    bfh , zf;
    //按钮  退格 清空 复位 关于 加 减 乘 除        等于 小数点  2次方根 倒数 百分号 正负号
    double sum = 0 ,getVlues;
    int  i = 0, j = 0,p = 0 ,l,action;
    GuiCalculator(){
        //菜单
        bar = new JMenuBar();
        jmb = new JMenuItem("历史记录");
        jmb1 = new JMenuItem("复制(C)");
        jmb2 = new JMenuItem("粘贴(V)");
        jmb3 = new JMenuItem("关于(A)");
        tl = new JTextArea();
        tl.setRows(4);
        tl.setText("");
        tl.setColumns(24);
       tl.setEditable(true);
        jscro = new JScrollPane(tl);
        list = new JPanel();
        list2 = new JPanel();
        list3 = new JPanel();
        jm = new JMenu("菜单(M)");
        jm1 = new JMenu("编辑(E)");
        jm2 = new JMenu("帮助(H)");
        //初始化函数
        bc = new JButton("Clear");
        c = new JButton("C");
        ce = new JButton("CE");
        ad = new JButton("AB");
        jia =  new JButton("+");
        jian = new JButton("-");
        cheng = new JButton("X");
        chu = new JButton("÷");
        equ = new JButton("=");
        point = new JButton(".");
        sqrt =new JButton("1/X");
        ds = new JButton("DS");
        bfh = new JButton("%");
        zf = new  JButton("±");
        //数字按钮按键
        JButton b0 = new JButton("0");
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        //界面按钮设置
        jm1.add(jmb1);
        jm1.add(jmb2);
        jm1.add(jmb);
        jm2.add(jmb3);
        bar.add(jm);
        bar.add(jm1);
        bar.add(jm2);
        list.add(bc);
        list.add(c);
        list.add(ce);
        list.add(ad);
        list.add(ds);
        list.add(bfh);
        list.add(sqrt);
        list.add(zf);
        list.add(b7);
        list.add(b8);
        list.add(b9);
        list.add(chu);
        list.add(b4);
        list.add(b5);
        list.add(b6);
        list.add(cheng);
        list.add(b3);
        list.add(b2);
        list.add(b1);
        list.add(jian);
        list.add(b0);
        list.add(point);
        list.add(equ);
        list.add(jia);
        setFont(new Font("TimeRoman",Font.PLAIN,6));
        list.setLayout(new GridLayout(6,4,3,3));
        list2.add(jscro,BorderLayout.NORTH);
        list2.add(list,BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setJMenuBar(bar);
        this.setTitle("计算器");
        this.setVisible(true);
        this.setResizable(false);
        this.setSize(280,300);
        //this.add(list);
        this.add(list2);
        setPreferredSize(new Dimension(100,50));
        jmb3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Guanyu gi = new Guanyu();
            }
        });
    }
        //监听按钮事件
        public void actionPerformed(ActionEvent e) {

      }
     public static void main(String[]args){
        GuiCalculator gi = new  GuiCalculator();
    }
}
    class Guanyu extends JFrame{
       public  Guanyu(){
        JPanel  jp = new JPanel();
        JTextArea jtxt = new JTextArea();
        //读取关于
        jtxt.setText("这是一个简易的计算器");
        jtxt.setRows(30);
        jtxt.setEditable(false);
        jp.add(jtxt);
        this.setTitle("关于");
        this.setResizable(false);
        this.setVisible(true);
        this.add(jp);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setSize(300,200);

    }
}

你可能感兴趣的:(java 简易计算器Swing)