java编写的计算器_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java编写的计算器

java编写的计算器

 2012/2/27 9:38:12  c514858501  程序员俱乐部  我要评论(0)
  • 摘要:importjava.awt.Color;importjava.awt.Component;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event
  • 标签:Java
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
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 javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class Calculator1 extends JFrame implements ActionListener{
JPanel jp1,jp2,jp3;
JTextField jtf;
JButton jb0,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jbAdd,jbSub,jbMul,jbDiv,jbR,jbAC,jbAbout,jbCancer,jbSur,jbD;
boolean add,sub,end,mul,div,sur;
String str;
Double num1,num2;

public Calculator1(){
jp1 = new JPanel();
jtf= new JTextField("0",20);
jtf.setHorizontalAlignment(JTextField.RIGHT);
jtf.setForeground(Color.BLUE);
jtf.setEditable(false);
TitledBorder tb= new TitledBorder("输出");
tb.setTitleColor(Color.RED);
jp1.add(jtf);
jp1.setBorder(tb);

jp2 = new JPanel();
jb0 = new JButton("0");     jb0.addActionListener(this);   
jb1 = new JButton("1");     jb1.addActionListener(this);  
jb2 = new JButton("2");     jb2.addActionListener(this);
jb3 = new JButton("3");     jb3.addActionListener(this);  
jb4 = new JButton("4");     jb4.addActionListener(this);
jb5 = new JButton("5");     jb5.addActionListener(this);
jb6 = new JButton("6"); jb6.addActionListener(this);
jb7 = new JButton("7"); jb7.addActionListener(this);
jb8 = new JButton("8"); jb8.addActionListener(this);
jb9 = new JButton("9"); jb9.addActionListener(this);
jbAdd = new JButton("+"); jbAdd.addActionListener(this);
jbSub = new JButton("-"); jbSub.addActionListener(this);
jbMul = new JButton("*"); jbMul.addActionListener(this);
jbDiv = new JButton("/"); jbDiv.addActionListener(this);
jbSur=new JButton("%");     jbSur.addActionListener(this);
jbD=new JButton(".");     jbD.addActionListener(this);
jbAC = new JButton("AC"); jbAC.addActionListener(this);
jbR= new JButton("="); jbR.addActionListener(this);
jp2.add(jb0);    
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
jp2.add(jb5);
jp2.add(jb6);
jp2.add(jb7);
jp2.add(jb8);
jp2.add(jb9);
jp2.add(jbAdd);
jp2.add(jbMul);
jp2.add(jbDiv);
jp2.add(jbSub);
jp2.add(jbSur);
jp2.add(jbD);
jp2.add(jbAC);
jp2.add(jbR);
jp2.setLayout(new GridLayout(4, 4));

jp3 = new JPanel();
jbAbout = new JButton("关于");   jbAbout.addActionListener(this);
jbCancer = new JButton("关闭");  jbCancer.addActionListener(this);
jp3.add(jbAbout);
jp3.add(jbCancer);

this.add(jp1,"North");
this.add(jp2,"Center");
this.add(jp3,"South");





}
public static void main(String[] args) {

Calculator1 c = new Calculator1();
c.setSize(300,280);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setResizable(false);
c.pack();
c.setVisible(true);
c.setLocationRelativeTo(null);   //将窗口显示在屏幕中央
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb0){
num(0);
}
else if(e.getSource()==jb1){
num(1);
}
else if(e.getSource()==jb2){
num(2);
}
else if(e.getSource()==jb3){
num(3);
}
else if(e.getSource()==jb4){
num(4);
}
else if(e.getSource()==jb5){
num(5);
}
else if(e.getSource()==jb6){
num(6);
}
else if(e.getSource()==jb7){
num(7);
}
else if(e.getSource()==jb8){
num(8);
}
else if(e.getSource()==jb9){
num(9);
}
else if(e.getSource()==jbAdd){
jbD.setEnabled(true);
sign(1);
}
else if(e.getSource()==jbSub){
jbD.setEnabled(true);
sign(2);
}
else if(e.getSource()==jbMul){
jbD.setEnabled(true);
sign(3);
}
else if(e.getSource()==jbDiv){
jbD.setEnabled(true);
sign(4);
}
else if(e.getSource()==jbSur){
jbD.setEnabled(true);
sign(5);
}
else if(e.getSource()==jbD){
str=jtf.getText();
str+=".";
jtf.setText(str);
jbD.setEnabled(false);
}
else if(e.getSource()==jbAC){
jtf.setText("0");
}

else if(e.getSource()==jbR){
jbD.setEnabled(true);
num2 = Double.parseDouble(jtf.getText());
if(add){
num1=num1+num2;
}
else if(sub){
num1=num1-num2;
}
else if(mul){
num1=num1*num2;
}
else if(div){
num1=num1/num2;
}
else if(sur){
num1=num1%num2;
}
jtf.setText(String.valueOf(num1));
        end=true;
}


else if (e.getSource()==jbCancer) {
System.exit(0);
}

else if (e.getSource()==jbAbout) {
JOptionPane.showMessageDialog(null,"陈国权","消息",JOptionPane.INFORMATION_MESSAGE);
}

}
private void sign(int i) {
if(i==1){
add=true;
sub=false;
mul=false;
div=false;
sur=false;
}
else if (i==2) {
add=false;
sub=true;
mul=false;
div=false;
sur=false;
}
else if (i==3) {
add=false;
sub=false;
mul=true;
div=false;
sur=false;
}
else if (i==4) {
add=false;
sub=false;
mul=false;
div=true;
sur=false;
}
else if (i==5) {
add=false;
sub=false;
mul=false;
div=false;
sur=true;
}
num1=Double.parseDouble(jtf.getText());
end=true;
}
private void num(int i) {
String s=null;
s=String.valueOf(i);
if(end){
jtf.setText("0");
end=false;
}
if(jtf.getText().equals("0")) {
jtf.setText(s);
}
else {
str=jtf.getText()+s;
jtf.setText(str);
}

}


}
发表评论
用户名: 匿名