/*
* Created on 2009-4-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package colculor;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class demo implements ActionListener{
JFrame frame;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JButton button0;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonPoint;
JButton buttonAdd;
JButton buttonMultiply;
JButton buttonDevi;
JButton buttonMinus;
JButton buttonEqual;
JTextField text;
double num1=0,num2=0,count=10;
double input1=0,input2=0;
double resout=0;
double val1=0;
boolean decimo=false;
String str1;
String str2;
int nflag=1;
public static void main(String[] args) {
demo demo1=new demo();
demo1.go();
}
void go(){
Font f=new Font("隶书",Font.BOLD+Font.ITALIC,15); ////字体设置
frame=new JFrame("Colculator");
frame.setResizable(false); //使窗口最大化按钮失效
panel1=new JPanel();
panel2=new JPanel();
panel3=new JPanel();
button0=new JButton("0");
// button0.setSize(-1,-1);
button1=new JButton("1");
button2=new JButton("2");
button3=new JButton("3");
button4=new JButton("4");
button5=new JButton("5");
button6=new JButton("6");
button7=new JButton("7");
button8=new JButton("8");
button9=new JButton("9");
buttonPoint=new JButton(".");
buttonAdd=new JButton("+");
buttonMinus=new JButton("-");
buttonMultiply=new JButton("*");
buttonDevi=new JButton("/");
buttonEqual=new JButton("=");
text=new JTextField("0",20);
//text.setDefaultLocale();
text.setHorizontalAlignment(JTextField.RIGHT); //使文本域中内容靠右
text.setFont(f);
text.setCaretColor(new Color(0,0,255));
text.setSelectionColor(new Color(255,0,0));
text.setForeground(new Color(50,255,50));
///text.setSize(35,35);
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonPoint.addActionListener(this);
buttonAdd.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDevi.addActionListener(this);
buttonEqual.addActionListener(this);
panel2.setLayout(null);
panel2.add(button7);
//button7.setSize(20,15);
button7.setBounds(10,10,45,25);
panel2.add(button8);
//button8.setSize(20,15);
button8.setBounds(60,10,50,25);
panel2.add(button9);
//button9.setSize(20,15);
button9.setBounds(115,10,55,25);
panel2.add(buttonAdd);
buttonAdd.setBounds(175,10,55,25);
panel2.add(button4);
button4.setBounds(10,50,45,25);
panel2.add(button5);
button5.setBounds(60,50,50,25);
panel2.add(button6);
button6.setBounds(115,50,55,25);
panel2.add(buttonMinus);
buttonMinus.setBounds(175,50,55,25);
panel2.add(button1);
button1.setBounds(10,90,45,25);
panel2.add(button2);
button2.setBounds(60,90,50,25);
panel2.add(button3);
button3.setBounds(115,90,55,25);
panel2.add(buttonMultiply);
buttonMultiply.setBounds(175,90,55,25);
panel2.add(button0);
button0.setBounds(10,130,45,25);
panel2.add(buttonPoint);
buttonPoint.setBounds(60,130,50,25);
panel2.add(buttonEqual);
buttonEqual.setBounds(115,130,55,25);
panel2.add(buttonDevi);
buttonDevi.setBounds(175,130,55,25);
panel3.setLayout(new FlowLayout());
panel3.add(text);
panel1.setLayout(new BorderLayout());
panel1.add(panel3,BorderLayout.NORTH);
panel1.add(panel2);
frame.add(panel1);
frame.setSize(250,250);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(button0==e.getSource()){
if(decimo==false){
input1=input1*10+0;
val1=input1+input2;
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
else{
input2=input2+0/count;
count=10*count;
val1=input1+input2;
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
}
if(button1==e.getSource()){
if(decimo==false){
input1=input1*10+1;
val1=input1+input2;
}
else{
input2=input2+1/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button2==e.getSource()){
if(decimo==false){
input1=input1*10+2;
val1=input1+input2;
}
else{
input2=input2+2/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button3==e.getSource()){
if(decimo==false){
input1=input1*10+3;
val1=input1+input2;
}
else{
input2=input2+3/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button4==e.getSource()){
if(decimo==false){
input1=input1*10+4;
val1=input1+input2;
}
else{
input2=input2+4/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button5==e.getSource()){
if(decimo==false){
input1=input1*10+5;
val1=input1+input2;
}
else{
input2=input2+5/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button6==e.getSource()){
if(decimo==false){
input1=input1*10+6;
val1=input1+input2;
}
else{
input2=input2+6/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button7==e.getSource()){
if(decimo==false){
input1=input1*10+7;
val1=input1+input2;
}
else{
input2=input2+7/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button8==e.getSource()){
if(decimo==false){
input1=input1*10+8;
val1=input1+input2;
}
else{
input2=input2+8/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(button9==e.getSource()){
if(decimo==false){
input1=input1*10+9;
val1=input1+input2;
}
else{
input2=input2+9/count;
count=10*count;
val1=input1+input2;
}
str1=new String();
str1=Double.toString(val1);
text.setText(str1);
}
if(buttonPoint==e.getSource()){
decimo=true;
}
if(buttonAdd==e.getSource()){
str1=new String();
str1=text.getText();
num1=Double.parseDouble(str1);
input1=0;
input2=0;
text.setText("0");
decimo=false;
count=10;
nflag=1;
}
if(buttonMinus==e.getSource()){
str1=new String();
str1=text.getText();
num1=Double.parseDouble(str1);