63685deb92d7a352fbc2bae374d805c5.zip
立即下载
资源介绍:
63685deb92d7a352fbc2bae374d805c5.zip
package a;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
private String currentNumber = "";
private int currentOperator = 0;
JTextField txtUser =new JTextField();
public Calculator()
{
this.setTitle("计算器");
this.setSize(400, 260);
JLabel bianji =new JLabel("编辑");
JLabel chakong1 =new JLabel("");
JLabel chakan =new JLabel("查看");
JLabel bangzhu =new JLabel("帮助");
JPanel panInput =new JPanel();
panInput.setLayout(new GridLayout(1,3));
panInput.add(bianji);
panInput.add(chakan);
panInput.add(bangzhu);
JButton kong = new JButton("");
JButton MC = new JButton("MC");
JButton MR = new JButton("MR");
JButton MS = new JButton("MS");
JButton Mjia = new JButton("M+");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton chu = new JButton("/");
JButton cheng = new JButton("*");
JButton jian = new JButton("-");
JButton jia = new JButton("+");
JButton deng = new JButton("=");
JButton sqrt = new JButton("sqrt");
JButton baifen =new JButton ("%");
JButton daoshu = new JButton("1/x");
JButton CE = new JButton("CE");
JButton C = new JButton("C");
JButton dian = new JButton(".");
JButton delete = new JButton("backspace");
JButton x= new JButton("+/-");
kong.addActionListener(this);
MC.addActionListener(this);
MR.addActionListener(this);
MS.addActionListener(this);
Mjia.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
chu.addActionListener(this);
cheng.addActionListener(this);
jian.addActionListener(this);
jia.addActionListener(this);
deng.addActionListener(this);
sqrt.addActionListener(this);
baifen.addActionListener(this);
daoshu.addActionListener(this);
CE.addActionListener(this);
C.addActionListener(this);
dian.addActionListener(this);
delete.addActionListener(this);
x.addActionListener(this);
JPanel panButton3 = new JPanel();
panButton3.setLayout(new GridLayout(1,3));
JPanel panButton1 = new JPanel();
panButton1.setLayout(new GridLayout(4,6));
panButton1.add(MC);
panButton1.add(seven);
panButton1.add(eight);
panButton1.add(nine);
panButton1.add(chu);
panButton1.add(sqrt);
panButton1.add(MR);
panButton1.add(four);
panButton1.add(five);
panButton1.add(six);
panButton1.add(cheng);
panButton1.add(baifen);
panButton1.add(MS);
panButton1.add(one);
panButton1.add(two);
panButton1.add(three);
panButton1.add(jian);
panButton1.add(daoshu);
panButton1.add(Mjia);
panButton1.add(zero);
panButton1.add(dian);
panButton1.add(x);
panButton1.add(jia);
panButton1.add(deng);
this.add(panButton1,BorderLayout.SOUTH);
JPanel panButton2 = new JPanel();
panButton2.setLayout(new GridLayout(1,4));
panButton2.add(kong);
panButton2.add(delete);
panButton2.add(CE);
panButton2.add(C);
JPanel a =new JPanel();
a.setLayout(new GridLayout(4,1));
a.add(panInput);
a.add(txtUser);
a.add(panButton3);
a.add(panButton2);
this.add(a,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("1"))
{
txtUser.setText("1");
}
else if(e.getActionCommand().equals("2"))
{
txtUser.setText("2");
}
else if(e.getActionCommand().equals("3"))
{
txtUser.setText("3");
}
else if(e.getActionCommand().equals("4"))
{
txtUser.setText("4");
}
else if(e.getActionCommand().equals("5"))
{
txtUser.setText("5");
}
else if(e.getActionCommand().equals("6"))
{
txtUser.setText("6");
}
else if(e.getActionCommand().equals("7"))
{
txtUser.setText("7");
}
else if(e.getActionCommand().equals("8"))
{
txtUser.setText("8");
}
else if(e.getActionCommand().equals("9"))
{
txtUser.setText("9");
}
else if(e.getActionCommand().equals("0"))
{
txtUser.setText("0");
}
else if(e.getActionCommand().equals("+"))
{
txtUser.setText("+");
}
else if(e.getActionCommand().equals("-"))
{
txtUser.setText("-");
}
else if(e.getActionCommand().equals("/"))
{
txtUser.setText("/");
}
else if(e.getActionCommand().equals("*"))
{
txtUser.setText("*");
}
if (e.getSource() instanceof JButton)
{
JButton button = (JButton) e.getSource();
String buttonText = button.getText();
if (buttonText.equals("1") || buttonText.equals("2") || buttonText.equals("3") || buttonText.equals("4") ||
buttonText.equals("5") || buttonText.equals("6") || buttonText.equals("7") || buttonText.equals("8") ||
buttonText.equals("9") || buttonText.equals("0"))
{
if (currentOperator == 0)
{
currentNumber += buttonText;
}
else
{
currentNumber = currentNumber.concat(buttonText);
}
}
else if (buttonText.equals("/"))
{
currentOperator = 3;
}
else if (buttonText.equals("*"))
{
currentOperator = 2;
}
else if (buttonText.equals("-"))
{
currentOperator = 1;
}
else if (buttonText.equals("+"))
{
currentOperator = 0;
}
else if (buttonText.equals("="))
{
if (currentOperator == 0)
{
double num1 = Double.parseDouble(currentNumber.substring(0, currentNumber.length() - 1));
double num2 = Double.parseDouble(currentNumber.substring(currentNumber.length() - 1));
double result = num1 + num2;
txtUser.setText(String.format("%.2f"+"+"+"%.2f"+"="+"%.2f", num1,num2, result));
}
else if (currentOperator == 1)