当前位置:编程学习 > JAVA >>

求JAVA编写的计算器

要写好窗体的。谢谢

答案:import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
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;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Calculator extends WindowAdapter implements ActionListener {
 JFrame list;
 JTextField show;
 JButton bc, c, ce, ab, jia, jian, cheng, chu, equ, point, sqrt, ds, bfh,
   zf;
 // 按钮 退格,清空,复位,关于,加,减,乘,除,等号,小数点,2次方根,倒数,百分号,正负号
 JButton b[] = new JButton[10]; // 按钮数组,数字键0~9
 double sum = 0, getValue;
 int i = 0, j = 0, p = 0, l, action;//新数,连续运数,小数点,0~9,四则运算
 JDialog about;
 final int slength = 30; // 设置结果显示有效长度
 public void disp() {
  list = new JFrame("简易计算器");
  list.setSize(360, 230);
  list.setLocation(380, 260);
  list.setBackground(Color.LIGHT_GRAY);
  list.setLayout(new FlowLayout(FlowLayout.CENTER));
  list.setResizable(false);
  show = new JTextField(31);
  show.setText("0");
  show.setHorizontalAlignment(SwingConstants.RIGHT);
  show.setEditable(false);
  list.add(show);
  Panel dispTop = new Panel();
  list.add(dispTop);
  dispTop.setLayout(new GridLayout(1, 4, 3, 3));
  bc = new JButton("  Back    ");
  bc.setForeground(Color.BLUE);
  dispTop.add(bc);
  ce = new JButton("  CE   ");
  ce.setForeground(Color.BLUE);
  dispTop.add(ce);
  c = new JButton("  C   ");
  c.setForeground(Color.BLUE);
  dispTop.add(c);
  ab = new JButton("   About   ");
  ab.setForeground(Color.BLUE);
  dispTop.add(ab);
  about = new JDialog(list, "关于计算器", true);
Label ct = new Label(" http://blog.csdn.net/wibnmo", 1);
  ct.setForeground(Color.RED);
  about.add(ct, "Center");
  about.setSize(200, 100);
  about.setLocation(500, 300);
  about.addWindowListener(this);
  Panel dispMain = new Panel();
  list.add(dispMain);
  dispMain.setLayout(new GridLayout(1, 2, 10, 10));
  Panel dispLeft = new Panel();
  dispMain.add(dispLeft);
  dispLeft.setLayout(new GridLayout(4, 3, 3, 3));
  Panel dispRight = new Panel();
  dispMain.add(dispRight);
  dispRight.setLayout(new GridLayout(4, 2, 3, 3));
  for (l = 9; l >= 0; l--) {
   b[l] = new JButton(String.valueOf(l));
   dispLeft.add(b[l]);
   b[l].addActionListener(this);
  }
  jia = new JButton("+");
  jia.setForeground(Color.RED);
  jian = new JButton("-");
  jian.setForeground(Color.RED);
  cheng = new JButton("*");
  cheng.setForeground(Color.RED);
  chu = new JButton("/");
  chu.setForeground(Color.RED);
  equ = new JButton("=");
  equ.setForeground(Color.RED);
  point = new JButton(".");
  zf = new JButton(" +/- ");
  sqrt = new JButton("sqrt");
  bfh = new JButton("%");
  ds = new JButton("1/x");
  dispRight.add(chu);
  dispRight.add(sqrt);
  dispRight.add(cheng);
  dispRight.add(bfh);
  dispRight.add(jian);
  dispRight.add(ds);
  dispRight.add(jia);
  dispRight.add(equ);
  dispLeft.add(zf);
  dispLeft.add(point);
  bc.addActionListener(this);
  ce.addActionListener(this);
  c.addActionListener(this);
  ab.addActionListener(this);
  jia.addActionListener(this);
  jian.addActionListener(this);
  cheng.addActionListener(this);
  chu.addActionListener(this);
  equ.addActionListener(this);
  point.addActionListener(this);
  zf.addActionListener(this);
  sqrt.addActionListener(this);
  bfh.addActionListener(this);
  ds.addActionListener(this);
  list.addWindowListener(this);
  list.setVisible(true);
 }
 public void actionPerformed(ActionEvent e) {
  getValue = Double.valueOf(show.getText()).doubleValue();
  if (e.getSource() == jia) { // 加运算,可连加
   if (j == 0) {
    sum = getValue;
   } else if (action == 1) {
    sum += getValue;
   }
   setSum();
   j++;
   p = 0;
   i = 0;
   action = 1;
  } else if (e.getSource() == jian) { // 减运算,可连减
   if (j == 0) {
    sum = getValue;
   } else if (action == 2) {
    sum -= getValue;
   }
   setSum();
   j++;
   p = 0;
   i = 0;
   action = 2;
  } else if (e.getSource() == cheng) { // 乘运算,可连乘
   if (j == 0) {
    sum = getValue;
   } else if (action == 3) {
    sum *= getValue;
   }
   setSum();
   j++;
   p = 0;
   i = 0;
   action = 3;
  } else if (e.getSource() == chu) { // 除运算,可连除
   if (j == 0)
    sum = getValue;
   else if (action == 4) {
    sum /= getValue;
   }
   setSum();
   j++;
   p = 0;
   i = 0;
   action = 4;
  } else if (e.getSource() == equ) { // 等号,运算最后一个操作数
   switch (action) {
   case 1:
    show.setText(String.valueOf(sum += getValue));
    break;
   case 2:
    show.setText(String.valueOf(sum -= getValue));
    break;
   case 3:
    show.setText(String.valueOf(sum *= getValue));
    break;
   case 4:
    show.setText(String.valueOf(sum /= getValue));
    break;
   }
   setSum();
   i = 0;
   j = 0;
   action = 0;
  } else if (e.getSource() == point) { // 小数点,只能按一个小数点
   if (p == 0)
    show.setText(show.getText() + e.getActionCommand());
   p = 1;
  } else if (e.getSource() == c || e.getSource() == ce) { // 清空与复位
   i = 0;
   j = 0;
   p = 0;
   sum = 0;
   action = 0;
  

上一个:java中j2ee标准是什么?
下一个:数组——JAVA程序设计

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,