import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
//声明该类实现ActionListener接口,监听者对象是calculator类的对象
public class calculator extends JFrame implements ActionListener{ 
 JPanel jPanel1,jPanel2; 
 JTextField tf; 
 JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,c,f; 
 boolean end,jia,jian,cheng,chu; 
 String str; // 创建一个空字符串缓冲区
 double num1,num2; // num1和num2和y都是运算数
 calculator(){ 
super("简cool计算器"); 
setSize(400,200); 
Container cp=getContentPane(); //创建JFrame的容器对象
cp.setLayout(new BorderLayout()); //设置BorderLayout布局组件间隔为5
jPanel1=new JPanel(); //创建jPanel1面板
jPanel1.setLayout(new GridLayout()); //为面板jPanel1默认设置一行一列的组件GridLayout布局
jPanel2=new JPanel(); //创建jPanel2面板
jPanel2.setLayout(new GridLayout(4,4)); //为面板jPanel2设置G组件四行四列的GridLayout布局
tf=new JTextField("0"); 
jPanel1.add(tf); //将tf放置在面板jPanel1里
tf.setBackground(Color.white);//为文本区域设置背景色
tf.setFont(new Font("宋体", Font.BOLD + Font.ITALIC, 28)); 
//设置显示字体
cp.add(jPanel1,BorderLayout.NORTH); //将jpane1放置在BorderLayou布局的北区
s1=new JButton(" 1 "); s1.addActionListener(this); //创建按钮1,并注册s1的监听者对象this
s2=new JButton(" 2 "); s2.addActionListener(this); 
s3=new JButton(" 3 "); s3.addActionListener(this); 
s4=new JButton(" 4 "); s4.addActionListener(this); 
s5=new JButton(" 5 "); s5.addActionListener(this); 
s6=new JButton(" 6 "); s6.addActionListener(this); 
s7=new JButton(" 7 "); s7.addActionListener(this); 
s8=new JButton(" 8 "); s8.addActionListener(this); 
s9=new JButton(" 9 "); s9.addActionListener(this); 
s0=new JButton(" 0 "); s0.addActionListener(this); 
b1=new JButton(" + "); b1.addActionListener(this); 
b2=new JButton(" - "); b2.addActionListener(this); 
b3=new JButton(" × "); b3.addActionListener(this); 
b4=new JButton(" ÷ "); b4.addActionListener(this); 
 c=new JButton("AC");  c.addActionListener(this);
 f=new JButton(" = ");  f.addActionListener(this); 
jPanel2.add(s1); jPanel2.add(s2); jPanel2.add(s3); jPanel2.add(b1);//将按钮添加到jpanel2面板中
jPanel2.add(s4); jPanel2.add(s5); jPanel2.add(s6); jPanel2.add(b2); 
jPanel2.add(s7); jPanel2.add(s8); jPanel2.add(s9); jPanel2.add(b3); 
jPanel2.add(s0);  jPanel2.add(c);  jPanel2.add(f); jPanel2.add(b4); 
cp.add(jPanel2,BorderLayout.CENTER); //将面板jpanel2加载到BorderLayout界面上
jPanel2.setBackground(Color.red);
s1.setForeground(Color.white);  s2.setForeground(Color.white);//设置按钮的前景色
s3.setForeground(Color.white);  s4.setForeground(Color.white);
s5.setForeground(Color.white);  s6.setForeground(Color.white);
s7.setForeground(Color.white);  s8.setForeground(Color.white);
s9.setForeground(Color.white);  s0.setForeground(Color.white);
  b1.setForeground(Color.red);    b2.setForeground(Color.red);
  b3.setForeground(Color.red);    b4.setForeground(Color.red);
c.setForeground(Color.white);    f.setForeground(Color.white);
s1.setBackground(Color.black);  s2.setBackground(Color.black);//设置按钮的背景色
s3.setBackground(Color.black);  s4.setBackground(Color.black);
s5.setBackground(Color.black);  s6.setBackground(Color.black);
s7.setBackground(Color.black);  s8.setBackground(Color.black);
s9.setBackground(Color.black);  s0.setBackground(Color.black);
b1.setBackground(Color.black);  b2.setBackground(Color.black);
b3.setBackground(Color.black);  b4.setBackground(Color.black);
c.setBackground(Color.black);    f.setBackground(Color.black);
setResizable(false); // 设置面板大小不可变
setVisible(true);//设置面板可见

public void num(int i){ //num()方法
String s = null; 
s=String.valueOf(i); //解释
if(end){ 
tf.setText("0"); 
end=false; 

if((tf.getText()).equals("0")){ 
tf.setText(s); 

else{ 
str = tf.getText() + s; 
tf.setText(str); 


public void actionPerformed(ActionEvent e){ //整个方法解释
//数字事件 
if(e.getSource()==s1) 
num(1); 
else if(e.getSource()==s2) 
num(2); 
else if(e.getSource()==s3) 
num(3); 
else if(e.getSource()==s4) 
num(4); 
else if(e.getSource()==s5) 
num(5); 
else if(e.getSource()==s6) 
num(6); 
else if(e.getSource()==s7) 
num(7); 
else if(e.getSource()==s8) 
num(8); 
else if(e.getSource()==s9) 
num(9); 
else if(e.getSource()==s0) 
num(0); 
else if (e.getSource() == c)// 选择的是“AC”按钮
{
tf.setText("0");// 把显示屏清零
}
else if(e.getSource()==b1) 
sign(1); 
else if(e.getSource()==b2) 
sign(2); 
else if(e.getSource()==b3) 
sign(3); 
else if(e.getSource()==b4) 
sign(4); 
//等号 
else if(e.getSource()==f){ 
num2=Double.parseDouble(tf.getText()); //得到文本域中的数字字符串调用parseDouble()函数转化为数值型数字 
if(jia){ 
num1=num1 + num2;} 
else if(jian){ 
num1=num1 - num2;} 
else if(cheng){ 
num1=num1 * num2;} 
else if(chu){ 
num1=num1 / num2;} 
tf.setText(""+num1); 
end=true; 


public void sign(int s){ //运算符的使用整个方法if(s==1){ 
jia=true; 
jian=false; 
cheng=false; 
chu=false; 

else if(s==2){ 
jia=false; 
jian=true; 
cheng=false; 
chu=false; 

else if(s==3){ 
jia=false; 
jian=false; 
cheng=true; 
chu=false; 

else if(s==4){ 
jia=false; 
jian=false; 
cheng=false; 
chu=true; 

num1=Double.parseDouble(tf.getText()); //得到文本域中的数字字符串调用parseDouble()函数转化为数值型数字 
end=true; 

//// 主方法初始化
public static void main(String[] args){ 
calculator reno=new calculator(); 


解决方案 »

  1.   

    JDK
    楼主要有点耐心!
    不要把自己不想去思考的问题扔给别人.
      

  2.   

    String.valueOf(i) 转成字符串
      

  3.   

    懒得要命,建议楼主重新系统学习一下Java基本语法
      

  4.   

    jia=true; 
    jian=false; 
    cheng=false; 
    chu=false; 
    能不能把三个false的去掉????时间紧迫,没时间系统学啦,各未大侠帮帮忙
      

  5.   

    你试一下不就知道了?
    再说,你为什么要去掉?放在那里碍你什么事了?话说回来,如果你的actionPerformed()方法能保证只执行一遍,那么可以去掉!