日期:2014-05-20 浏览次数:20902 次
package com;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Caculator extends JFrame implements ActionListener,KeyListener{
/**
*
*/
private static final long serialVersionUID = 5204982079673572494L;
private JTextField tf=new JTextField();
private float x=0;
private float y=0;
private int code=0;
private boolean enable;
private boolean first;
private String str="";
public Caculator(){
Container ct=this.getContentPane();
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
tf.setHorizontalAlignment(JTextField.RIGHT);
//tf.setText("0");
enable=true;
first=true;
ct.add(tf,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,4));
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Caculator.this,"确定要关闭程序吗?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}
}
});
Button btn=null;
btn=new Button("1");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("2");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("3");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("+");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("4");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("5");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("6");
panel.add(btn);
btn.addActionListener(this);