import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.KeyStroke;
public class ff extends JFrame implements ActionListener
{
Panel bu=new Panel();
Panel bu1=new Panel();
private KeyEventHandler keyListener= new KeyEventHandler();
private JButton one=new JButton("1");
private JButton two=new JButton("2");
private JButton three=new JButton("3");
private JButton four=new JButton("4");
private JButton five=new JButton("5");
private JButton six=new JButton("6");
private JButton seven=new JButton("7");
private JButton night=new JButton("8");
private JButton nine=new JButton("9");
private JButton zero=new JButton("0");
private JButton dian=new JButton(".");
private JButton a=new JButton("+");
private JButton b=new JButton("-");
private JButton c=new JButton("*");
private JButton d=new JButton("/");
private JButton de=new JButton("=");
private JButton t=new JButton("退格");
private JButton x=new JButton("exit");
private JButton cl=new JButton("clear");
private JButton jd=new JButton("+/-");
private JTextField text=new JTextField();
JPanel p1=new JPanel(new BorderLayout());
JPanel p2=new JPanel(new GridLayout(4,4));
JPanel p3=new JPanel(new FlowLayout());
public double tmp=0;
public double opt1,opt2;
public int flag;
public ff()
{
super("计算器");
text.setEditable(false);
text.addKeyListener(keyListener);
text.setHorizontalAlignment(JTextField.RIGHT);
Container p=getContentPane();
p.setLayout(new BorderLayout());
JPanel p1=new JPanel(new BorderLayout());
JPanel p2=new JPanel(new GridLayout(4,4));
JPanel p3=new JPanel(new FlowLayout());
p.add(text,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p1.add(de,BorderLayout.EAST);
p1.add(p3,BorderLayout.NORTH);
p1.add(p2,BorderLayout.CENTER);
p3.add(t);
p3.add(x);
p3.add(cl);
p2.add(nine);
p2.add(night);
p2.add(seven);
p2.add(b);
p2.add(four);
p2.add(five);
p2.add(six);
p2.add(a);
p2.add(one);
p2.add(two);
p2.add(three);
p2.add(d);
p2.add(zero);
p2.add(dian);
p2.add(jd);
p2.add(c);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}});
pack();
show();
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
night.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
d.addActionListener(this);
x.addActionListener(this);
cl.addActionListener(this);
de.addActionListener(this);
dian.addActionListener(this);
t.addActionListener(this);
jd.addActionListener(this);
}
public void this_keyPressed(KeyEvent e)
{
int kc = e.getKeyCode();
if (kc==KeyEvent.VK_A)
text.setText(text.getText()+"2");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==x)
System.exit(0);
else if(e.getSource()==one)
text.setText(text.getText()+"1");
else if(e.getSource()==two)
text.setText(text.getText()+"2");
else if(e.getSource()==three)
text.setText(text.getText()+"3");
else if(e.getSource()==four)
text.setText(text.getText()+"4");
else if(e.getSource()==five)
text.setText(text.getText()+"5");
else if(e.getSource()==six)
text.setText(text.getText()+"6");
else if(e.getSource()==seven)
text.setText(text.getText()+"7");
else if(e.getSource()==night)
text.setText(text.getText()+"8");
else if(e.getSource()==nine){
text.setText(text.getText()+"9");
}
else if(e.getSource()==t)
{ if(text.getText().equals(""))return;
else{
StringBuffer sb=new StringBuffer(text.getText());
sb.deleteCharAt(sb.length()-1);
String s9=new String(sb);
text.setText(s9);}
}
else if(e.getSource()==dian)
{ int gm;
gm=text.getText().length();
StringBuffer aaa=new StringBuffer(text.getText().substring(gm-1));
String aa=new String(aaa);
String bb=".";
if(aa.equals(bb)){return;}
else {
text.setText(text.getText()+".");
}
}
else if(e.getSource()==jd)
{ if(text.getText().length()>0){
double aa,bb;
aa=Double.valueOf(text.getText()).doubleValue();
bb=-(aa);
text.setText(String.valueOf(bb));
}
}
else if(e.getSource()==zero)
{StringBuffer aaa=new StringBuffer(text.getText());
String aa=new String(aaa);
String bb="0";
if(aa.equals(bb)&&aa.length()==1){}
else
text.setText(text.getText()+"0");
}
else if(e.getSource()==cl)
{ text.setText("");
opt1=0;
opt2=0;
}
else if(e.getSource()==a)
{if(text.getText().length()>0)
{ flag=1;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");}
}
else if(e.getSource()==b)
{ if(text.getText().length()>0)
{ flag=2;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");}
}
else if(e.getSource()==c)
{if(text.getText().length()>0){
flag=3;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}}
else if(e.getSource()==d)
{if(text.getText().length()>0){
flag=4;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");}
}
else if(e.getSource()==de)
{if(text.getText().length()>0){
opt2=Double.valueOf(text.getText()).doubleValue();
if(flag==1){
opt1=opt1+opt2;
text.setText(String.valueOf(opt1));
}
if(flag==2){
opt1=opt1-opt2;
text.setText(String.valueOf(opt1));
}
if(flag==3){
opt1=opt1*opt2;
text.setText(String.valueOf(opt1));
}
if(flag==4){
if(opt2==0)
{
text.setText("除数不可以为零");
}
else {opt1=opt1/opt2;
text.setText(String.valueOf(opt1));
}
}
}}
}
public static void main(String args[])
{ff mf =new ff();
mf.setSize(250,205);
mf.setResizable(false);
mf.setVisible(true);
}
public class KeyEventHandler implements KeyListener
{public void keyPressed(KeyEvent Ke)
{
if(Ke.getKeyChar()=='1')
text.setText(text.getText()+"1");
else if(Ke.getKeyChar()=='2')
text.setText(text.getText()+"2");
else if(Ke.getKeyChar()=='3')
text.setText(text.getText()+"3");
else if(Ke.getKeyChar()=='4')
text.setText(text.getText()+"4");
else if(Ke.getKeyChar()=='5')
text.setText(text.getText()+"5");
else if(Ke.getKeyChar()=='6')
text.setText(text.getText()+"6");
else if(Ke.getKeyChar()=='7')
text.setText(text.getText()+"7");
else if(Ke.getKeyChar()=='8')
text.setText(text.getText()+"8");
else if(Ke.getKeyChar()=='9')
text.setText(text.getText()+"9");
else if(Ke.getKeyChar()=='0')
{
StringBuffer aaa=new StringBuffer(text.getText());
String aa=new String(aaa);
String bb="0";
if(aa.equals(bb)&&aa.length()==1){}
else
text.setText(text.getText()+"0");
}
else if(Ke.getKeyChar()=='.')
{ int gm;
gm=text.getText().length();
StringBuffer aaa=new StringBuffer(text.getText().substring(gm-1));
String aa=new String(aaa);
String bb=".";
if(aa.equals(bb)){return;}
else {
text.setText(text.getText()+".");
}
}//
else if(Ke.getKeyChar()=='+')
{
if(text.getText().length()>0)
{ flag=1;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}
}//
else if(Ke.getKeyChar()=='-')
{
if(text.getText().length()>0)
{ flag=2;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}
}//
else if(Ke.getKeyChar()=='*')
{
if(text.getText().length()>0)
{ flag=3;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}
}//
else if(Ke.getKeyChar()=='/')
{
if(text.getText().length()>0)
{ flag=4;
opt1=Double.valueOf(text.getText()).doubleValue();
text.setText("");
}
}//
else if(Ke.getKeyChar()=='=')
{if(text.getText().length()>0){
opt2=Double.valueOf(text.getText()).doubleValue();
if(flag==1){
opt1=opt1+opt2;
text.setText(String.valueOf(opt1));
}
if(flag==2){
opt1=opt1-opt2;
text.setText(String.valueOf(opt1));
}
if(flag==3){
opt1=opt1*opt2;
text.setText(String.valueOf(opt1));
}
if(flag==4){
if(opt2==0)
{
text.setText("除数不可以为零");
}
else {opt1=opt1/opt2;
text.setText(String.valueOf(opt1));
}
}
}} //
else if(Ke.getKeyChar()=='q')
{System.exit(0);}
else if(Ke.getKeyChar()=='b')
{
if(text.getText().equals(""))return;
else{
StringBuffer sb=new StringBuffer(text.getText());
sb.deleteCharAt(sb.length()-1);
String s9=new String(sb);
text.setText(s9);}}
else if(Ke.getKeyChar()=='d')
{text.setText("");
opt1=0;
opt2=0;}
}
public void keyReleased(KeyEvent Ke)
{}
public void keyTyped(KeyEvent Ke)
{}
}
}
问题还是很多的大家帮我看看呀
写的很垃圾 和你们的没法比 大家看看帮帮我呀!
[此贴子已经被作者于2006-5-17 18:50:59编辑过]