日期:2014-05-20  浏览次数:20657 次

一个简易计算器的问题
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calc extends JFrame implements ActionListener
{
  static int i=0;
JButton jb1=new JButton("0");
  JButton jb2=new JButton("1");
  JButton jb3=new JButton("2");
  JButton jb4=new JButton("3");
  JButton jb5=new JButton("4");
  JButton jb6=new JButton("5");
  JButton jb7=new JButton("6");
  JButton jb8=new JButton("7");
  JButton jb9=new JButton("8");
  JButton jb10=new JButton("9");
  JButton jb11=new JButton("+");
  JButton jb12=new JButton("-");
  JButton jb13=new JButton("=");
  JLabel jl=new JLabel("答案是多少");
  JPanel jp=new JPanel();
  GridLayout gl=new GridLayout(5,2);
  public Calc()
  {
  this.setTitle("简易计算器");
  jp.setLayout(gl);
  jp.add(jb1);
  jp.add(jb2);
  jp.add(jb3);
  jp.add(jb4);
  jp.add(jb5);
  jp.add(jb6);
  jp.add(jb7);
  jp.add(jb8);
  jp.add(jb9);
  jp.add(jb10);
  jp.add(jb11);
  jp.add(jb12);
  jp.add(jb13);
  jp.add(jl);
  jb1.addActionListener(this);
  jb2.addActionListener(this);
  jb3.addActionListener(this);
  jb4.addActionListener(this);
  jb5.addActionListener(this);
  jb6.addActionListener(this);
  jb7.addActionListener(this);
  jb8.addActionListener(this);
  jb9.addActionListener(this);
  jb10.addActionListener(this);
  jb11.addActionListener(this);
  jb12.addActionListener(this);
  jb13.addActionListener(this);
  this.add(jp);
  this.setBounds(300,200,500,300);
  this.setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
  if(e.getSource()==jb1)
  {
  jl.setText("0");
  i=0;
  }
  if(e.getSource()==jb2)
  {
  jl.setText("1");
  i=1;
  }
  if(e.getSource()==jb3)
  {
  jl.setText("2");
  i=2;
  }
  if(e.getSource()==jb4)
  {
  jl.setText("3");
  i=3;
  }
  if(e.getSource()==jb5)
  {
  jl.setText("4");
  i=4;
  }
  if(e.getSource()==jb6)
  {
  jl.setText("5");
  i=5;
  }
  if(e.getSource()==jb7)
  {
  jl.setText("6");
  i=6;
  }
  if(e.getSource()==jb8)
  {
  jl.setText("7");
  i=7;
  }
  if(e.getSource()==jb9)
  {
  jl.setText("8");
  i=8;
  }
  if(e.getSource()==jb10)
  {
  jl.setText("9");
  i=9;
  }
  if(e.getSource()==jb11)
  {
  jl.setText(i+"+");
  }
  if(e.getSource()==jb12)
  {
  jl.setText(i+"-");
  }
  if(e.getSource()==jb13)
  {