用Java编写计算器,请高手帮我解决余下的代码或者重写一下,感谢了!!!
发布网友
发布时间:2022-04-24 20:26
我来回答
共5个回答
热心网友
时间:2023-10-09 20:49
最近刚好在写计算器的 这是我自己写你 你看看吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class calculator extends JFrame implements ActionListener {
boolean i=true,clickable=true;
double storedNumber,currentNumber;
char lastoperate='?';
JFrame jframe=new JFrame("计算器");
JButton plu=new JButton("+");
JButton min=new JButton("-");
JButton equ=new JButton("=");
JButton div=new JButton("/");
JTextField text1=new JTextField("0");
JButton code=new JButton("-/+");
JButton point=new JButton(".");
JButton num1=new JButton("1");
JButton num2=new JButton("2");
JButton num3=new JButton("3");
JButton num4=new JButton("4");
JButton num5=new JButton("5");
JButton num6=new JButton("6");
JButton num7=new JButton("7");
JButton num8=new JButton("8");
JButton num9=new JButton("9");
JButton num0=new JButton("0");
JButton clear=new JButton("E");
JButton mul=new JButton("*");
JButton log2=new JButton("log2");
calculator(){
storedNumber = 0;
jframe.setSize(300, 200);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//jframe.setBackground(Color.red);
JPanel p=new JPanel();
p.setBackground(Color.gray);
p.setLayout(new GridLayout(5, 4));
plu.addActionListener(this);
equ.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
clear.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
num0.addActionListener(this);
min.addActionListener(this);
code.addActionListener(this);
div.addActionListener(this);
point.addActionListener(this);
mul.addActionListener(this);
log2.addActionListener(this);
jframe.add(p);
p.add(num1);
p.add(num2);
p.add(num3);
p.add(clear);
p.add(num4);
p.add(num5);
p.add(num6);
p.add(plu);
p.add(num7);
p.add(num8);
p.add(num9);
p.add(min);
p.add(code);
p.add(num0);
p.add(point);
p.add(mul);
p.add(div);
p.add(equ);
p.add(log2);
jframe.add(text1,"North");
jframe.setLocation(300, 300);
//setContentPane(p);
jframe.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String str1,str2,str3,str4;
double value;
Object soc=evt.getSource();
if(soc==plu)
{//加
str1=text1.getText();
switch(lastoperate){
case '?' : storedNumber=Double.parseDouble(str1.trim());
lastoperate='+';
i=true;
break;
case '+' : storedNumber+=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='+';
i=true;
break;
case '=' : storedNumber=Double.parseDouble(str1.trim());
lastoperate='+';
i=true;
break;
case '-' : storedNumber-=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='+';
i=true;
break;
case'/': storedNumber=storedNumber/Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='+';
i=true;
break;
case'*' : storedNumber=storedNumber*Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='+';
i=true;
break;
}
}
if(soc==min){
str1=text1.getText();
switch(lastoperate){
case'?': storedNumber=Double.parseDouble(str1.trim());
lastoperate='-';
i=true;
break;
case'+': storedNumber+=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='-';
i=true;
break;
case'-': storedNumber-=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='-';
i=true;
break;
case'=' : storedNumber=Double.parseDouble(str1.trim());
lastoperate='-';
i=true;
break;
case'/': storedNumber=storedNumber/Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='-';
i=true;
break;
case'*' : storedNumber=storedNumber*Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='*';
i=true;
break;
}
}
if(soc==mul){
str1=text1.getText();
switch(lastoperate){
case'?': storedNumber=Double.parseDouble(str1.trim());
lastoperate='*';
i=true;
break;
case'+': storedNumber+=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='*';
i=true;
break;
case'-' : storedNumber-=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='*';
i=true;
break;
case'=' : storedNumber=Double.parseDouble(str1.trim());
lastoperate='*';
i=true;
break;
case'/': storedNumber=storedNumber/Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='*';
i=true;
break;
case'*' : storedNumber=storedNumber*Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='*';
i=true;
break;
}
}
if(soc==div){
str1=text1.getText();
switch(lastoperate){
case'?': storedNumber=Double.parseDouble(str1.trim());
lastoperate='/';
i=true;
break;
case'+': storedNumber+=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='/';
i=true;
break;
case'-' : storedNumber-=Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='/';
i=true;
break;
case'=' : storedNumber=Double.parseDouble(str1.trim());
lastoperate='/';
i=true;
break;
case'/': storedNumber=storedNumber/Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='/';
i=true;
break;
case'*' : storedNumber=storedNumber*Double.parseDouble(str1.trim());
value=storedNumber;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='/';
i=true;
break;
}
}
if(soc==equ)
{//等
str2=text1.getText();
switch(lastoperate){
case'+': currentNumber=Double.parseDouble(str2.trim());
value=currentNumber+storedNumber;
storedNumber=value;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='=';
i=true;
break;
case'-': currentNumber=Double.parseDouble(str2.trim());
value=storedNumber-currentNumber;
storedNumber=value;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='=';
i=true;
break;
case'/': currentNumber=Double.parseDouble(str2.trim());
value=storedNumber/currentNumber;
storedNumber=value;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='=';
i=true;
break;
case'*':currentNumber=Double.parseDouble(str2.trim());
value=storedNumber*currentNumber;
storedNumber=value;
str3=java.lang.String.valueOf(value);
text1.setText(str3);
lastoperate='=';
i=true;
break;
}
}
if(soc==num1){
if(i==true)
{
text1.setText("1");
i=false;
}
else text1.setText(text1.getText()+'1');
}
if(soc==num2){
if(i==true)
{
text1.setText("2");
i=false;
}
else text1.setText(text1.getText()+'2');
}
if(soc==num3){
if(i==true)
{
text1.setText("3");
i=false;
}
else text1.setText(text1.getText()+'3');
}
if(soc==num4){
if(i==true)
{
text1.setText("4");
i=false;
}
else text1.setText(text1.getText()+'4');
}
if(soc==num5){
if(i==true)
{
text1.setText("5");
i=false;
}
else text1.setText(text1.getText()+'5');
}
if(soc==num6){
if(i==true)
{
text1.setText("6");
i=false;
}
else text1.setText(text1.getText()+'6');
}
if(soc==num7){
if(i==true)
{
text1.setText("7");
i=false;
}
else text1.setText(text1.getText()+'7');
}
if(soc==num8){
if(i==true)
{
text1.setText("8");
i=false;
}
else text1.setText(text1.getText()+'8');
}
if(soc==num9){
if(i==true)
{
text1.setText("9");
i=false;
}
else text1.setText(text1.getText()+'9');
}
if(soc==point){
clickable=true;
for (int i = 0; i < text1.getText().length(); i++)
if ('.' == text1.getText().charAt(i))
{
clickable=false;
break;
} //第一层判断是否里面含有小数点;
if(clickable==true)//第二坛判断
text1.setText(text1.getText()+".");
i=false;
}
if(soc==code){
double a = 0;
str4=text1.getText();
a=Double.parseDouble(str4.trim());
a=-1*a;
str4=java.lang.String.valueOf(a);
text1.setText(str4);
}
if(soc==num0){
if(i==true)
{
i=true;
}
else text1.setText(text1.getText()+'0');
}
if(soc==log2){
str1=text1.getText();
double m=Math.log10(Double.parseDouble(str1.trim()))/Math.log10(2);
str3=java.lang.String.valueOf(m);
text1.setText(str3);
i=true;
}
if(soc==clear){//清零
lastoperate='?';
storedNumber=0;
currentNumber=0;
value=0;
text1.setText("0");
i=true;
}
repaint();
}
private void operate(char operator)
{
double currentNumber = Double.valueOf(text1.getText()).doubleValue();
}
public static void main(String[] arg)
{
calculator cal=new calculator();
}
}
热心网友
时间:2023-10-09 20:50
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class jisuanqi extends JFrame implements ActionListener
{
static MenuBar mb=new MenuBar();
static Menu m1=new Menu("查看");
static Menu m2=new Menu("帮助");
static Menu m3=new Menu("关于");
static MenuItem m11=new MenuItem("作者");
static MenuItem m12=new MenuItem("退出");
static MenuItem m21=new MenuItem("操作说明");
static MenuItem m31=new MenuItem("联系方式");
static String a = "";
static String c,x,p;
static double b,d;
static jisuanqi frm = new jisuanqi();
static JTextField lb1 = new JTextField(a);
static JPanel lb2 = new JPanel();
static JPanel lb3 = new JPanel();
static JPanel lb5 = new JPanel();
static JButton x1 = new JButton("1");
static JButton x2 = new JButton("2");
static JButton x3 = new JButton("3");
static JButton x4 = new JButton("4");
static JButton x5 = new JButton("5");
static JButton x6 = new JButton("6");
static JButton x7 = new JButton("7");
static JButton x8 = new JButton("8");
static JButton x9 = new JButton("9");
static JButton x10 = new JButton("0");
static JButton x11 = new JButton("+");
static JButton x12 = new JButton("-");
static JButton x13 = new JButton("*");
static JButton x14 = new JButton("/");
static JButton x15 = new JButton(".");
static JButton x16 = new JButton("=");
static JLabel lb4 = new JLabel("北方民族大学计算机学院08软件一班");
static JLabel lb6 = new JLabel();
static Date nowTime=new Date();
static SimpleDateFormat time=new SimpleDateFormat("yyyy-mm-dd");
static String Time = time.format(nowTime);
static JButton x17 = new JButton("重新计算");
static JButton x18 = new JButton("退出程序");
static class ActLis implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if(btn==x1)
{
a=a+x1.getText();
lb1.setText(a);
}
if(btn==x2)
{
a=a+x2.getText();
lb1.setText(a);
}
if(btn==x3)
{
a=a+x3.getText();
lb1.setText(a);
}
if(btn==x4)
{
a=a+x4.getText();
lb1.setText(a);
}
if(btn==x5)
{
a=a+x5.getText();
lb1.setText(a);
}
if(btn==x6)
{
a=a+x6.getText();
lb1.setText(a);
}
if(btn==x7)
{
a=a+x7.getText();
lb1.setText(a);
}
if(btn==x8)
{
a=a+x8.getText();
lb1.setText(a);
}
if(btn==x9)
{
a=a+x9.getText();
lb1.setText(a);
}
if(btn==x10)
{
a=a+x10.getText();
lb1.setText(a);
}
if(btn==x11)
{
b=Double.parseDouble(a);
c=x11.getText();
a="";
lb1.setText(a);
}
if(btn==x12)
{
b=Double.parseDouble(a);
c=x12.getText();
a="";
lb1.setText(a);
}
if(btn==x13)
{
b=Double.parseDouble(a);
c=x13.getText();
a="";
lb1.setText(a);
}
if(btn==x14)
{
b=Double.parseDouble(a);
c=x14.getText();
a="";
lb1.setText(a);
}
if(btn==x15)
{
b=Double.parseDouble(a);
c=x15.getText();
a="";
lb1.setText(a);
}
if(btn==x16)
{
if(c=="+")
{
d=Double.parseDouble(a);
x=Double.toString(b+d);
lb1.setText(x);
a=x;
}
if(c=="-")
{
d=Double.parseDouble(a);
x=Double.toString(b-d);
lb1.setText(x);
a=x;
}
if(c=="*")
{
d=Double.parseDouble(a);
x=Double.toString(b*d);
lb1.setText(x);
a=x;
}
if(c=="/")
{
d=Double.parseDouble(a);
x=Double.toString(b/d);
lb1.setText(x);
a=x;
}
}
if(btn==x17)
{
a="";
lb1.setText(a);
}
if(btn==x18)
{
System.exit(0);
}
}
}
public static void main(String[] args) {
GridLayout m = new GridLayout(4, 4, 5, 5);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//实现关闭窗体
frm.setBounds(450, 180, 300, 460);
frm.setLayout(null);
lb2.setLayout(m);
lb2.setBounds(23, 120, 250, 200);
lb3.setLayout(null);
lb3.setBounds(0, 0, 300, 100);
lb5.setLayout(null);
lb5.setBounds(0,320,300,110);
lb3.add(lb1);
lb1.setBounds(24, 50, 247, 40);
lb1.setHorizontalAlignment(JTextField.RIGHT);//swing中的右对齐
lb2.add(x1);
lb2.add(x2);
lb2.add(x3);
lb2.add(x4);
lb2.add(x5);
lb2.add(x6);
lb2.add(x7);
lb2.add(x8);
lb2.add(x9);
lb2.add(x10);
lb2.add(x11);
lb2.add(x12);
lb2.add(x13);
lb2.add(x14);
lb2.add(x15);
lb2.add(x16);
lb5.add(lb4);
lb5.add(lb6);
lb5.add(x17);
lb5.add(x18);
lb4.setBounds(37,52,250,30);
lb6.setBounds(200,76, 80, 20);
lb6.setText(Time);
x17.setBounds(23,8,90,35);
x18.setBounds(180,8,90,35);
mb.add(m1);
mb.add(m2);
mb.add(m3);
m1.add(m11);
m1.add(m12);
m2.add(m21);
m3.add(m31);
frm.setMenuBar(mb);
frm.add(lb2);
frm.add(lb3);
frm.add(lb5);
frm.setTitle("小计算器");
frm.setVisible(true);
frm.setResizable(false);
x1.addActionListener(new ActLis());
x2.addActionListener(new ActLis());
x3.addActionListener(new ActLis());
x4.addActionListener(new ActLis());
x5.addActionListener(new ActLis());
x6.addActionListener(new ActLis());
x7.addActionListener(new ActLis());
x8.addActionListener(new ActLis());
x9.addActionListener(new ActLis());
x10.addActionListener(new ActLis());
x11.addActionListener(new ActLis());
x12.addActionListener(new ActLis());
x13.addActionListener(new ActLis());
x14.addActionListener(new ActLis());
x15.addActionListener(new ActLis());
x16.addActionListener(new ActLis());
x17.addActionListener(new ActLis());
x18.addActionListener(new ActLis());
m31.addActionListener(frm);
}
public void actionPerformed(ActionEvent e)
{
jisuanqi frm1 = new jisuanqi();
JLabel xx=new JLabel("Email:libowudi321@163.com");
xx.setHorizontalAlignment(JTextField.CENTER);
frm1.add(xx);
frm1.setVisible(true);
frm1.setBounds(500, 300, 190, 130);
}
}
热心网友
时间:2023-10-09 20:50
程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Calculator extends JFrame implements ActionListener{
int n=0;
double b1,b2;
double js;
StringBuffer s1=new StringBuffer();
StringBuffer s2=new StringBuffer();
String d1=new String();
String d2=new String();
String s="+";
JPanel jp1,jp2;
JTextArea jt,jt1,jt2,jt3;
JButton jb1;
JButton jb2;
JButton jb3;
JButton jb4;
JButton jb5;
JButton jb6;
JButton jb7;
JButton jb8;
JButton jb9;
JButton jb0;
JButton add;
JButton subtract;
JButton multiply;
JButton divide;
JButton drop;
JButton equal;
public Calculator(){
jp1=new JPanel();
jp2=new JPanel();
jt=new JTextArea();
jt1=new JTextArea();
jt2=new JTextArea();
jt3=new JTextArea();
jb1=new JButton("1");
jb2=new JButton("2");
jb3=new JButton("3");
jb4=new JButton("4");
jb5=new JButton("5");
jb6=new JButton("6");
jb7=new JButton("7");
jb8=new JButton("8");
jb9=new JButton("9");
jb0=new JButton("0");
add=new JButton("+");
subtract=new JButton("-");
multiply=new JButton("*");
divide=new JButton("/");
drop=new JButton(".");
equal=new JButton("=");
jp1.setLayout(new GridLayout(4,1));
jp2.setLayout(new GridLayout(4,4));
jp1.add(jt1);
jp1.add(jt2);
jp1.add(jt3);
jp1.add(jt);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(add);
jp2.add(jb4);
jp2.add(jb5);
jp2.add(jb6);
jp2.add(subtract);
jp2.add(jb7);
jp2.add(jb8);
jp2.add(jb9);
jp2.add(multiply);
jp2.add(jb0);
jp2.add(drop);
jp2.add(equal);
jp2.add(divide);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
jb8.addActionListener(this);
jb9.addActionListener(this);
jb0.addActionListener(this);
add.addActionListener(this);
subtract.addActionListener(this);
multiply.addActionListener(this);
divide.addActionListener(this);
drop.addActionListener(this);
equal.addActionListener(this);
setBounds(100,100,300,300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
char a[];
JButton jb=(JButton)e.getSource();
if(jb==add||jb==subtract||jb==multiply||jb==divide){
s=jb.getText();
jt2.setText(s);
n=-1;
}
if(n==0&&(jb==jb1||jb==jb2||jb==jb3||jb==jb4||jb==jb5||jb==jb6||jb==jb7||jb==jb8||jb==jb9||jb==jb0||jb==drop)){
s1.append(jb.getText());
n=0;
jt1.setText("");
jt1.setText(new String(s1));
jt.setText("");
}
if(n==-1&&(jb==jb1||jb==jb2||jb==jb3||jb==jb4||jb==jb5||jb==jb6||jb==jb7||jb==jb8||jb==jb9||jb==jb0||jb==drop)){
s2.append(jb.getText());
jt3.setText("");
jt3.setText(new String(s2));
}
if(e.getSource()==equal){
d1=new String(s1);
d2=new String(s2);
b1=Double.parseDouble(d1);
b2=Double.parseDouble(d2);
a=s.toCharArray();
switch(a[0]){
case '+': js=b1+b2;
break;
case '-': js=b1-b2;
break;
case '*': js=b1*b2;
break;
case '/': js=b1/b2;
}
jt.setText(js+"");
n=0;
s1=new StringBuffer();
s2=new StringBuffer();
}
}
public static void main(String args[]){
new CalculatorP();
}
}
热心网友
时间:2023-10-09 20:51
public class Calculator extends JFrame implements ActionListener {
private JPanel jp1;
private JPanel jp2;
private JTextField jt;
private JButton[] buttons = new JButton[16];
public Calculator () {
jp1 = new JPanel();
jp2 = new JPanel();
jt = new JTextField("");
String ch[] = new String[]{"+","-","*","/",".","="};
for(int i=0; i<16; i++) {
if(i>9) {
buttons[i] = new JButton(ch[i-10]);
}else {
buttons[i]= new JButton(i+"");
}
jp2.add(buttons[i]);
}
add(jp1);
add(jp2);
jp1.add(jt);
setLayout(null);
jp1.setBounds(0, 0, 280, 30);
jp1.setLayout(null);
jt.setBounds(0, 0, 280, 30);
jp2.setBounds(0, 0, 400, 300);
jp2.setLayout(null);
/*你这里写的这一段完全可以用我刚才的方式通过循环来控制,自己调吧,你那样写,写的太繁琐了*/
setSize(300, 320);
setLocation(300, 200);
setVisible(true);
}
}
希望采纳,有不会的449060843留言咨询,加也行
热心网友
时间:2023-10-09 20:52
350555602 或qq邮箱
san381@21cn.com
请参考