//HTML
创新互联建站2013年开创至今,先为武乡等服务建站,武乡等地企业,进行企业商务咨询服务。为武乡企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
html
applet code=SZJSQ.class width=400 height=180
/applet
/html
//APPLET
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class SZJSQ extends JApplet implements ActionListener
{
boolean i=true;
private JButton num0=new JButton("0");
private JButton num1=new JButton("1");
private JButton num2=new JButton("2");
private JButton num3=new JButton("3");
private JButton num4=new JButton("4");
private JButton num5=new JButton("5");
private JButton num6=new JButton("6");
private JButton num7=new JButton("7");
private JButton num8=new JButton("8");
private JButton num9=new JButton("9");
private JButton zuok=new JButton("(");
private JButton youk=new JButton(")");
private JButton dian=new JButton(".");
private JButton NULL=new JButton("N");
private JButton plu=new JButton("+");
private JButton min=new JButton("-");
private JButton mul=new JButton("x");
private JButton div=new JButton("/");
private JButton equ=new JButton("=");
private JButton cle=new JButton("C");//清除
private JTextField space=new JTextField(30);
public void init()
{
JPanel text=new JPanel();
text.setLayout(new FlowLayout());
text.add(space);
JPanel buttons=new JPanel();
buttons.setLayout(new GridLayout(5,4));
buttons.add(num9);
buttons.add(num8);
buttons.add(num7);
buttons.add(plu);
buttons.add(num6);
buttons.add(num5);
buttons.add(num4);
buttons.add(min);
buttons.add(num3);
buttons.add(num2);
buttons.add(num1);
buttons.add(mul);
buttons.add(num0);
buttons.add(cle);
buttons.add(equ);
buttons.add(div);
buttons.add(zuok);
buttons.add(youk);
buttons.add(dian);
buttons.add(NULL);
(num9).addActionListener(this);
(num8).addActionListener(this);
(num7).addActionListener(this);
(num6).addActionListener(this);
(num5).addActionListener(this);
(num4).addActionListener(this);
(num3).addActionListener(this);
(num2).addActionListener(this);
(num1).addActionListener(this);
(num0).addActionListener(this);
(plu).addActionListener(this);
(min).addActionListener(this);
(mul).addActionListener(this);
(div).addActionListener(this);
(equ).addActionListener(this);
(cle).addActionListener(this);
(zuok).addActionListener(this);
(youk).addActionListener(this);
(dian).addActionListener(this);
setLayout(new BorderLayout());
add("North",text);
add("South",buttons);
space.setText("0");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==num9)
{
if(i==true)
{
space.setText("9");
i=false;
}
else space.setText(space.getText()+'9');
}
if(e.getSource()==num8)
{
if(i==true)
{
space.setText("8");
i=false;
}
else space.setText(space.getText()+'8');
}
if(e.getSource()==num7)
{
if(i==true)
{
space.setText("7");
i=false;
}
else space.setText(space.getText()+'7');
}
if(e.getSource()==num6)
{
if(i==true)
{
space.setText("6");
i=false;
}
else space.setText(space.getText()+'6');
}
if(e.getSource()==num5)
{
if(i==true)
{
space.setText("5");
i=false;
}
else space.setText(space.getText()+'5');
}
if(e.getSource()==num4)
{
if(i==true)
{
space.setText("4");
i=false;
}
else space.setText(space.getText()+'4');
}
if(e.getSource()==num3)
{
if(i==true)
{
space.setText("3");
i=false;
}
else space.setText(space.getText()+'3');
}
if(e.getSource()==num2)
{
if(i==true)
{
space.setText("2");
i=false;
}
else space.setText(space.getText()+'2');
}
if(e.getSource()==num1)
{
if(i==true)
{
space.setText("1");
i=false;
}
else space.setText(space.getText()+'1');
}
if(e.getSource()==num0)
{
if(i==true)
{
space.setText("0");
i=false;
}
else space.setText(space.getText()+'0');
}
if(e.getSource()==zuok)
{
if(i==true)
{
space.setText("(");
i=false;
}
else space.setText(space.getText()+'(');
} if(e.getSource()==youk)
{
if(i==false)
space.setText(space.getText()+')');
}
if(e.getSource()==dian)
{
if(i==false)
space.setText(space.getText()+'.');
}
if(e.getSource()==plu)
{
space.setText(space.getText()+'+');
i=false;
}
if(e.getSource()==min)
{
space.setText(space.getText()+'-');
i=false;
}
if(e.getSource()==mul)
{
space.setText(space.getText()+'*');
i=false;
}
if(e.getSource()==div)
{
space.setText(space.getText()+'/');
i=false;
}
if(e.getSource()==equ)
{
space.setText(String.valueOf(Calculator(space.getText())));
i=true;
}
if(e.getSource()==cle)
{
space.setText("0");
i=true;
}
}
public double Calculator(String f)//科学计算
{
int i=0,j=0,k;
char c;
StringBuffer s=new StringBuffer();
s.append(f);
s.append('=');
String formula=s.toString();
char[] anArray;
anArray=new char[50];
StackCharacter mystack=new StackCharacter();
while(formula.charAt(i)!='=')
{
c=formula.charAt(i);
switch(c)
{
case '(': mystack.push(new Character(c));
i++;
break;
case ')':
while(mystack.peek().charValue()!='(')
{
anArray[j++]=mystack.pop().charValue();
}
mystack.pop();
i++;
break;
case '+':
case '-':
while(!mystack.empty()mystack.peek().charValue()!='(')
{
anArray[j++]=mystack.pop().charValue();
}
mystack.push(new Character(c));
i++;
break;
case '*':
case '/':
while(!mystack.empty()(mystack.peek().charValue()=='*'||mystack.peek().charValue()=='/'))
{
anArray[j++]=mystack.pop().charValue();
}
mystack.push(new Character(c));
i++;
break;
case' ': i++;
break;
default: while((c='0'c='9')||c=='.')
{
anArray[j++]=c;
i++;
c=formula.charAt(i);
}
anArray[j++]='#';
break;
}
}
while(!(mystack.empty()))
anArray[j++]=mystack.pop().charValue();
i=0;
int count;
double a,b,d;
StackDouble mystack1 =new StackDouble();
while(ij)
{
c=anArray[i];
switch(c)
{
case '+':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b+a;
mystack1.push(new Double(d));
i++;
break;
case '-':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b-a;
mystack1.push(new Double(d));
i++;
break;
case '*':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b*a;
mystack1.push(new Double(d));
i++;
break;
case '/':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
if(a!=0)
{
d=b/a;
mystack1.push(new Double(d));
i++;
}
else
{
System.out.println("Error!");
}
break;
default:
d=0;count=0;
while((c='0'c='9'))
{
d=10*d+c-'0';
i++;
c=anArray[i];
}
if(c=='.')
{
i++;
c=anArray[i];
while((c='0'c='9'))
{
count++;
d=d+(c-'0')/Math.pow(10,count);
i++;
c=anArray[i];
}
}
if(c=='#')
mystack1.push(new Double(d));
i++;
break;
}
}
return(mystack1.peek().doubleValue());
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
class CalculatorPanel extends JPanel {
private JPanel jp; // 中间的面板
private double result; // 计算结果
private boolean start; // 用于判断是否是首次输入,true首次,false不是首次
private boolean flag; // 用于判断是否清空显示区域的值,true需要,false不需要
private String lastCommand; // 用于保存运算符
JButton display; // 显示区域
public CalculatorPanel() {
// 初始化各项值
result = 0;
start = true;
flag = false;
lastCommand = "=";
// 设置CalculatorPanel这个面板的布局为框架布局
setLayout(new BorderLayout());
// 这个按钮用来显示值的区域
display = new JButton("0.0");
display.setEnabled(false);// 按钮样式设置为禁用样式
// 用来初始化,清除用的
JButton clear = new JButton("clear");
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 作初始化的操作
result = 0;
start = true;
flag = false;
lastCommand = "=";
display.setText("0.0");
}
});
jp = new JPanel();
// 将此面板布局设置为网格布局,4行4列
jp.setLayout(new GridLayout(4, 4));
// 实例化监听器对象
NumberAction na = new NumberAction();
CommandAction ca = new CommandAction();
makeButton("7", na);
makeButton("8", na);
makeButton("9", na);
makeButton("/", ca);
makeButton("4", na);
makeButton("5", na);
makeButton("6", na);
makeButton("*", ca);
makeButton("1", na);
makeButton("2", na);
makeButton("3", na);
makeButton("-", ca);
makeButton("0", na);
makeButton(".", na);
makeButton("+", ca);
makeButton("=", ca);
// 将面板,两个按键加入到我的面板,实现计算器的界面
add(display, BorderLayout.NORTH);
add(jp, BorderLayout.CENTER);
add(clear, BorderLayout.SOUTH);
}
private void makeButton(String buttonName, ActionListener al) {
JButton jb = new JButton(buttonName);
jp.add(jb);
jb.addActionListener(al);
}
// 数字监听器
private class NumberAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource(); // 获取事件源对象
String input = jb.getText(); // 获取事件源对象上的标签
if (start) { // 首次输入
// 一上就".",就什么也不做
if (input.equals(".")) {
return;
}
// 如果是"0.0"的话,就清空
if (display.getText().equals("0.0")) {
display.setText("");
}
start = false;// 改变是否首次输入的标记值
} else {
// 判断显示区域的值里面是否已经有".",如果有,输入的又是".",就什么都不做
if (display.getText().indexOf(".") != -1) {
if (input.equals(".")) {
return;
}
}
// 判断显示区域的值里面只有"-",输入的又是".",就什么都不做
if (display.getText().equals("-")) {
if (input.equals(".")) {
return;
}
}
// 判断显示区域的值如果是"0",输入的不是".",就什么也不做
if (display.getText().equals("0")) {
if (!input.equals(".")) {
return;
}
}
}
// 如果我点击了运算符以后,再输入数字的话,就要清空显示区域的值
if (flag) {
display.setText("");
flag = false;// 还原初始值,不需要清空
}
display.setText(display.getText() + input);// 设置显示区域的值
}
}
// 运算符监听器
private class CommandAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource(); // 获取事件源对象
String inputCommand = jb.getText(); // 获取事件源对象上的标签
if (start) {// 首次输入
// 一上就是"-"
if (inputCommand.equals("-")) {
display.setText("-");// 显示区域的内容设置为"-"
start = false;// 改变首次输入的标记
}
} else {
if (!flag) {// 如果flag=false不需要清空显示区的值,就调用方法计算
calculate(Double.parseDouble(display.getText()));// 保存显示区域的值,并计算
}
// 保存你点击的运算符
lastCommand = inputCommand;
flag = true;// 因为我这里已经输入过运算符,
}
}
}
// 计算用的方法
private void calculate(double x) {
if (lastCommand.equals("+")) {
result += x;
} else if (lastCommand.equals("-")) {
result -= x;
} else if (lastCommand.equals("*")) {
result *= x;
} else if (lastCommand.equals("/")) {
result /= x;
} else if (lastCommand.equals("=")) {
result = x;
}
display.setText("" + result);
}
}
class MyFrameUitl {
public static void init(JFrame jFrame, Dimension frameSize, String title,
String iconFileName, boolean resizable) {
Toolkit tk = Toolkit.getDefaultToolkit(); // 系统工具包,可以读取系统信息
Dimension screenSize = tk.getScreenSize();// 获取屏幕大小
// 获取宽高
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
// 找中心点
int centerX = screenWidth / 2;
int centerY = screenHeight / 2;
// 解析窗体大小
int frameWidth = frameSize.width;
int frameHeight = frameSize.height;
// 设置窗体位置
jFrame.setBounds(centerX - frameWidth / 2, centerY - frameHeight / 2,
frameWidth, frameHeight);
jFrame.setTitle(title);// 设置标题
if (iconFileName != null) {
jFrame.setIconImage(tk.getImage(iconFileName));// 设置图标
}
// 设置皮肤com.sun.java.swing.plaf.windows.WindowsLookAndFeel
/**/
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(jFrame);
} catch (Exception e1) {
e1.printStackTrace();
}
jFrame.setResizable(resizable);// 设置窗体是否可以改变大小
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置程序关闭动作
jFrame.setVisible(true);// 显示窗体
}
}
class CalculatorFrame extends JFrame {
public CalculatorFrame() {
add(new CalculatorPanel());
MyFrameUitl.init(this, new Dimension(400, 300), "简易计算器", null,
false);
this.pack();// 让组件按原始大小显示,并且窗体的大小刚好能够容纳所有的组件
}
}
public class CalculatorTest {
public static void main(String[] args) {
new CalculatorFrame();
}
}
运行结果:
package jisuanqi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener{ //建一个Calculator类继承JFrame实现ActionListener
public static void main (String args[]){
Calculator calculator=new Calculator(); //创建对象calculator
calculator.setVisible(true); //窗口可见
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭图标后结束程序
}
String keys []={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//表示计算器的键的名字
String command []={"backspace","C"};//功能键
JButton KEYS []=new JButton[keys.length];//创建键对象
JButton COMMAND[]=new JButton[command.length];//功能键对象
static JTextField resultSet=new JTextField();//建一个文本框
double resultNum; //结果
String operator="";//运算符 ---------------------------
public Calculator() {// 构造函数
super();
init();//初始化计算器
this.setBackground(Color.WHITE);//this代表用当前构造方法创建的对象
this.setTitle("简单计算器"); //窗口标题
this.setLocation(500, 300); //窗口大小
this.pack(); //使组件大小合适
}
void init() { //初始化计算器
resultSet.setHorizontalAlignment(JTextField.RIGHT); //设置文本框内容向右对齐
resultSet.setBackground(Color.WHITE); //设置文本框背景颜色
JPanel keysPanel=new JPanel();//创建一个面板
keysPanel.setLayout(new GridLayout(4,5,3,3)); //设置面板的布局为网格型
for (int i=0;ikeys.length;i++) { //把数字键和符号键设为按钮
KEYS[i]=new JButton(keys[i]);
keysPanel.add(KEYS[i]); //把按钮加到面板中
}
JPanel comPanel=new JPanel(); //创建命令键面板
comPanel.setLayout(new GridLayout(1,2,3,3));//设置面板布局为网格型
for(int i=0;icommand.length;i++) { //把命令键设置为按钮
COMMAND[i]=new JButton(command[i]);
comPanel.add(COMMAND[i]); //把按钮放到面板上
}
JPanel pane=new JPanel(); //创建面板
pane.setLayout(new BorderLayout()); //面板布局为区域型
pane.add(comPanel,BorderLayout.NORTH); //把命令面板加入到大面板上
pane.add(keysPanel,BorderLayout.CENTER); //把数字键面板加到大面板
getContentPane().setLayout(new BorderLayout()); //设置底层容器的布局为区域型
getContentPane().add(resultSet,BorderLayout.NORTH); //把文本框放到北面
getContentPane().add(pane,BorderLayout.CENTER); //把大面板放到中央
for(int i=0;ikeys.length;i++){
KEYS[i].addActionListener(this);//为按钮添加监听器
}
for(int i=0;icommand.length;i++){
COMMAND[i].addActionListener(this);
}
}
//处理事件
public void actionPerformed(ActionEvent e){
String lable=e.getActionCommand();
if(lable.equals("backspace")){ //如果命令为backspace
backspace();
}
else if (lable.equals("C")){ //如果命令为C
Clear();
}
else if (Character.isDigit(lable.charAt(0))) { //---------------
if(resultSet.getText().equals("0")){
resultSet.setText("");
}
resultSet.setText(resultSet.getText()+lable);
}
else if (lable.equals(".")){
point();
}
else if (lable.equals("=")){
equal();
}
else {
doOperation();
resultSet.setText("");
operator=lable;
}
}
void point() {
String s=resultSet.getText();
if(s.indexOf(".")==-1){
resultSet.setText(s+".");
}
}
void equal() {
doOperation();
resultSet.setText(""+resultNum);
operator="";
}
void doOperation() {
double m=Double.parseDouble(resultSet.getText());
if(operator.equals("")){
resultNum=m;
}
if(operator.equals("+")){
resultNum+=m;
}
if(operator.equals("-")){
resultNum-=m;
}
if(operator.equals("*")){
resultNum*=m;
}
if(operator.equals("/")){
resultNum/=m;
}
}
void Clear() { //清零
resultSet.setText("0");
}
void backspace() { //退格
String text =resultSet.getText();
if (text.length()0){
resultSet.setText(text.substring(0, text.length()-1));//将文本最后一个字符删除
}
else if (text.length()==0){
resultSet.setText("0");
}
}
}
.class文件是java编译后的文件,它不是源代码,真正的java源代码是.java文件。
java源代码是txt格式的.java文件,用记事本就可以打开。
用eclipse打开java文件的方式是:
如果java文件是一个eclipse工程(根目录带有.project文件),用file/import/general/exist java project/(大概是)然后找到你的目录。
否则需要自己新建一个工程file/new/java project
然后把java文件拷贝到.src目录下。
.class文件是直接的编译好的文件,可以用jad把.class文件反编译成java文件,不过反编译的代码和原来的代码不一定完全一样。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;public class CaculatorA {
private JFrame jf;
private JButton[] jbs;
private JTextField jtf;
private JButton clear;
private double num1,num2,jieguo;
private char c;
/**
* 构造方法实例化属性
*
*/
public CaculatorA(){
jf=new JFrame("我的计算器v1.0");
jtf=new JTextField(20);
clear=new JButton("clear");
jbs=new JButton[16];
String str="123+456-789*0./=";
for(int i=0; istr.length(); i++){
jbs[i]=new JButton(str.charAt(i)+"");
}
init();
addEventHandler();
// setFont();
// setColor();
showMe();
}
/**
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class JPanelEa1 extends JFrame implements MenuListener,ActionListener
{
JPanel mainp,p1,p2,p3,p4;
JTextField jt1;
JMenuItem mnuCopy;
JMenuItem mnuPaste;
JButton bM;
boolean isDouble = false;// 是否为实数
int opFlag = -1;
static double t1 = 0, t2 = 0, t3 = 0, result = 0;
static int opflag1 = -1, opflag2 = -1, flag = 0, resflag = 1;
int preOp, currentOp = 0;// 标准位
double op1 = 0, op2 = 0;// 操作数
double n3;
StringBuffer buf = new StringBuffer(20);
StringBuffer copyBoard = new StringBuffer(20);// 剪贴板
StringBuffer memory = new StringBuffer(20);// M系列
StringBuffer str = new StringBuffer();
public JPanelEa1()
{
p1=new JPanel();
p1.setLayout(new GridLayout(2,1,10,10));
JMenuBar mnuNotepad=new JMenuBar();
JMenu mnuEdit=new JMenu("编辑(E)");
mnuEdit.setMnemonic(KeyEvent.VK_E);
JMenu mnuCheck=new JMenu("查看(V)");
mnuCheck.setMnemonic(KeyEvent.VK_V);
JMenu mnuHelp=new JMenu("帮助(H)");
mnuCopy=new JMenuItem("复制(C)");
mnuPaste=new JMenuItem("粘贴(P)");
JMenuItem mnuVisit=new JMenuItem("查看帮助(V)");
JMenuItem mnuAbout=new JMenuItem("关于计算器(A)");
JSeparator sep=new JSeparator();
jt1=new JTextField("0.");
jt1.setEnabled(false);
jt1.setHorizontalAlignment(JTextField.RIGHT);
mnuEdit.addMenuListener(this);
mnuCheck.addMenuListener(this);
mnuHelp.addMenuListener(this);
mnuCopy.addActionListener(this);
mnuPaste.addActionListener(this);
mnuVisit.addActionListener(this);
mnuAbout.addActionListener(this);
mnuNotepad.add(mnuEdit);
mnuNotepad.add(mnuCheck);
mnuNotepad.add(mnuHelp);
mnuEdit.add(mnuCopy);
mnuEdit.add(mnuPaste);
mnuEdit.add(sep);
mnuHelp.add(mnuVisit);
mnuHelp.add(sep);
mnuHelp.add(mnuAbout);
p1.add(mnuNotepad);
p1.add(jt1);
p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
bM = new JButton(" ");
bM.setEnabled(false);
p2.add(bM);
Button b25=new Button("Backspace");
b25.addActionListener(this);
Button b26=new Button("CE");
b26.addActionListener(this);
Button b27=new Button("C");
b27.addActionListener(this);
p2.add(b25);
p2.add(b26);
p2.add(b27);
p3=new JPanel();
p3.setLayout(new GridLayout(4,6));
Button button1=new Button("MC");
button1.addActionListener(this);
Button button2=new Button("7");
button2.addActionListener(this);
Button button3=new Button("8");
button3.addActionListener(this);
Button button4=new Button("9");
button4.addActionListener(this);
Button button5=new Button("/");
button5.addActionListener(this);
Button button6=new Button("sqrt");
button6.addActionListener(this);
Button button7=new Button("MR");
button7.addActionListener(this);
Button button8=new Button("4");
button8.addActionListener(this);
Button button9=new Button("5");
button9.addActionListener(this);
Button button10=new Button("6");
button10.addActionListener(this);
Button button11=new Button("*");
button11.addActionListener(this);
Button button12=new Button("%");
button12.addActionListener(this);
Button button13=new Button("MS");
button13.addActionListener(this);
Button button14=new Button("1");
button14.addActionListener(this);
Button button15=new Button("2");
button15.addActionListener(this);
Button button16=new Button("3");
button16.addActionListener(this);
Button button17=new Button("-");
button17.addActionListener(this);
Button button18=new Button("1/x");
button18.addActionListener(this);
Button button19=new Button("M+");
button19.addActionListener(this);
Button button20=new Button("0");
button20.addActionListener(this);
Button button21=new Button("+/-");
button21.addActionListener(this);
Button button22=new Button(".");
button22.addActionListener(this);
Button button23=new Button("+");
button23.addActionListener(this);
Button button24=new Button("=");
button24.addActionListener(this);
p3.add(button1);p3.add(button2);
p3.add(button3);p3.add(button4);
p3.add(button5);p3.add(button6);
p3.add(button7);p3.add(button8);
p3.add(button9);p3.add(button10);
p3.add(button11);p3.add(button12);
p3.add(button13);p3.add(button14);
p3.add(button15);p3.add(button16);
p3.add(button17);p3.add(button18);
p3.add(button19);p3.add(button20);
p3.add(button21);p3.add(button22);
p3.add(button23);p3.add(button24);
setSize(250,150);
mainp=new JPanel();
mainp.setLayout(new GridLayout(3,1));
mainp.add(p1);
mainp.add(p2);
mainp.add(p3);
setContentPane(mainp);
setTitle("计算器");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s= e.getActionCommand();
if (s.equals("复制(C)")) {
String temp = jt1.getText().trim();
copyBoard.replace(0, copyBoard.length(), temp);
mnuPaste.setEnabled(true);
} else if (s.equals("粘贴(p)")) {
jt1.setText(copyBoard.toString());
} else if (s.equals("CE")) {
// 如果是CE则清除文本框
jt1.setText("0.");
} else if (s.equals("Backspace")) {
if (!jt1.getText().trim().equals("0.")) {
// 如果文本框中有内容
if (str.length() != 1 str.length() != 0) {
jt1.setText(str.delete(str.length() - 1, str.length())
.toString());
} else {
jt1.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(jt1.getText().trim());
} else if (s.equals("C")) {
// 如果是C删除当前计算
jt1.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
} else if (s.equals("MC")) {
// 如果是MC则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
} else if (s.equals("MR")) {
// 如果按键为MR则恢复缓冲区的数到文本框
jt1.setText(memory.toString());
} else if (s.equals("MS")) {
// 如果按键为MS则将文本框的数存入缓冲区
String s1 = jt1.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
} else if (s.equals("M+")) {
// 如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
String temp1 = jt1.getText().trim();
double dtemp = Double.parseDouble(temp1);
String temp2 = memory.toString();
dtemp += Double.parseDouble(temp2);
temp1 = String.valueOf(dtemp);
memory.replace(0, memory.length(), temp1);
} else if (s.equals("1/x")) {
// 如果按键为1/x则将文本框中的数据为它的倒数
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + 1 / dtemp);
} else if (s.equals("sqrt")) {
// 如果按键为sqrt则将文本框中的内容求平方根
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + Math.sqrt(dtemp));
} else if (s.equals("+")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 1;
op2 = 0;
jt1.setText("" + op1);
} else {
currentOp = preOp;
preOp = 1;
switch (currentOp) {
case 1:
op1 += op2;
jt1.setText("" + op1);
break;
case 2:
op1 -= op2;
jt1.setText("" + op1);
break;
case 3:
op1 *= op2;
jt1.setText("" + op1);
break;
case 4:
op1 /= op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("-")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 2;// op1=op2;op2=0;
jt1.setText("" + op1);
} else {
currentOp = preOp;
preOp = 2;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("*"))// *
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 3;// op1=op2;op2=1;
jt1.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 3;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("/"))// /
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 4;// op2=1;
jt1.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 4;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("="))// =
{
if (currentOp == 0) {
str.setLength(0);
jt1.setText("" + op2);
} else {
str.setLength(0);
currentOp = preOp;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
currentOp = 0;
op2 = 0;
}
} else if (s.equals(".")) {
isDouble = true;
if (jt1.getText().trim().indexOf('.') != -1)
;
else {
if (jt1.getText().trim().equals("0")) {
str.setLength(0);
jt1.setText((str.append("0" + s)).toString());
}
// else
// if(jt1.getText().trim().equals("")){}//如果初时显示为空则不做任何操作
else {
jt1.setText((str.append(s)).toString());
}
}
} else if (s.equals("0"))// 如果选择的是"0"这个数字键
{
if (jt1.getText().trim().equals("0.")) {
} else {
jt1.setText(str.append(s).toString());
op2 = Double.parseDouble(jt1.getText().trim());
}
} else {
jt1.setText(str.append(s).toString());
op2 = Double.parseDouble(jt1.getText().trim());
if (currentOp == 0)
op1 = op2;
}
}
public static void main(String args[])
{
JPanelEa1 g=new JPanelEa1();
}
@Override
public void menuSelected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuDeselected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuCanceled(MenuEvent e) {
// TODO Auto-generated method stub
}
}