作者在 2007-08-04 10:07:00 发布以下内容
看到网上经常有人寻找简单计算器的源代码,于是今天花了点时间写了一个简单的计算器,供大家参考一下.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Jsq extends JFrame
{
double a=0,b=0,c=0;
int i=0;
JPanel pl1=new JPanel();
JPanel pl2=new JPanel();
JTextField tf=new JTextField(10);
JButton bt=new JButton("清空");
JButton bt1=new JButton("1");
JButton bt2=new JButton("2");
JButton bt3=new JButton("3");
JButton bt4=new JButton("4");
JButton bt5=new JButton("5");
JButton bt6=new JButton("6");
JButton bt7=new JButton("7");
JButton bt8=new JButton("8");
JButton bt9=new JButton("9");
JButton bt10=new JButton("0");
JButton bt11=new JButton("+");
JButton bt12=new JButton("-");
JButton bt13=new JButton("*");
JButton bt14=new JButton("/");
JButton bt15=new JButton(".");
JButton bt16=new JButton("=");
public Jsq()
{
super("计算器");
Container c=getContentPane();
c.add(pl1,BorderLayout.NORTH);
c.add(pl2,BorderLayout.CENTER);
pl1.setLayout(new FlowLayout());
pl1.add(tf);
pl1.add(bt);
pl2.setLayout(new GridLayout(4,4));
pl2.add(bt1);
pl2.add(bt2);
pl2.add(bt3);
pl2.add(bt11);
pl2.add(bt4);
pl2.add(bt5);
pl2.add(bt6);
pl2.add(bt12);
pl2.add(bt7);
pl2.add(bt8);
pl2.add(bt9);
pl2.add(bt13);
pl2.add(bt10);
pl2.add(bt14);
pl2.add(bt15);
pl2.add(bt16);
bt.addActionListener(new shijian());
bt1.addActionListener(new shijian());
bt2.addActionListener(new shijian());
bt3.addActionListener(new shijian());
bt4.addActionListener(new shijian());
bt5.addActionListener(new shijian());
bt6.addActionListener(new shijian());
bt7.addActionListener(new shijian());
bt8.addActionListener(new shijian());
bt9.addActionListener(new shijian());
bt10.addActionListener(new shijian());
bt11.addActionListener(new shijian());
bt12.addActionListener(new shijian());
bt13.addActionListener(new shijian());
bt14.addActionListener(new shijian());
bt15.addActionListener(new shijian());
bt16.addActionListener(new shijian());
setSize(200,200);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new Jsq();
}
class shijian implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.ge