//HTML
創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,先為武鄉(xiāng)等服務(wù)建站,武鄉(xiāng)等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為武鄉(xiāng)企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
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)//科學(xué)計算
{
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;?//?計算結(jié)果
private?boolean?start;?//?用于判斷是否是首次輸入,true首次,false不是首次
private?boolean?flag;?//?用于判斷是否清空顯示區(qū)域的值,true需要,false不需要
private?String?lastCommand;?//?用于保存運算符
JButton?display;?//?顯示區(qū)域
public?CalculatorPanel()?{
//?初始化各項值
result?=?0;
start?=?true;
flag?=?false;
lastCommand?=?"=";
//?設(shè)置CalculatorPanel這個面板的布局為框架布局
setLayout(new?BorderLayout());
//?這個按鈕用來顯示值的區(qū)域
display?=?new?JButton("0.0");
display.setEnabled(false);//?按鈕樣式設(shè)置為禁用樣式
//?用來初始化,清除用的
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();
//?將此面板布局設(shè)置為網(wǎng)格布局,4行4列
jp.setLayout(new?GridLayout(4,?4));
//?實例化監(jiān)聽器對象
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);
//?將面板,兩個按鍵加入到我的面板,實現(xiàn)計算器的界面
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);
}
//?數(shù)字監(jiān)聽器
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?{
//?判斷顯示區(qū)域的值里面是否已經(jīng)有".",如果有,輸入的又是".",就什么都不做
if?(display.getText().indexOf(".")?!=?-1)?{
if?(input.equals("."))?{
return;
}
}
//?判斷顯示區(qū)域的值里面只有"-",輸入的又是".",就什么都不做
if?(display.getText().equals("-"))?{
if?(input.equals("."))?{
return;
}
}
//?判斷顯示區(qū)域的值如果是"0",輸入的不是".",就什么也不做
if?(display.getText().equals("0"))?{
if?(!input.equals("."))?{
return;
}
}
}
//?如果我點擊了運算符以后,再輸入數(shù)字的話,就要清空顯示區(qū)域的值
if?(flag)?{
display.setText("");
flag?=?false;//?還原初始值,不需要清空
}
display.setText(display.getText()?+?input);//?設(shè)置顯示區(qū)域的值
}
}
//?運算符監(jiān)聽器
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("-");//?顯示區(qū)域的內(nèi)容設(shè)置為"-"
start?=?false;//?改變首次輸入的標記
}
}?else?{
if?(!flag)?{//?如果flag=false不需要清空顯示區(qū)的值,就調(diào)用方法計算
calculate(Double.parseDouble(display.getText()));//?保存顯示區(qū)域的值,并計算
}
//?保存你點擊的運算符
lastCommand?=?inputCommand;
flag?=?true;//?因為我這里已經(jīng)輸入過運算符,
}
}
}
//?計算用的方法
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();?//?系統(tǒng)工具包,可以讀取系統(tǒng)信息
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;
//?設(shè)置窗體位置
jFrame.setBounds(centerX?-?frameWidth?/?2,?centerY?-?frameHeight?/?2,
frameWidth,?frameHeight);
jFrame.setTitle(title);//?設(shè)置標題
if?(iconFileName?!=?null)?{
jFrame.setIconImage(tk.getImage(iconFileName));//?設(shè)置圖標
}
//?設(shè)置皮膚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);//?設(shè)置窗體是否可以改變大小
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//?設(shè)置程序關(guān)閉動作
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();
}
}
運行結(jié)果:
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實現(xiàn)ActionListener
public?static?void?main?(String?args[]){
Calculator?calculator=new?Calculator();?????//創(chuàng)建對象calculator
calculator.setVisible(true);???????????????//窗口可見
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??//關(guān)閉圖標后結(jié)束程序
}
String?keys?[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//表示計算器的鍵的名字
String?command?[]={"backspace","C"};//功能鍵
JButton?KEYS?[]=new?JButton[keys.length];//創(chuàng)建鍵對象
JButton?COMMAND[]=new?JButton[command.length];//功能鍵對象
static?JTextField?resultSet=new?JTextField();//建一個文本框
double?resultNum;????????//結(jié)果
String?operator="";//運算符??---------------------------
public?Calculator()?{//????構(gòu)造函數(shù)
super();??????
init();//初始化計算器
this.setBackground(Color.WHITE);//this代表用當(dāng)前構(gòu)造方法創(chuàng)建的對象
this.setTitle("簡單計算器");????//窗口標題
this.setLocation(500,?300);???//窗口大小
this.pack(); //使組件大小合適
}
void?init()?{??????//初始化計算器
resultSet.setHorizontalAlignment(JTextField.RIGHT);?????//設(shè)置文本框內(nèi)容向右對齊
resultSet.setBackground(Color.WHITE);???????????//設(shè)置文本框背景顏色
JPanel?keysPanel=new?JPanel();//創(chuàng)建一個面板
keysPanel.setLayout(new?GridLayout(4,5,3,3));?//設(shè)置面板的布局為網(wǎng)格型
for?(int?i=0;ikeys.length;i++)?{??????//把數(shù)字鍵和符號鍵設(shè)為按鈕
KEYS[i]=new?JButton(keys[i]);
keysPanel.add(KEYS[i]);????????????//把按鈕加到面板中
}
JPanel?comPanel=new?JPanel();???????//創(chuàng)建命令鍵面板
comPanel.setLayout(new?GridLayout(1,2,3,3));//設(shè)置面板布局為網(wǎng)格型?
for(int?i=0;icommand.length;i++)?{????//把命令鍵設(shè)置為按鈕
COMMAND[i]=new?JButton(command[i]);
comPanel.add(COMMAND[i]);??????????//把按鈕放到面板上
}
JPanel?pane=new?JPanel();??????//創(chuàng)建面板
pane.setLayout(new?BorderLayout());??//面板布局為區(qū)域型
pane.add(comPanel,BorderLayout.NORTH);?????//把命令面板加入到大面板上
pane.add(keysPanel,BorderLayout.CENTER); //把數(shù)字鍵面板加到大面板
getContentPane().setLayout(new?BorderLayout());?????//設(shè)置底層容器的布局為區(qū)域型
getContentPane().add(resultSet,BorderLayout.NORTH);???//把文本框放到北面
getContentPane().add(pane,BorderLayout.CENTER);????//把大面板放到中央
for(int?i=0;ikeys.length;i++){
KEYS[i].addActionListener(this);//為按鈕添加監(jiān)聽器
}
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;
/**
* 構(gòu)造方法實例化屬性
*
*/
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;// 是否為實數(shù)
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;// 操作數(shù)
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("復(fù)制(C)");
mnuPaste=new JMenuItem("粘貼(P)");
JMenuItem mnuVisit=new JMenuItem("查看幫助(V)");
JMenuItem mnuAbout=new JMenuItem("關(guān)于計算器(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("復(fù)制(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.")) {
// 如果文本框中有內(nèi)容
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刪除當(dāng)前計算
jt1.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
} else if (s.equals("MC")) {
// 如果是MC則清除緩沖區(qū)
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
} else if (s.equals("MR")) {
// 如果按鍵為MR則恢復(fù)緩沖區(qū)的數(shù)到文本框
jt1.setText(memory.toString());
} else if (s.equals("MS")) {
// 如果按鍵為MS則將文本框的數(shù)存入緩沖區(qū)
String s1 = jt1.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
} else if (s.equals("M+")) {
// 如果按鍵為MS則將文本框值與緩沖區(qū)的數(shù)相加但不顯示結(jié)果
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則將文本框中的數(shù)據(jù)為它的倒數(shù)
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + 1 / dtemp);
} else if (s.equals("sqrt")) {
// 如果按鍵為sqrt則將文本框中的內(nèi)容求平方根
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"這個數(shù)字鍵
{
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
}
}