/*001*/ import java.awt.*;
/*002*/ import java.awt.event.*;
/*003*/ import java.util.EventObject;
/*004*/
/*005*/ public class EasyCalculator{
/*006*/ private Frame f;
/*007*/ private Panel p1,p2;
/*008*/ private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
/*009*/ private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;
/*010*/ private TextField tf;
/*011*/ private String s,op;
/*012*/ private Calculator cal = new Calculator();
/*013*/ private boolean ifOp;
/*014*/
/*015*/ public EasyCalculator(){
/*016*/ f = new Frame("Calculator");
/*017*/ p1 = new Panel();
/*018*/ p2 = new Panel();
/*019*/
/*020*/ b0 = new Button("0");
/*021*/ b1 = new Button("1");
/*022*/ b2 = new Button("2");
/*023*/ b3 = new Button("3");
/*024*/ b4 = new Button("4");
/*025*/ b5 = new Button("5");
/*026*/ b6 = new Button("6");
/*027*/ b7 = new Button("7");
/*028*/ b8 = new Button("8");
/*029*/ b9 = new Button("9");
/*030*/ bPoint = new Button(".");
/*031*/ bAdd = new Button("+");
/*032*/ bDec = new Button("-");
/*033*/ bMul = new Button("*");
/*034*/ bDiv = new Button("/");
/*035*/ bCal = new Button("=");
/*036*/
/*037*/ tf = new TextField(25);
/*038*/ tf.setEditable(false);
/*039*/
/*040*/
/*041*/ }
/*042*/
/*043*/ public void launchFrame(){
/*044*/ f.setSize(220,160);
/*045*/ f.setResizable(false);
/*046*/ f.addWindowListener(new myWindowListener());
/*047*/ p1.setLayout(new FlowLayout(FlowLayout.CENTER));
/*048*/ p1.add(tf);
/*049*/ f.add(p1,BorderLayout.NORTH);
/*050*/ p2.setLayout(new GridLayout(4,4));
/*051*/
/*052*/ b0.addActionListener(new setLabelText_ActionListener());
/*053*/ b1.addActionListener(new setLabelText_ActionListener());
/*054*/ b2.addActionListener(new setLabelText_ActionListener());
/*055*/ b3.addActionListener(new setLabelText_ActionListener());
/*056*/ b4.addActionListener(new setLabelText_ActionListener());
/*057*/ b5.addActionListener(new setLabelText_ActionListener());
/*058*/ b6.addActionListener(new setLabelText_ActionListener());
/*059*/ b7.addActionListener(new setLabelText_ActionListener());
/*060*/ b8.addActionListener(new setLabelText_ActionListener());
/*061*/ b9.addActionListener(new setLabelText_ActionListener());
/*062*/ bPoint.addActionListener(new setLabelText_ActionListener());
/*063*/ bAdd.addActionListener(new setOperator_ActionListener());
/*064*/ bDec.addActionListener(new setOperator_ActionListener());
/*065*/ bMul.addActionListener(new setOperator_ActionListener());
/*066*/ bDiv.addActionListener(new setOperator_ActionListener());
/*067*/ bCal.addActionListener(new setOperator_ActionListener());
/*068*/
/*069*/ p2.add(b7);
/*070*/ p2.add(b8);
/*071*/ p2.add(b9);
/*072*/ p2.add(bAdd);
/*073*/ p2.add(b4);
/*074*/ p2.add(b5);
/*075*/ p2.add(b6);
/*076*/ p2.add(bDec);
/*077*/ p2.add(b1);
/*078*/ p2.add(b2);
/*079*/ p2.add(b3);
/*080*/ p2.add(bMul);
/*081*/ p2.add(b0);
/*082*/ p2.add(bPoint);
/*083*/ p2.add(bCal);
/*084*/ p2.add(bDiv);
/*085*/ f.add(p2,BorderLayout.SOUTH);
/*086*/ f.setVisible(true);
/*087*/ }
/*088*/
/*089*/ public void setTextFieldText_Temp(){
/*090*/ if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){
/*091*/ tf.setText(tf.getText()+s);
/*092*/ }else{
/*093*/ tf.setText((tf.getText()+s).substring(0,15));
/*094*/ }
/*095*/ }
/*096*/ public void setTextFieldText(){
/*097*/ if(ifOp){
/*098*/ ifOp = false;
/*099*/ tf.setText("");
/*100*/ setTextFieldText_Temp();
/*101*/ }else{
/*102*/ setTextFieldText_Temp();
/*103*/ }
/*104*/ }
/*105*/
/*106*/ public static void main(String[] args){
/*107*/ EasyCalculator calculator = new EasyCalculator();
/*108*/ calculator.launchFrame();
/*109*/ }
/*110*/
/*111*/ class myWindowListener extends WindowAdapter{
/*112*/ public void windowClosing(WindowEvent e){
/*113*/ System.exit(0);
/*114*/ }
/*115*/ }
/*116*/
/*117*/ class setLabelText_ActionListener implements ActionListener{
/*118*/ public void actionPerformed(ActionEvent e){
/*119*/ Button tempB = (Button)e.getSource();
/*120*/ s = tempB.getLabel();
/*121*/ setTextFieldText();
/*122*/ }
/*123*/ }
/*124*/
/*125*/ class setOperator_ActionListener implements ActionListener{
/*126*/ public void actionPerformed(ActionEvent e){
/*127*/ Button tempB = (Button)e.getSource();
/*128*/ op = tempB.getLabel();
/*129*/ if(op.equals("+")){
/*130*/ tf.setText( cal.opAdd(tf.getText()) );
/*131*/ ifOp = true;
/*132*/ }else if(op.equals("-")){
/*133*/ tf.setText(cal.opSubtract( tf.getText()) );
/*134*/ ifOp = true;
/*135*/ }else if(op.equals("*")){
/*136*/ tf.setText(cal.opMultiply( tf.getText()) );
/*137*/ ifOp = true;
/*138*/ }else if(op.equals("/")){
/*139*/ tf.setText(cal.opDivide( tf.getText()) );
/*140*/ ifOp = true;
/*141*/ }else if(op.equals("=")){
/*142*/ tf.setText(cal.opEquals( tf.getText()) );
/*143*/ ifOp = true;
/*144*/ }
/*145*/ }
/*146*/ }
/*147*/ }
/*002*/ import java.awt.event.*;
/*003*/ import java.util.EventObject;
/*004*/
/*005*/ public class EasyCalculator{
/*006*/ private Frame f;
/*007*/ private Panel p1,p2;
/*008*/ private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
/*009*/ private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;
/*010*/ private TextField tf;
/*011*/ private String s,op;
/*012*/ private Calculator cal = new Calculator();
/*013*/ private boolean ifOp;
/*014*/
/*015*/ public EasyCalculator(){
/*016*/ f = new Frame("Calculator");
/*017*/ p1 = new Panel();
/*018*/ p2 = new Panel();
/*019*/
/*020*/ b0 = new Button("0");
/*021*/ b1 = new Button("1");
/*022*/ b2 = new Button("2");
/*023*/ b3 = new Button("3");
/*024*/ b4 = new Button("4");
/*025*/ b5 = new Button("5");
/*026*/ b6 = new Button("6");
/*027*/ b7 = new Button("7");
/*028*/ b8 = new Button("8");
/*029*/ b9 = new Button("9");
/*030*/ bPoint = new Button(".");
/*031*/ bAdd = new Button("+");
/*032*/ bDec = new Button("-");
/*033*/ bMul = new Button("*");
/*034*/ bDiv = new Button("/");
/*035*/ bCal = new Button("=");
/*036*/
/*037*/ tf = new TextField(25);
/*038*/ tf.setEditable(false);
/*039*/
/*040*/
/*041*/ }
/*042*/
/*043*/ public void launchFrame(){
/*044*/ f.setSize(220,160);
/*045*/ f.setResizable(false);
/*046*/ f.addWindowListener(new myWindowListener());
/*047*/ p1.setLayout(new FlowLayout(FlowLayout.CENTER));
/*048*/ p1.add(tf);
/*049*/ f.add(p1,BorderLayout.NORTH);
/*050*/ p2.setLayout(new GridLayout(4,4));
/*051*/
/*052*/ b0.addActionListener(new setLabelText_ActionListener());
/*053*/ b1.addActionListener(new setLabelText_ActionListener());
/*054*/ b2.addActionListener(new setLabelText_ActionListener());
/*055*/ b3.addActionListener(new setLabelText_ActionListener());
/*056*/ b4.addActionListener(new setLabelText_ActionListener());
/*057*/ b5.addActionListener(new setLabelText_ActionListener());
/*058*/ b6.addActionListener(new setLabelText_ActionListener());
/*059*/ b7.addActionListener(new setLabelText_ActionListener());
/*060*/ b8.addActionListener(new setLabelText_ActionListener());
/*061*/ b9.addActionListener(new setLabelText_ActionListener());
/*062*/ bPoint.addActionListener(new setLabelText_ActionListener());
/*063*/ bAdd.addActionListener(new setOperator_ActionListener());
/*064*/ bDec.addActionListener(new setOperator_ActionListener());
/*065*/ bMul.addActionListener(new setOperator_ActionListener());
/*066*/ bDiv.addActionListener(new setOperator_ActionListener());
/*067*/ bCal.addActionListener(new setOperator_ActionListener());
/*068*/
/*069*/ p2.add(b7);
/*070*/ p2.add(b8);
/*071*/ p2.add(b9);
/*072*/ p2.add(bAdd);
/*073*/ p2.add(b4);
/*074*/ p2.add(b5);
/*075*/ p2.add(b6);
/*076*/ p2.add(bDec);
/*077*/ p2.add(b1);
/*078*/ p2.add(b2);
/*079*/ p2.add(b3);
/*080*/ p2.add(bMul);
/*081*/ p2.add(b0);
/*082*/ p2.add(bPoint);
/*083*/ p2.add(bCal);
/*084*/ p2.add(bDiv);
/*085*/ f.add(p2,BorderLayout.SOUTH);
/*086*/ f.setVisible(true);
/*087*/ }
/*088*/
/*089*/ public void setTextFieldText_Temp(){
/*090*/ if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){
/*091*/ tf.setText(tf.getText()+s);
/*092*/ }else{
/*093*/ tf.setText((tf.getText()+s).substring(0,15));
/*094*/ }
/*095*/ }
/*096*/ public void setTextFieldText(){
/*097*/ if(ifOp){
/*098*/ ifOp = false;
/*099*/ tf.setText("");
/*100*/ setTextFieldText_Temp();
/*101*/ }else{
/*102*/ setTextFieldText_Temp();
/*103*/ }
/*104*/ }
/*105*/
/*106*/ public static void main(String[] args){
/*107*/ EasyCalculator calculator = new EasyCalculator();
/*108*/ calculator.launchFrame();
/*109*/ }
/*110*/
/*111*/ class myWindowListener extends WindowAdapter{
/*112*/ public void windowClosing(WindowEvent e){
/*113*/ System.exit(0);
/*114*/ }
/*115*/ }
/*116*/
/*117*/ class setLabelText_ActionListener implements ActionListener{
/*118*/ public void actionPerformed(ActionEvent e){
/*119*/ Button tempB = (Button)e.getSource();
/*120*/ s = tempB.getLabel();
/*121*/ setTextFieldText();
/*122*/ }
/*123*/ }
/*124*/
/*125*/ class setOperator_ActionListener implements ActionListener{
/*126*/ public void actionPerformed(ActionEvent e){
/*127*/ Button tempB = (Button)e.getSource();
/*128*/ op = tempB.getLabel();
/*129*/ if(op.equals("+")){
/*130*/ tf.setText( cal.opAdd(tf.getText()) );
/*131*/ ifOp = true;
/*132*/ }else if(op.equals("-")){
/*133*/ tf.setText(cal.opSubtract( tf.getText()) );
/*134*/ ifOp = true;
/*135*/ }else if(op.equals("*")){
/*136*/ tf.setText(cal.opMultiply( tf.getText()) );
/*137*/ ifOp = true;
/*138*/ }else if(op.equals("/")){
/*139*/ tf.setText(cal.opDivide( tf.getText()) );
/*140*/ ifOp = true;
/*141*/ }else if(op.equals("=")){
/*142*/ tf.setText(cal.opEquals( tf.getText()) );
/*143*/ ifOp = true;
/*144*/ }
/*145*/ }
/*146*/ }
/*147*/ }
解决方案 »
- list集合问题,希望有人能解决
- 编码转换
- 在Hibernate里面的SessionFactory.getCurrentSession()与(Session)ThreadLocal.get()有什么区别。
- 关于SQL2008 image类型转换成String
- 执行一次大概需要多长时间?
- 关于java.net.URL.openConnection()生成的对象
- 请问BufferedWriter和FileWriter有什么区别吗?
- jxl开发中如何利用一个已经存在的EXCEL作为模板创建新的EXCEL文件
- java中的bufferreader和bufferwriter有什么用阿?如果不使用,有影响么?
- sdk配制问题?请帮忙!谢谢
- 高手帮帮忙~~怎么给我这个小程序,,注册键盘上的"ENTER"事件
- java初学者,向各位前辈请教:有关异常处理的问题
EasyCalculator.javaimport java.awt.*;
import java.awt.event.*;
import java.util.EventObject;public class EasyCalculator{
private Frame f;
private Panel p1,p2;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;
private TextField tf;
private String s,op;
private Calculator cal = new Calculator();
private boolean ifOp;public EasyCalculator(){
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");tf = new TextField(25);
tf.setEditable(false);
}public void launchFrame(){
f.setSize(220,160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,4));b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2,BorderLayout.SOUTH);
f.setVisible(true);
}public void setTextFieldText_Temp(){
if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){
tf.setText(tf.getText()+s);
}else{
tf.setText((tf.getText()+s).substring(0,15));
}
}
public void setTextFieldText(){
if(ifOp){
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
}else{
setTextFieldText_Temp();
}
}public static void main(String[] args){
EasyCalculator calculator = new EasyCalculator();
calculator.launchFrame();
}class myWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}class setOperator_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
}else if(op.equals("-")){
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
}else if(op.equals("*")){
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
}else if(op.equals("/")){
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
}else if(op.equals("=")){
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
import java.awt.event.*;import javax.swing.*;
public class Computer
implements ActionListener{
private boolean append = false;
JTextField jtf = new JTextField(20);
private String s1 = "0";
private String operator = "";
public Computer(){
JFrame jf = new JFrame("cal");
jf.add(jtf,BorderLayout.NORTH);
String[] st = {"Back","CE","C","+",
"7","8","9","-","4","5","6","*",
"1","2","3","/","0","+/-",".","="};
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(5,4));
//JButton[] jb = new JButton[st.length];
for(int i=0;i<st.length;i++){
JButton jb = new JButton(st[i]);
jp.add(jb);
jb.addActionListener(this);
}
jtf.setEditable(false);
jf.add(jp);
jf.setResizable(false);
jf.pack();
jf.setLocation(300,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Computer();
}
public void actionPerformed(ActionEvent ae) {
String comm = ae.getActionCommand();
//if(comm.matches("^\\d+$")){
if("0123456789".indexOf(comm)!=-1){
if(append){
String temp = jtf.getText();
jtf.setText(temp+comm);
}else{
jtf.setText(comm);
append = true;
}
}else if(".".equals(comm)){
String temp = jtf.getText();
if(temp.indexOf(".")==-1){
jtf.setText(temp+".");
append = true;
}
}else if("+-*/".indexOf(comm)!=-1){
s1 = jtf.getText();
operator = comm;
append = false;
}else if("=".equals(comm)){
String s2 = jtf.getText();
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if("+".equals(operator)){
d1 = d1 + d2;
}else if("-".equals(operator)){
d1 = d1 - d2;
}else if("*".equals(operator)){
d1 = d1 * d2;
}else{
d1 = d1/d2;
}
s1 = d1+"";
if(s1.endsWith(".0")){
s1 = s1.substring(0,s1.length()-2);
}
jtf.setText(s1);
append = false;
}else if("+/-".equals(comm)){
String temp = jtf.getText();
if(temp.startsWith("-")){
jtf.setText(temp.substring(1));
}else{
jtf.setText("-"+temp);
}
}else if("C".equals(comm)|| "CE".equals(comm)){
jtf.setText("0");
append = false;
}else if("Back".equals(comm)){
String temp = jtf.getText();
if(temp.length()>0){
jtf.setText(temp.
substring(0,temp.length()-1));
}
}
}
}
多学习ing........
http://yiding-he.javaeye.com/blog/198178
import java.awt.*;
import java.awt.event.*;
public class MyCalculator {
JFrame frame;
JTextField jtf;
JButton[] jbs=new JButton[15];
boolean append=false;
double result;
char operator='='; class NumberListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String text=e.getActionCommand();
if (append){
String s=jtf.getText();
jtf.setText(s+text);
}
else{
jtf.setText(text);
}
append=true;
}
}
class OperatorListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
char o=s.charAt(0);
if (!append && o!='='){
operator=o;
return;
}
append=false;
double tf=Double.parseDouble(jtf.getText());
switch(operator){
case '+':result+=tf;break;
case '-':result-=tf;break;
case '*':result*=tf;break;
case '/':result/=tf;break;
case '=':result=tf;
}
jtf.setText(result+"");
operator=o;
}
}
public MyCalculator(){
init();
}
public void init(){
frame=new JFrame("MyCalculator");
frame.setSize(400,300);
jtf=new JTextField();
jtf.setEditable(false);
jtf.setText("0");
frame.add(jtf,"North");
ActionListener number=new NumberListener();
ActionListener oper=new OperatorListener();
JPanel panel=new JPanel();
for(int i=0;i<10;i++){
jbs[i]=new JButton(i+"");
jbs[i].addActionListener(number);
}
jbs[10]=new JButton("+");
jbs[11]=new JButton("-");
jbs[12]=new JButton("*");
jbs[13]=new JButton("/");
jbs[14]=new JButton("=");
for(int i=10;i<15;i++){
jbs[i].addActionListener(oper);
}
panel.setLayout(new GridLayout(5,3));
for(int i=0;i<5;i++){
panel.add(jbs[i*2]);
panel.add(jbs[i*2+1]);
panel.add(jbs[i+10]);
}
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new MyCalculator();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class NuoyaComputerDemo
{ public static void main(String[] args)
{ Computer NuoyaGUI=new Computer();
NuoyaGUI.setVisible(true);
String lookAndFeelName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try
{
UIManager.setLookAndFeel(lookAndFeelName);//通过UIManager管理器来设置,GUI 的UI界面
SwingUtilities.updateComponentTreeUI(NuoyaGUI);//通过SingUtilities来frame容器UI外观。
}
catch (Exception e)
{ }
}
}
//自定义错误类。不能被零除的错误
class DivisionByZerException extends Exception
{ String str="";
DivisionByZerException()
{
str="除数不能为零。";
}
public String toStrion()
{
return str;
}
}
class Computer extends JFrame
{ private JPanel pane;
Computer()
{ pane=new JPanel();
pane.setLayout(null);
pane.setSize(255,230);
Dimension paneSize=pane.getSize();
//窗体大小及窗体基本属性的定义
setTitle("计算器-无菜单");
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
setSize(paneSize.width,paneSize.height);
Dimension frameSize=getSize();
setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
//窗体布局方法
getContentPane().add(pane);//窗体使用null布局
//计算器中的按钮布局
JButtonClass jButtonClass=new JButtonClass();
//计算器中的显示屏。
jButtonClass.showStringNumberSize();
}
//命令按键的类定义
class JButtonClass implements ActionListener
{ private JTextField JFTextNumber;
// 计算器中所有按钮的定义
private JButton[] JButtonClear=new JButton[4];
private JButton[] JButtonM=new JButton[4];
private JButton[] JButtonGroup=new JButton[20];
//设置计算变量
String Number="";
//键盘和按钮的键面字符接收器
String keyAndBut="";
//用于计算的两个数。
private double FirstNumber=0,SecondNumber=0;
//探测FirstNumber,SecondNumber是否被赋值,为false表示没有值。
private boolean DetectFirstNumber=true,DetectSecondNumber=true;
//等号探测
private boolean DetectDengyu = true;
//探测记忆体是否存在。memoryDetector记忆探测器,memoryCoexist记忆共存器
private boolean memoryDetector=false,memoryCoexist=false;
//对于计算的结果无权进行Backspace处理.power变量为boolean类型,false表示无权,true表示有权。
private boolean power=true;
//saveRecover 为变量为预存恢复量,类型为boolean。主要发生在“等号”按钮上。
//true为进行预存恢复,false为不进行预存恢复。
private boolean saveRecover=false;
//为FirstNumber和SecondNumber传递数值的publicDoubleNumber变量
private double publicDoubleNumber=0;
//完成M系列按钮的数字记忆体
private double memoryNumber=0,memoryNumberSum=0;
//saveRecoverValue变量为int类型,主要是将最后的结果进行预存操作
private double saveRecoverValue=0,saveRecoverValueTwo=0;
//探测+-*/按钮被按下的是哪一个,-1为哨兵值。
private int DetectButtonTag=-1,Tag=-1;
//没有点击小数点时为真
private boolean xiaoshudian=true;
JButtonClass()
{ //按钮定位量:Pointx 表示x轴,Pointy 表示y轴,
//JButtonWidth 表示按钮的宽度,JButtonHeight 表示按钮的高度。
int Pointx=54,Pointy=35,JButtonWidth=75,JButtonHeight=25;
// 计算器在所有按钮的名称定义
String[] ButtonClearName=new String[]{"Backspace","CE","C"};
String[] ButtonMName=new String[]{"MC","MR","MS","M+"};
String[] ButtonGroupName=new String[]{"7","8","9","/","sqrt","4","5","6","*","%",
"1","2","3","-","1/x","0","+/-",".","+","="};
JButtonClear[0] = new JButton(" ");
JButtonFC(JButtonClear[0],13,35,30,25);
JButtonClear[0].setEnabled(false);
for(int i=1;i<4;i++)
{ JButtonClear[i]=new JButton(ButtonClearName[i-1]);
JButtonFC(JButtonClear[i],Pointx,Pointy,JButtonWidth,JButtonHeight);
JButtonClear[i].setForeground(Color.RED);
Pointx +=JButtonWidth+5;
JButtonWidth=50;
}
Pointx=10; Pointy=70; JButtonWidth=35; JButtonHeight=25;
for(int i=0;i<4;i++)
{ JButtonM[i]=new JButton(ButtonMName[i]);
JButtonFC(JButtonM[i],Pointx,Pointy,JButtonWidth,JButtonHeight);
JButtonM[i].setForeground(Color.RED);
Pointy+=JButtonHeight+5;
}
Pointx=53; Pointy=70; JButtonWidth=35; JButtonHeight=25;
for(int i=0;i<20;i++)
{ if(i==5 || i==10 || i==15 )
{ Pointy+= JButtonHeight+5;
Pointx=53;
}
JButtonGroup[i]=new JButton(ButtonGroupName[i]);
JButtonFC(JButtonGroup[i],Pointx,Pointy,JButtonWidth,JButtonHeight);
if(i==3||i==8||i==13||i==18||i==19)
JButtonGroup[i].setForeground(Color.RED);
else
JButtonGroup[i].setForeground(Color.BLUE);
Pointx+=JButtonWidth+3;
}
} //所有命令按钮的事件代码
public void actionPerformed(ActionEvent e)
{ String buttonName=e.getActionCommand();
if(buttonName.equals("sqrt"))
{ try
{ double doubleNumber=Double.parseDouble(JFTextNumber.getText().trim());
saveRecover=false;
if(doubleNumber>0)
{ publicDoubleNumber=Math.sqrt(doubleNumber);
Number=String.valueOf(publicDoubleNumber);
if(Number.endsWith(".0"))
{ Number=Number.substring(0,Number.length()-1).trim();
JFTextNumber.setText(Number);
}
else
JFTextNumber.setText(Number);
Number="";
}
else if(doubleNumber==0)
{ publicDoubleNumber=0;
JFTextNumber.setText("0.");
}
else
{ JFTextNumber.setText("函数输入无效。");
Number="";
}
if(DetectFirstNumber)
FirstNumber=publicDoubleNumber;
else
saveRecoverValueTwo=SecondNumber=publicDoubleNumber;
}
catch(NumberFormatException e2)
{ return;
}
}
else if(buttonName.equals("%"))
{ if(JFTextNumber.getText().trim().equals("0."))
return;
else
{ try
{ Number=JFTextNumber.getText().trim();
double doubleNumber=Double.parseDouble(Number)/100;
saveRecover=false;
if((Number=String.valueOf(doubleNumber)).trim().endsWith(".0"))
{ Number=Number.substring(0,Number.length()-1);
JFTextNumber.setText(Number.trim());
}
else
JFTextNumber.setText(Number.trim());
publicDoubleNumber=doubleNumber;
if(DetectFirstNumber)
FirstDetect(publicDoubleNumber);
else
saveRecoverValueTwo=SecondNumber=publicDoubleNumber;
Number="";
}
catch(NumberFormatException e4)
{ Number="";
return;
}
}
} 贴不完了。