文档库 最新最全的文档下载
当前位置:文档库 › java简单的计算器实现

java简单的计算器实现

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class jisuan extends Frame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
Label dengyu=new Label("=");
Label fuhao=new Label();
JTextField a1=new JTextField();
JTextField a2=new JTextField();
JTextField a3=new JTextField();
JButton jia=new JButton("plus");
JButton jian=new JButton("minuts");
JButton cheng=new JButton("times");
JButton chu=new JButton("divide");
JPanel panel0 = new JPanel();
// MenuBar bar=new MenuBar();
// Menu menuEdit=new Menu("编辑(E)");
// Menu menuView=new Menu("查看(V)");
// Menu menuHelp=new Menu("帮助(H)");
// MenuItem menuEditCopy=new MenuItem("复制(C)");
// MenuItem menuEditPaste=new MenuItem("粘贴(V)");
// MenuItem menuHelpAbout = new MenuItem("关于计算器(A)");
public jisuan(){
setSize(650,200);
setLocation(300,400);
setTitle("计算器");
setBackground(Color.pink);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0){
System.exit(0);
}
});
// a1.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent Event){
// JOptionPane.showMessageDialog(null,"请输入两个整数");
// }
// });
setLayout(null);
a1.setHorizontalAlignment(JTextField.RIGHT);
a2.setHorizontalAlignment(JTextField.RIGHT);
a3.setHorizontalAlignment(JTextField.LEFT);
// this.setMenuBar(bar);
// bar.add(menuEdit);
// bar.add(menuView);
// bar.add(menuHelp);
// menuEdit.add(menuEditCopy);
// menuEdit.add(menuEditPaste);
// menuHelp.add(menuHelpAbout);
fuhao.setSize(10,5);
fuhao.setLocation(237, 87);
a1.setBounds(80,70,150,40);
a2.setBounds(255,70,150,40);
a3.setBounds(425,70,150,40);
dengyu.setLocation(410, 87);
dengyu.setSize(10, 5);
add(a1);
add(a2);
add(dengyu);
add(a3);
jia.setLocation(130, 120);
jia.setSize(75,30);
add(jia);
jian.setLocation(220, 120);
jian.setSize(75, 30);
add(jian);
cheng.setLocation(310, 120);
cheng.setSize(75, 30);
add(cheng);
chu.setLocation(400, 120);
chu.setSize(75, 30);
add(chu);
add(fuhao);
setResizable(false);
setVisible(true);
}//布局结束;
public void start() {
// menuEditCopy.addActionListener(this);
// menuEditPaste.addActionListener(this);
// menuHelpAbout.addActionListener(this);
//this.addWindowListener(new MyWinLis());
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String k;
int a=0,b=0;
double c=0;
try {
a=Integer.parseInt(a1.getText());

b=Integer.parseInt(a2.getText());
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,"请输入两个整数");
}

if(e.getSource()==jia){
c=a+b;fuhao.setText("+");

}
if(e.getSource()==jian) { //jian
c=a-b;fuhao.setText("-");
}
if(e.getSource()==cheng) { //cheng
c=a*b;fuhao.setText("×");
}
if(e.getSource()==chu) {
try {
c=a/b;
} catch (Exception e2) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,"除数不能为零");

}
fuhao.setText("÷");
}


k=String.valueOf(c);
a3.setText(k);
}
class NegativeIntegerException extends Exception{

/**
*
*/
private static final long serialVersionUID = 1L;

}

public static void main(String arg[]){
jisuan aa=(jisuan)new jisuan();
aa.start();
aa.setVisible(true);

}







}

相关文档