效果图:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//定义了一个面板存放控件
class MyPanel extends JPanel implements ActionListener
{
//定义了19个按钮(有两个还没实现),一个文本框
JButton jb1=new JButton("1");
JButton jb2=new JButton("2");
JButton jb3=new JButton("3");
JButton jb4=new JButton("4");
JButton jb5=new JButton("5");
JButton jb6=new JButton("6");
JButton jb7=new JButton("7");
JButton jb8=new JButton("8");
JButton jb9=new JButton("9");
JButton jb10=new JButton("0");
JButton jb11=new JButton("+");
JButton jb12=new JButton("-");
JButton jb13=new JButton("*");
JButton jb14=new JButton("/");
JButton jb15=new JButton("+/-");
JButton jb16=new JButton("清零");
JButton jb17=new JButton(""); //留着给以后加按钮
JButton jb18=new JButton(".");//起符号作用的按钮,还没实现
JButton jb19=new JButton("=");
JTextField jtf=new JTextField(20);
String type;//定义了一个字符串存放算术运算符
double a=0D,b=0D;//定义两个数字(算术运算符两边的数字)
public MyPanel()
{
jtf.setText("0");
jtf.setHorizontalAlignment(JTextField.RIGHT);
setLayout(null);//定义面板布局为空型
jb1.setBounds(new Rectangle(125, 148, 45, 25));
jb2.setBounds(new Rectangle(70, 148, 45, 25));
jb3.setBounds(new Rectangle(15, 148, 45, 25));
jb4.setBounds(new Rectangle(125, 115, 45, 25));
jb5.setBounds(new Rectangle(70, 115, 45, 25));
jb6.setBounds(new Rectangle(15, 115, 45, 25));
jb7.setBounds(new Rectangle(125, 80, 45, 25));
jb8.setBounds(new Rectangle(70, 80, 45, 25));
jb9.setBounds(new Rectangle(15, 80, 45, 25));
jb10.setBounds(new Rectangle(15, 180, 45, 25));
jb11.setBounds(new Rectangle(185, 148, 60, 25));
jb12.setBounds(new Rectangle(185, 180, 60, 25));
jb13.setBounds(new Rectangle(185, 115, 60, 25));
jb14.setBounds(new Rectangle(185, 80, 60, 25));
jb15.setBounds(new Rectangle(185, 45, 60, 25));
jb16.setBounds(new Rectangle(15, 45, 70, 25));
jb17.setBounds(new Rectangle(96, 45, 70, 25));
jb18.setBounds(new Rectangle(70, 180, 45, 25));
jb19.setBounds(new Rectangle(125, 180, 45, 25));
jtf.setBounds(new Rectangle(15, 9, 231, 28));
add(jtf);
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);
jb14.addActionListener(this);
jb15.addActionListener(this);
jb16.addActionListener(this);
jb17.addActionListener(this);
jb18.addActionListener(this);
jb19.addActionListener(this);
add(jb1);
add(jb2);
add(jb3);
add(jb4);
add(jb5);
add(jb6);
add(jb7);
add(jb8);
add(jb9);
add(jb10);
add(jb11);
add(jb12);
add(jb13);
add(jb14);
add(jb15);
add(jb16);
add(jb17);
add(jb18);
add(jb19);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
if(type=="equal")
jtf.setText("1");
else if(jtf.getText().equals("0"))
jtf.setText("1");
else
jtf.setText(jtf.getText()+'1');
}
else if(e.getSource()==jb2)
{
if(type=="equal")
jtf.setText("2");
else if(jtf.getText().equals("0"))
jtf.setText("2");
else
jtf.setText(jtf.getText()+'2');
}
else if(e.getSource()==jb3)
{
if(type=="equal")
jtf.setText("3");
else if(jtf.getText().equals("0"))
jtf.setText("3");
else
jtf.setText(jtf.getText()+'3');
}
else if(e.getSource()==jb4)
{
if(type=="equal")
jtf.setText("4");
else if(jtf.getText().equals("0"))
jtf.setText("4");
else
jtf.setText(jtf.getText()+'4');
}
else if(e.getSource()==jb5)
{
if(type=="equal")
jtf.setText("5");
else if(jtf.getText().equals("0"))
jtf.setText("5");
else
jtf.setText(jtf.getText()+'5');
}
else if(e.getSource()==jb6)
{
if(type=="equal")
jtf.setText("6");
else if(jtf.getText().equals("0"))
jtf.setText("6");
else
jtf.setText(jtf.getText()+'6');
}
else if(e.getSource()==jb7)
{
if(type=="equal")
jtf.setText("7");
else if(jtf.getText().equals("0"))
jtf.setText("7");
else
jtf.setText(jtf.getText()+'7');
}
else if(e.getSource()==jb8)
{
if(type=="equal")
jtf.setText("8");
else if(jtf.getText().equals("0"))
jtf.setText("8");
else
jtf.setText(jtf.getText()+'8');
}
else if(e.getSource()==jb9)
{
if(type=="equal")
jtf.setText("9");
else if(jtf.getText().equals("0"))
jtf.setText("9");
else
jtf.setText(jtf.getText()+'9');
}
else if(e.getSource()==jb10)
{
if(type=="equal")
jtf.setText("0");
else if(jtf.getText().equals("0"))
jtf.setText("0");
else
jtf.setText(jtf.getText()+'0');
}
else if(e.getSource()==jb18)
{
if(jtf.getText().indexOf(".")<1)
{
jtf.setText(jtf.getText()+'.');
}
else
{
jtf.setText(jtf.getText());
}
}
else if(e.getSource()==jb18)
{
if(jtf.getText().equals("0"))
jtf.setText("0");
}
else if(e.getSource()==jb16)
{
jtf.setText("0");
}
else if(e.getSource()==jb11)
{
if(jtf.getText().equals("0"))
jtf.setText("0");
else
{
type="add";
a=Double.parseDouble(jtf.getText());
jtf.setText("");
}
}
else if(e.getSource()==jb12)
{
if(jtf.getText().equals("0"))
jtf.setText("0");
else
{
type="subtract";
a=Double.parseDouble(jtf.getText());
jtf.setText("");
}
}
else if(e.getSource()==jb13)
{
if(jtf.getText().equals("0"))
jtf.setText("0");
else
{
type="multiply";
a=Double.parseDouble(jtf.getText());
jtf.setText("");
}
}
else if(e.getSource()==jb14)
{
if(jtf.getText().equals("0"))
jtf.setText("0");
else
{
type="divide";
a=Double.parseDouble(jtf.getText());
jtf.setText("");
}
}
else if(e.getSource()==jb19)
{
if(type=="add")
{
b=Double.parseDouble(jtf.getText());
jtf.setText(Double.toString(a+b));
type="equal";
}
else if(type=="subtract")
{
b=Double.parseDouble(jtf.getText());
jtf.setText(Double.toString(a-b));
type="equal";
}
else if(type=="multiply")
{
b=Double.parseDouble(jtf.getText());
jtf.setText(Double.toString(a*b));
type="equal";
}
else if(type=="divide")
{
b=Double.parseDouble(jtf.getText());
jtf.setText(Double.toString(a/b));
type="equal";
}
}
}
}
class Calculator extends JFrame
{
public Calculator()
{
this.setTitle("计算器");
this.getContentPane().add(new MyPanel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,250);
setVisible(true);
}
public static void main(String args[])
{
new Calculator();
}
}
posted on 2005-09-20 17:31
千山鸟飞绝 阅读(797)
评论(0) 编辑 收藏 所属分类:
J2SE