import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField text;
private JButton bt_sqrt,bt_pm,bt_CE,bt_C,bt_divide ,bt_multiply,bt_minus,bt_plus,bt_point,bt_equal;
private JButton bt[]=new JButton[10];
private JDialog dialog;
private JLabel label;
boolean first1=true,first2=true,first3=true,first4=true;
int result=0;
public Calculator()
{
//super("Calculator");
this.setSize(220,250);
this.setLocation(300,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
JPanel panel=new JPanel(new FlowLayout(1,4,4));
this.add(panel);
dialog=new JDialog(this,"提示",true);
dialog.setSize(240,100);
label=new JLabel("",JLabel.CENTER);
dialog.add(label);
dialog.setDefaultCloseOperation(1);
text=new JTextField(18);
text.setEditable(false);
bt_sqrt=new JButton("sqrt");
bt_pm=new JButton("+/-");
bt_CE=new JButton("CE");
bt_C=new JButton("C");
bt_divide=new JButton("/");
bt_multiply=new JButton("*");
bt_minus=new JButton("-");
bt_plus=new JButton("+");
bt_point=new JButton(".");
bt_equal=new JButton("=");
for(int i=0; i<10;i++)
{
bt[i]=new JButton(""+i);
}
panel.add(text);
panel.add(bt_sqrt);
bt_sqrt.addActionListener(this);
panel.add(bt_pm);
bt_pm.addActionListener(this);
panel.add(bt_CE);
bt_CE.addActionListener(this);
panel.add(bt_C);
bt_C.addActionListener(this);
panel.add(bt[7]);
bt[7].addActionListener(this);
panel.add(bt[8]);
bt[8].addActionListener(this);
panel.add(bt[9]);
bt[9].addActionListener(this);
// panel.add(bt_divide);
// bt_divide.addActionListener(this);
panel.add(bt[4]);
bt[4].addActionListener(this);
panel.add(bt[5]);
bt[5].addActionListener(this);
panel.add(bt[6]);
bt[6].addActionListener(this);
// panel.add(bt_multiply);
// bt_multiply.addActionListener(this);
panel.add(bt[1]);
bt[1].addActionListener(this);
panel.add(bt[2]);
bt[2].addActionListener(this);
panel.add(bt[3]);
bt[3].addActionListener(this);
// panel.add(bt_minus);
// bt_minus.addActionListener(this);
panel.add(bt[0]);
bt[0].addActionListener(this);
// panel.add(bt_point);
// bt_point.addActionListener(this);
panel.add(bt_equal);
bt_equal.addActionListener(this);
panel.add(bt_plus);
bt_plus.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str1="0123456789+-*/=CCEsqrt";
String str2= e.getActionCommand();
int n=str1.indexOf(str2,0);
if(n>=0&&n<=9)
if(first1)
text.setText(""+text.getText()+e.getActionCommand()); //只有数字字符,才能转化为数字!
else
{
text.setText(""+e.getActionCommand());
first1=true;
}
else
if(n==10)
{
first1=false;
result=result+Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==11)
if(first4)
{
result=Integer.parseInt(text.getText());first1=false;first4=false;
}
else
{
first1=false;
result=result-Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==12)
if(first2)
{
result=Integer.parseInt(text.getText());first1=false;first2=false;
}
else
{
first1=false;
result=result*Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==13)
if(first3)
{
result=Integer.parseInt(text.getText());first1=false;first3=false;
}
else
{
try
{
first1=false;
result=result/Integer.parseInt(text.getText());//在进行复制时,要注意此时变量与原来变量的不同。
text.setText(""+result);
}
catch(ArithmeticException nfe)
{
label.setText("除数不能为零");
dialog.setLocation(this.getX()+80,this.getY()+80);
dialog.setVisible(true);
}
finally
{}
}
if(n==14)
{
first1=false;
result=result+Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==15)
{
result=0;
text.setText(" ");
}
}
public static void main(String args[])
{
new Calculator();
}
}
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField text;
private JButton bt_sqrt,bt_pm,bt_CE,bt_C,bt_divide ,bt_multiply,bt_minus,bt_plus,bt_point,bt_equal;
private JButton bt[]=new JButton[10];
private JDialog dialog;
private JLabel label;
boolean first1=true,first2=true,first3=true,first4=true;
int result=0;
public Calculator()
{
//super("Calculator");
this.setSize(220,250);
this.setLocation(300,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
JPanel panel=new JPanel(new FlowLayout(1,4,4));
this.add(panel);
dialog=new JDialog(this,"提示",true);
dialog.setSize(240,100);
label=new JLabel("",JLabel.CENTER);
dialog.add(label);
dialog.setDefaultCloseOperation(1);
text=new JTextField(18);
text.setEditable(false);
bt_sqrt=new JButton("sqrt");
bt_pm=new JButton("+/-");
bt_CE=new JButton("CE");
bt_C=new JButton("C");
bt_divide=new JButton("/");
bt_multiply=new JButton("*");
bt_minus=new JButton("-");
bt_plus=new JButton("+");
bt_point=new JButton(".");
bt_equal=new JButton("=");
for(int i=0; i<10;i++)
{
bt[i]=new JButton(""+i);
}
panel.add(text);
panel.add(bt_sqrt);
bt_sqrt.addActionListener(this);
panel.add(bt_pm);
bt_pm.addActionListener(this);
panel.add(bt_CE);
bt_CE.addActionListener(this);
panel.add(bt_C);
bt_C.addActionListener(this);
panel.add(bt[7]);
bt[7].addActionListener(this);
panel.add(bt[8]);
bt[8].addActionListener(this);
panel.add(bt[9]);
bt[9].addActionListener(this);
// panel.add(bt_divide);
// bt_divide.addActionListener(this);
panel.add(bt[4]);
bt[4].addActionListener(this);
panel.add(bt[5]);
bt[5].addActionListener(this);
panel.add(bt[6]);
bt[6].addActionListener(this);
// panel.add(bt_multiply);
// bt_multiply.addActionListener(this);
panel.add(bt[1]);
bt[1].addActionListener(this);
panel.add(bt[2]);
bt[2].addActionListener(this);
panel.add(bt[3]);
bt[3].addActionListener(this);
// panel.add(bt_minus);
// bt_minus.addActionListener(this);
panel.add(bt[0]);
bt[0].addActionListener(this);
// panel.add(bt_point);
// bt_point.addActionListener(this);
panel.add(bt_equal);
bt_equal.addActionListener(this);
panel.add(bt_plus);
bt_plus.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str1="0123456789+-*/=CCEsqrt";
String str2= e.getActionCommand();
int n=str1.indexOf(str2,0);
if(n>=0&&n<=9)
if(first1)
text.setText(""+text.getText()+e.getActionCommand()); //只有数字字符,才能转化为数字!
else
{
text.setText(""+e.getActionCommand());
first1=true;
}
else
if(n==10)
{
first1=false;
result=result+Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==11)
if(first4)
{
result=Integer.parseInt(text.getText());first1=false;first4=false;
}
else
{
first1=false;
result=result-Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==12)
if(first2)
{
result=Integer.parseInt(text.getText());first1=false;first2=false;
}
else
{
first1=false;
result=result*Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==13)
if(first3)
{
result=Integer.parseInt(text.getText());first1=false;first3=false;
}
else
{
try
{
first1=false;
result=result/Integer.parseInt(text.getText());//在进行复制时,要注意此时变量与原来变量的不同。
text.setText(""+result);
}
catch(ArithmeticException nfe)
{
label.setText("除数不能为零");
dialog.setLocation(this.getX()+80,this.getY()+80);
dialog.setVisible(true);
}
finally
{}
}
if(n==14)
{
first1=false;
result=result+Integer.parseInt(text.getText());
text.setText(""+result);
}
if(n==15)
{
result=0;
text.setText(" ");
}
}
public static void main(String args[])
{
new Calculator();
}
}