java 计算器 源代码 后边计算部分
importjava.awt.*;importjava.awt.event.*;abstractclassjisuanqiextendsFrameimplementsAc...
import java.awt.*;
import java.awt.event.*;
abstract class jisuanqi extends Frame implements ActionListener
{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
TextField t1;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15;
public static void main(String[] args)//程序入口
{
Frame f=new Frame("计算器");
Panel P = new Panel();
P.setLayout(new GridLayout(4,4,1,1));
P.setSize(200,200);
//创建本窗体对象
}
jisuanqi()//构造方法
{
super("四则运算");
setBounds(100,100,300,200);
setVisible(true);
b1=new Button("+");
b2=new Button("-");
b3=new Button("*");
b4=new Button("/");
b5=new Button("1");
b6=new Button("2");
b7=new Button("3");
b8=new Button("4");
b9=new Button("5");
b10=new Button("6");
b11=new Button("7");
b12=new Button("8");
b13=new Button("9");
b14=new Button("0");
b15=new Button(".");
t1=new TextField(15);
setLayout(new FlowLayout());//更改窗体框架的布局
add(t1);
add(l1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
}//构造方法结束
} 展开
import java.awt.event.*;
abstract class jisuanqi extends Frame implements ActionListener
{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
TextField t1;
Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15;
public static void main(String[] args)//程序入口
{
Frame f=new Frame("计算器");
Panel P = new Panel();
P.setLayout(new GridLayout(4,4,1,1));
P.setSize(200,200);
//创建本窗体对象
}
jisuanqi()//构造方法
{
super("四则运算");
setBounds(100,100,300,200);
setVisible(true);
b1=new Button("+");
b2=new Button("-");
b3=new Button("*");
b4=new Button("/");
b5=new Button("1");
b6=new Button("2");
b7=new Button("3");
b8=new Button("4");
b9=new Button("5");
b10=new Button("6");
b11=new Button("7");
b12=new Button("8");
b13=new Button("9");
b14=new Button("0");
b15=new Button(".");
t1=new TextField(15);
setLayout(new FlowLayout());//更改窗体框架的布局
add(t1);
add(l1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
}//构造方法结束
} 展开
2个回答
展开全部
简单的实现了下
import java.awt.*;
import java.awt.event.*;
public class jisuanqi extends Frame implements ActionListener {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,b16;
TextField t1;
Label l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15;
@SuppressWarnings("serial")
public static void main(String[] args)// 程序入口
{
Frame f = new Frame("计算器");
Panel P = new Panel();
P.setLayout(new GridLayout(4, 4, 1, 1));
P.setSize(200, 200);
// 创建本窗体对象
new jisuanqi() ;
}
jisuanqi()// 构造方法
{
super("四则运算");
setBounds(100, 100, 300, 200);
setVisible(true);
b1 = new Button("+");
b2 = new Button("-");
b3 = new Button("*");
b4 = new Button("/");
b5 = new Button("1");
b6 = new Button("2");
b7 = new Button("3");
b8 = new Button("4");
b9 = new Button("5");
b10 = new Button("6");
b11 = new Button("7");
b12 = new Button("8");
b13 = new Button("9");
b14 = new Button("0");
b15 = new Button(".");
b16 = new Button("=");
b1.setActionCommand("+");
b2.setActionCommand("-");
b3.setActionCommand("*");
b4.setActionCommand("/");
b5.setActionCommand("1");
b6.setActionCommand("2");
b7.setActionCommand("3");
b8.setActionCommand("4");
b9.setActionCommand("5");
b10.setActionCommand("6");
b11.setActionCommand("7");
b12.setActionCommand("8");
b13.setActionCommand("9");
b14.setActionCommand("0");
b15.setActionCommand(".");
b16.setActionCommand("=");
t1 = new TextField(15);
setLayout(new FlowLayout());// 更改窗体框架的布局
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}// 构造方法结束
//计算部分在这实现
double n1 = 0;
double n2 = 0;
double res = 0;
char xx = '+';
boolean c = false;
@Override
public void actionPerformed(ActionEvent e) {
char x = e.getActionCommand().toCharArray()[0];
switch(x)
{
case '+':
case '-':
case '*':
case '/':
xx = x;
n1 = Double.parseDouble(t1.getText());
t1.setText("");
break;
case '=':
n2 = Double.parseDouble(t1.getText());
switch(xx)
{
case '+':
res = n1+n2;
break;
case '-':
res = n1-n2;
break;
case '*':
res = n1*n2;
break;
case '/':
res = n1/n2;
break;
}
t1.setText(res+"");
c = true;
break;
default:
if(c)
{
c = false;
t1.setText("");
}
t1.setText(t1.getText()+e.getActionCommand());
}
}
}
import java.awt.*;
import java.awt.event.*;
public class jisuanqi extends Frame implements ActionListener {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,b16;
TextField t1;
Label l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15;
@SuppressWarnings("serial")
public static void main(String[] args)// 程序入口
{
Frame f = new Frame("计算器");
Panel P = new Panel();
P.setLayout(new GridLayout(4, 4, 1, 1));
P.setSize(200, 200);
// 创建本窗体对象
new jisuanqi() ;
}
jisuanqi()// 构造方法
{
super("四则运算");
setBounds(100, 100, 300, 200);
setVisible(true);
b1 = new Button("+");
b2 = new Button("-");
b3 = new Button("*");
b4 = new Button("/");
b5 = new Button("1");
b6 = new Button("2");
b7 = new Button("3");
b8 = new Button("4");
b9 = new Button("5");
b10 = new Button("6");
b11 = new Button("7");
b12 = new Button("8");
b13 = new Button("9");
b14 = new Button("0");
b15 = new Button(".");
b16 = new Button("=");
b1.setActionCommand("+");
b2.setActionCommand("-");
b3.setActionCommand("*");
b4.setActionCommand("/");
b5.setActionCommand("1");
b6.setActionCommand("2");
b7.setActionCommand("3");
b8.setActionCommand("4");
b9.setActionCommand("5");
b10.setActionCommand("6");
b11.setActionCommand("7");
b12.setActionCommand("8");
b13.setActionCommand("9");
b14.setActionCommand("0");
b15.setActionCommand(".");
b16.setActionCommand("=");
t1 = new TextField(15);
setLayout(new FlowLayout());// 更改窗体框架的布局
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
}// 构造方法结束
//计算部分在这实现
double n1 = 0;
double n2 = 0;
double res = 0;
char xx = '+';
boolean c = false;
@Override
public void actionPerformed(ActionEvent e) {
char x = e.getActionCommand().toCharArray()[0];
switch(x)
{
case '+':
case '-':
case '*':
case '/':
xx = x;
n1 = Double.parseDouble(t1.getText());
t1.setText("");
break;
case '=':
n2 = Double.parseDouble(t1.getText());
switch(xx)
{
case '+':
res = n1+n2;
break;
case '-':
res = n1-n2;
break;
case '*':
res = n1*n2;
break;
case '/':
res = n1/n2;
break;
}
t1.setText(res+"");
c = true;
break;
default:
if(c)
{
c = false;
t1.setText("");
}
t1.setText(t1.getText()+e.getActionCommand());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询