求一个java文本框型计算器!就是输入数字到文本框里然后点计算按钮再将结果显示到结果文本框中!这应
求一个java文本框型计算器!就是输入数字到文本框里然后点计算按钮再将结果显示到结果文本框中!这应该怎么做?!...
求一个java文本框型计算器!就是输入数字到文本框里然后点计算按钮再将结果显示到结果文本框中!这应该怎么做?!
展开
2个回答
推荐于2016-09-12
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame implements ActionListener
{
JTextField Display = new JTextField(16);
JButton[] btn = new JButton[10];
JButton btnPlus = new JButton(" + ");
JButton btnMinus = new JButton(" - ");
JButton btnMultiply = new JButton(" * ");
JButton btnDivide = new JButton(" / ");
JButton btnEqual = new JButton(" = ");
int num1=0,num2=0,num3=0,count1=0,count2=0;
int operator=0;
Calculator()
{
for(int i=0;i <btn.length;i++)
{
btn[i] = new JButton(" "+String.valueOf(i)+" ");
}
setSize(220,300);
setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
validate();
setLayout(new FlowLayout());
add(Display);
Display.setEditable(false);
for(int i=0;i<10;i++)
{
add(btn[i]);
btn[i].addActionListener(this);
}
add(btnPlus);add(btnMinus);add(btnMultiply);add(btnDivide);add(btnEqual);
btnPlus.addActionListener(this);
btnMinus.addActionListener(this);
btnMultiply.addActionListener(this);
btnDivide.addActionListener(this);
btnEqual.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent A)
{
int i=0;
count1++;
if(A.getSource()!=btnPlus&&A.getSource()!=btnMinus&&A.getSource()!=btnMultiply
&&A.getSource()!=btnDivide&&A.getSource()!=btnEqual)
{
while(i<10)
{
if(A.getSource()==btn[i])
{
num1=i;
if(count1>1)
{
num1=num2*10+i;
}
i=10;
}
i++;
}
num2=num1;
Display.setText(""+num1);
}
else if(A.getSource()==btnPlus)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=1;
count2++;
}
else
{
num3+=num1;
num2=0;
count2++;
operator=1;
}
}
else if(A.getSource()==btnMinus)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=2;
count2++;
}
else
{
num3-=num1;
num2=0;
count2++;
operator=2;
}
}
else if(A.getSource()==btnMultiply)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=3;
count2++;
}
else
{
num3*=num1;
num2=0;
count2++;
operator=3;
}
}
else if(A.getSource()==btnDivide)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=4;
count2++;
}
else
{
num3/=num1;
num2=0;
operator=4;
count2++;
}
}
else if(A.getSource()==btnEqual)
{
switch(operator)
{
case 1:
num3+=num1;
num1=0;
break;
case 2:
num3-=num1;
num1=0;
break;
case 3:
num3*=num1;
num1=1;
break;
case 4:
num3/=num1;
num1=0;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
break;
}
Display.setText(""+num3);
count1=0;
count2=0;
}
}
}
public class CallMeKamy
{
public static void main(String[] args)
{
new Calculator();
}
}
我真大方 JAVA文件名新建为CallMeKamy
import java.awt.*;
import java.awt.event.*;
class Calculator extends JFrame implements ActionListener
{
JTextField Display = new JTextField(16);
JButton[] btn = new JButton[10];
JButton btnPlus = new JButton(" + ");
JButton btnMinus = new JButton(" - ");
JButton btnMultiply = new JButton(" * ");
JButton btnDivide = new JButton(" / ");
JButton btnEqual = new JButton(" = ");
int num1=0,num2=0,num3=0,count1=0,count2=0;
int operator=0;
Calculator()
{
for(int i=0;i <btn.length;i++)
{
btn[i] = new JButton(" "+String.valueOf(i)+" ");
}
setSize(220,300);
setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
validate();
setLayout(new FlowLayout());
add(Display);
Display.setEditable(false);
for(int i=0;i<10;i++)
{
add(btn[i]);
btn[i].addActionListener(this);
}
add(btnPlus);add(btnMinus);add(btnMultiply);add(btnDivide);add(btnEqual);
btnPlus.addActionListener(this);
btnMinus.addActionListener(this);
btnMultiply.addActionListener(this);
btnDivide.addActionListener(this);
btnEqual.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent A)
{
int i=0;
count1++;
if(A.getSource()!=btnPlus&&A.getSource()!=btnMinus&&A.getSource()!=btnMultiply
&&A.getSource()!=btnDivide&&A.getSource()!=btnEqual)
{
while(i<10)
{
if(A.getSource()==btn[i])
{
num1=i;
if(count1>1)
{
num1=num2*10+i;
}
i=10;
}
i++;
}
num2=num1;
Display.setText(""+num1);
}
else if(A.getSource()==btnPlus)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=1;
count2++;
}
else
{
num3+=num1;
num2=0;
count2++;
operator=1;
}
}
else if(A.getSource()==btnMinus)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=2;
count2++;
}
else
{
num3-=num1;
num2=0;
count2++;
operator=2;
}
}
else if(A.getSource()==btnMultiply)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=3;
count2++;
}
else
{
num3*=num1;
num2=0;
count2++;
operator=3;
}
}
else if(A.getSource()==btnDivide)
{
if(count2==0)
{
num3=num1;
num2=0;
operator=4;
count2++;
}
else
{
num3/=num1;
num2=0;
operator=4;
count2++;
}
}
else if(A.getSource()==btnEqual)
{
switch(operator)
{
case 1:
num3+=num1;
num1=0;
break;
case 2:
num3-=num1;
num1=0;
break;
case 3:
num3*=num1;
num1=1;
break;
case 4:
num3/=num1;
num1=0;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 0:
break;
}
Display.setText(""+num3);
count1=0;
count2=0;
}
}
}
public class CallMeKamy
{
public static void main(String[] args)
{
new Calculator();
}
}
我真大方 JAVA文件名新建为CallMeKamy
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询