用JAVA编一个简单的计算器
发布网友
发布时间:2023-07-17 01:23
我来回答
共4个回答
热心网友
时间:2024-12-03 06:22
实现如下次:
import java.awt.*;
import java.awt.event.*;
public class JSQ implements ActionListener,WindowListener
{
Button[]b=new Button[20];
TextField tf=new TextField(35);
int fh;
int cn1=0,cn2=0,cn3=0,cn4=0;
double js1,js2,js3;
public JSQ()
{
Frame f=new Frame("计算器");
f.setLayout(new BorderLayout());
f.addWindowListener(this);
Panel p1=new Panel();
Panel p2=new Panel(new GridLayout(4,5,5,5));
Panel p3=new Panel();
Panel p4=new Panel();
Panel p5=new Panel();
f.setBackground(new Color(220,220,220));
tf.setBackground(new Color(250,250,250));
tf.setEditable(false);
p1.add(tf);
f.add(p1,BorderLayout.NORTH);
f.add(p3,BorderLayout.WEST);
f.add(p4,BorderLayout.SOUTH);
f.add(p5,BorderLayout.EAST);
for(int i=0;i<10;++i)
{
b[i]=new Button(String.valueOf(i));
}
b[10]=new Button("+");
b[11]=new Button("-");
b[12]=new Button("*");
b[13]=new Button("/");
b[14]=new Button("+/-");
b[15]=new Button("=");
b[16]=new Button("C");
b[17]=new Button("sqrt");
b[18]=new Button("1/x");
b[19]=new Button(".");
p2.add(b[7]);
p2.add(b[8]);
p2.add(b[9]);
p2.add(b[13]);
p2.add(b[16]);
p2.add(b[4]);
p2.add(b[5]);
p2.add(b[6]);
p2.add(b[12]);
p2.add(b[17]);
p2.add(b[1]);
p2.add(b[2]);
p2.add(b[3]);
p2.add(b[11]);
p2.add(b[18]);
p2.add(b[0]);
p2.add(b[14]);
p2.add(b[19]);
p2.add(b[10]);
p2.add(b[15]);
for(int i=0;i<20;++i)
{
b[i].addActionListener(this);
}
f.add(p2);
f.setSize(300,300);
f.setVisible(true);
f.setMaximumSize(new Dimension(300,300));//没有作用
}
public void actionPerformed(ActionEvent e)
{
for(int i=1;i<10;i++)//如果按下1--9键处理代码
{
if(e.getSource()==b[i])
{
if((cn1!=0||cn2!=0||cn3!=0||cn4!=0)&&tf.getText()!=null)
{
tf.setText(null);
cn1=0;cn2=0;cn3=0;cn4=0;
}
if(tf.getText().equals("0"))
{
tf.setText(null);
String str=tf.getText()+e.getActionCommand();
tf.setText(str);
}
else
{
String str=tf.getText()+e.getActionCommand();
tf.setText(str);
}
}
}
if(e.getSource()==b[0])//如果按下0键处理代码
{
if(tf.getText().equals("0"))
{}
else
{
String str=tf.getText()+e.getActionCommand();
tf.setText(str);
}
}
if(e.getSource()==b[16])//如按下C则清零
{
tf.setText(null);
js1=0;
js2=0;
js3=0;
cn1=0;cn2=0;cn3=0;cn4=0;
}
if(e.getSource()==b[14])//如果按下+/-键处理代码
{
double a=Double.parseDouble(tf.getText());
if(a!=0)
tf.setText(String.valueOf(a*(-1)));
}
if(e.getSource()==b[18])//如果按下1/x键处理代码
{
double b=Double.parseDouble(tf.getText());
tf.setText(String.valueOf(1/b));
}
if(e.getSource()==b[17])//如果按下sqrt键处理代码
{
double c=Double.parseDouble(tf.getText());
if(c<0)
tf.setText("error!");
else
tf.setText(String.valueOf(Math.sqrt(c)));
}
if(e.getSource()==b[19])//如果按下 . 键处理代码
{
String str=tf.getText();
boolean bnn=(str.length()==0);
if(bnn)
{
tf.setText("0");
}
boolean bn=(str.indexOf('.')!=-1);//如果没有.则加.
if(bn)
{}
else
{
str=tf.getText()+e.getActionCommand();
tf.setText(str);
}
}
if(e.getSource()==b[10])// 如果按下+键处理代码
{
fh=10;
cn1++;
js1=Double.parseDouble(tf.getText());
}
if(e.getSource()==b[11])// 如果按下-键处理代码
{
fh=11;
cn2++;
js1=Double.parseDouble(tf.getText());
}
if(e.getSource()==b[12])//如果按下*键处理代码
{
fh=12;
cn3++;
js1=Double.parseDouble(tf.getText());
}
if(e.getSource()==b[13])// 如果按下/键处理代码
{
fh=13;
cn4++;
js1=Double.parseDouble(tf.getText());
}
if(e.getSource()==b[15])// 如果按下=键处理代码
{
switch(fh)
{
case 10:
{
js2=Double.parseDouble(tf.getText());
js3=js2+js1;
tf.setText(String.valueOf(js3));
}break;
case 11:
{
js2=Double.parseDouble(tf.getText());
js3=js1-js2;
tf.setText(String.valueOf(js3));
}break;
case 12:
{
js2=Double.parseDouble(tf.getText());
js3=js2*js1;
tf.setText(String.valueOf(js3));
}break;
case 13:
{
js2=Double.parseDouble(tf.getText());
js3=js1/js2;
tf.setText(String.valueOf(js3));
}break;
}
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e)
{}
public void windowClosed(WindowEvent e)
{}
public void windowActivated(WindowEvent e)
{}
public void windowDeactivated(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public static void main(String[]args)
{
new JSQ();
}
}
如果你觉得我的回答对你有帮助,请给我多加分,如果你还有什么疑问的话,请给我留言,百度,qq,甚至手机都可以,帮助别人就是帮助自己,大家一块进步嘛!
热心网友
时间:2024-12-03 06:23
敲了半天;给你;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
private boolean dotExist, operated, equaled; // 帮助运算的布尔变量
private double storedNumber; // 目前的结果
private char lastOperator; // 表示上一运算符
private JTextField operation; // 结果栏
private JButton dot, plus, minus, multi, div, sqrt, equal, changePN, clear; // 运算符
private JButton[] numbers; // 数字
// 构造者
public Calculator()
{
setTitle("Calculator");
// 初始化变量
dotExist = false; // 表示当前的数是否有小数点
operated = false; // 表示任意运算符是否被按下
equaled = false; // 表示等号是否被按下
storedNumber = 0;
lastOperator = '?';
// 初始化窗口变量
operation = new JTextField("0");
operation.setEditable(false);
numbers = new JButton[10];
for (int i = 0; i < 10; i++)
numbers[i] = new JButton("" + i);
dot = new JButton(".");
plus = new JButton("+");
minus = new JButton("-");
multi = new JButton("*");
div = new JButton("/");
sqrt = new JButton("√");
equal = new JButton("=");
changePN = new JButton("±");
clear = new JButton("AC");
// 将窗口物体放入窗口
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(layout, operation, 0, 0, 4, 1);
addComponent(layout, numbers[1], 1, 0, 1, 1);
addComponent(layout, numbers[2], 1, 1, 1, 1);
addComponent(layout, numbers[3], 1, 2, 1, 1);
addComponent(layout, numbers[4], 2, 0, 1, 1);
addComponent(layout, numbers[5], 2, 1, 1, 1);
addComponent(layout, numbers[6], 2, 2, 1, 1);
addComponent(layout, numbers[7], 3, 0, 1, 1);
addComponent(layout, numbers[8], 3, 1, 1, 1);
addComponent(layout, numbers[9], 3, 2, 1, 1);
addComponent(layout, dot, 4, 0, 1, 1);
addComponent(layout, numbers[0], 4, 1, 1, 1);
addComponent(layout, sqrt, 4, 2, 1, 1);
addComponent(layout, plus, 1, 3, 1, 1);
addComponent(layout, minus, 2, 3, 1, 1);
addComponent(layout, multi, 3, 3, 1, 1);
addComponent(layout, div, 4, 3, 1, 1);
addComponent(layout, equal, 5, 0, 2, 1);
addComponent(layout, changePN, 5, 2, 1, 1);
addComponent(layout, clear, 5, 3, 1, 1);
}
// 对按钮进行反应的方法
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
if (btn == clear)
{
operation.setText("0");
dotExist = false;
storedNumber = 0;
lastOperator = '?';
}
else if (btn == equal)
{
operate('=');
equaled = true;
}
else if (btn == plus)
{
operate('+');
equaled = false;
}
else if (btn == minus)
{
operate('-');
equaled = false;
}
else if (btn == multi)
{
operate('*');
equaled = false;
}
else if (btn == div)
{
operate('/');
equaled = false;
}
else if (btn == changePN)
{
operate('p');
operate('=');
equaled = true;
}
else if (btn == sqrt)
{
operate('s');
operate('=');
equaled = true;
}
else
{
if (equaled)
storedNumber = 0;
for (int i = 0; i < 10; i++)
if (btn == numbers[i])
{
if (operation.getText().equals("0"))
operation.setText("" + i);
else if(! operated)
operation.setText(operation.getText() + i);
else
{
operation.setText("" + i);
operated = false;
}
}
if (btn == dot && ! dotExist)
{
operation.setText(operation.getText() + ".");
dotExist = true;
}
}
}
// 进行运算的方法
private void operate(char operator)
{
double currentNumber = Double.valueOf(operation.getText()).doubleValue();
if (lastOperator == '?')
storedNumber = currentNumber;
else if (lastOperator == '+')
storedNumber += currentNumber;
else if (lastOperator == '-')
storedNumber -= currentNumber;
else if (lastOperator == '*')
storedNumber *= currentNumber;
else if (lastOperator == '/')
storedNumber /= currentNumber;
else if (lastOperator == 'p')
storedNumber *= -1;
else if (lastOperator == 's')
storedNumber = Math.sqrt(currentNumber);
else if (lastOperator == '=' && equaled)
storedNumber = currentNumber;
operation.setText("" + storedNumber);
operated = true;
lastOperator = operator;
}
// 快捷使用GridBagLayout的方法
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(10, 2, 10, 2);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = col;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;
layout.setConstraints(component, constraints);
if (component instanceof JButton)
((JButton)component).addActionListener(this);
getContentPane().add(component);
}
// 主方法初始化并显示窗口
public static void main(String[] args)
{
Calculator calc = new Calculator();
calc.setSize(290, 400);
calc.setVisible(true);
}
}
直接就可以运行。。。。完全代码做的。。。。
热心网友
时间:2024-12-03 06:23
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class A extends Applet implements ActionListener
{
Panel p=new Panel();
TextField text1=new TextField(5);
TextField text2=new TextField(5);
TextField text3=new TextField(5);
Button b1=new Button("加");
Button b2=new Button("减");
Button b3=new Button("乘");
Button b4=new Button("除");
public void init()
{
p.setLayout(new FlowLayout());
p.add(text1);
p.add(text2);
p.add(text3);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
add(p);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s1=null;
String s2=null;
if(e.getSource()==b1)
{
s1=text1.getText();
s2=text2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
text3.setText(a+b+"");
}
else if(e.getSource()==b2)
{
s1=text1.getText();
s2=text2.getText();
int c=Integer.parseInt(s1);
int d=Integer.parseInt(s2);
text3.setText(c-d+"");
}
else if(e.getSource()==b3)
{
s1=text1.getText();
s2=text2.getText();
int f=Integer.parseInt(s1);
int g=Integer.parseInt(s2);
text3.setText(f*g+"");
}
else if(e.getSource()==b4)
{
s1=text1.getText();
s2=text2.getText();
int j=Integer.parseInt(s1);
int k=Integer.parseInt(s2);
text3.setText(j/k+"");
}
}
public static void main(String[] args)
{
new A();
}
}
applet小程序。
热心网友
时间:2024-12-03 06:24
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Counter extends JApplet{
private Label l=new Label("输入数字");
private Label l1=new Label("输入数字");
private Label l2=new Label("总数");
private TextField tf=new TextField(20);
private TextField tf1=new TextField(20);
private TextField tf2=new TextField(20);
private Button b=new Button(" + ");
private Button b1=new Button(" - ");
private Button b2=new Button(" * ");
private Button b3=new Button(" / ");
public void init(){
Container c= getContentPane();
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
int result=Integer.parseInt(tf.getText());
int count=Integer.parseInt(tf1.getText());
tf2.setText(String.valueOf(result+count));
}
});
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
int result=Integer.parseInt(tf.getText());
int count=Integer.parseInt(tf1.getText());
tf2.setText(String.valueOf(result-count));
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
int result=Integer.parseInt(tf.getText());
int count=Integer.parseInt(tf1.getText());
tf2.setText(String.valueOf(result*count));
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
int result=Integer.parseInt(tf.getText());
int count=Integer.parseInt(tf1.getText());
tf2.setText(String.valueOf(result/count));
}
});
c.add(l);
c.add(tf);
c.add(l1);
c.add(tf1);
c.add(l2);
c.add(tf2);
c.add(b);
c.add(b1);
c.add(b2);
c.add(b3);
c.setLayout(new FlowLayout());
}
}
看看我这个可以不