用java编写一个简单计算器
发布网友
发布时间:2022-04-29 19:25
我来回答
共5个回答
热心网友
时间:2022-04-24 05:58
/*
* @(#)JCalculator.java 1.00 06/17/2015
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A simple calculator program.
* <p>I saw this program in a QQ group, and help a friend correct it.</p>
*
* @author Singyuen Yip
* @version 1.00 12/29/2009
* @see JFrame
* @see ActionListener
*/
public class JCalculator extends JFrame implements ActionListener {
/**
* Serial Version UID
*/
private static final long serialVersionUID = -169068472193786457L;
/**
* This class help close the Window.
* @author Singyuen Yip
*
*/
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
int i;
// Strings for Digit & Operator buttons.
private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*","1",
"2", "3", "-", ".", "0", "=", "+" };
// Build buttons.
JButton[] buttons = new JButton[str.length];
// For cancel or reset.
JButton reset = new JButton("CE");
// Build the text field to show the result.
JTextField display = new JTextField("0");
/**
* Constructor without parameters.
*/
public JCalculator() {
super("Calculator");
// Add a panel.
JPanel panel1 = new JPanel(new GridLayout(4, 4));
// panel1.setLayout(new GridLayout(4,4));
for (i = 0; i < str.length; i++) {
buttons[i] = new JButton(str[i]);
panel1.add(buttons[i]);
}
JPanel panel2 = new JPanel(new BorderLayout());
// panel2.setLayout(new BorderLayout());
panel2.add("Center", display);
panel2.add("East", reset);
// JPanel panel3 = new Panel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", panel2);
getContentPane().add("Center", panel1);
// Add action listener for each digit & operator button.
for (i = 0; i < str.length; i++)
buttons[i].addActionListener(this);
// Add listener for "reset" button.
reset.addActionListener(this);
// Add listener for "display" button.
display.addActionListener(this);
// The "close" button "X".
addWindowListener(new WindowCloser());
// Initialize the window size.
setSize(800, 800);
// Show the window.
// show(); Using show() while JDK version is below 1.5.
setVisible(true);
// Fit the certain size.
pack();
}
public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
String label = e.getActionCommand();
if (target == reset)
handleReset();
else if ("0123456789.".indexOf(label) > 0)
handleNumber(label);
else
handleOperator(label);
}
// Is the first digit pressed?
boolean isFirstDigit = true;
/**
* Number handling.
* @param key the key of the button.
*/
public void handleNumber(String key) {
if (isFirstDigit)
display.setText(key);
else if ((key.equals(".")) && (display.getText().indexOf(".") < 0))
display.setText(display.getText() + ".");
else if (!key.equals("."))
display.setText(display.getText() + key);
isFirstDigit = false;
}
/**
* Reset the calculator.
*/
public void handleReset() {
display.setText("0");
isFirstDigit = true;
operator = "=";
}
double number = 0.0;
String operator = "=";
/**
* Handling the operation.
* @param key pressed operator's key.
*/
public void handleOperator(String key) {
if (operator.equals("+"))
number += Double.valueOf(display.getText());
else if (operator.equals("-"))
number -= Double.valueOf(display.getText());
else if (operator.equals("*"))
number *= Double.valueOf(display.getText());
else if (operator.equals("/"))
number /= Double.valueOf(display.getText());
else if (operator.equals("="))
number = Double.valueOf(display.getText());
display.setText(String.valueOf(number));
operator = key;
isFirstDigit = true;
}
public static void main(String[] args) {
new JCalculator();
}
}
运行界面:
热心网友
时间:2022-04-24 07:16
package swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Calculator extends JFrame{
JTextField jt1 ,jt2 ,jt3;
JLabel jLabel;
JButton equButton,addButton,receButton,mulButton,divButton;
public Calculator() {
super("简易计算器");
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(2, 0));
JPanel uJPanel = new JPanel();
Dimension preferredSize = new Dimension(50, 29);
GridBagConstraints gbc = new GridBagConstraints();
uJPanel.setLayout(new GridBagLayout());
gbc.gridx = GridBagConstraints.RELATIVE;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(1, 2, 1, 2);
jt1 = new JTextField(4);
jLabel = new JLabel();
jLabel.setPreferredSize(new Dimension(10, 29));
jt2 = new JTextField(4);
equButton = new JButton("=");
ActionListener myActionListener = new MyActionListener();
equButton.addActionListener(myActionListener);
jt3 = new JTextField(8);
jt1.setPreferredSize(preferredSize);
jt2.setPreferredSize(preferredSize);
jt3.setPreferredSize(preferredSize);
uJPanel.add(jt1,gbc);
uJPanel.add(jLabel,gbc);
uJPanel.add(jt2,gbc);
uJPanel.add(equButton,gbc);
gbc.weightx = 1;
uJPanel.add(jt3,gbc);
contentPanel.add(uJPanel);
JPanel dJPanel = new JPanel();
dJPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 8, 2));
addButton = new JButton("+");
receButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
addButton.addActionListener(myActionListener);
receButton.addActionListener(myActionListener);
mulButton.addActionListener(myActionListener);
divButton.addActionListener(myActionListener);
dJPanel.add(addButton);
dJPanel.add(receButton);
dJPanel.add(mulButton);
dJPanel.add(divButton);
contentPanel.add(dJPanel);
this.setContentPane(contentPanel);
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
class MyActionListener implements ActionListener{
Double d1,d2,d3;
String operator = "";
public void actionPerformed(ActionEvent e) {
String fun = e.getActionCommand();
if (!fun.equals("=")) {
jLabel.setText(fun);
operator = fun;
}else {
d1 = jt1.getText().equals("")?null:Double.valueOf(jt1.getText());
d2 = jt2.getText().equals("")?null:Double.valueOf(jt2.getText());
d3 = calculate(d1,d2,operator);
jt3.setText(d3==null ? "":d3.toString());
jt3.setCaretPosition(0);
}
}
}
Double calculate(Double d1,Double d2,String operator){
if (d1 == null || d2 == null) {
return null;
}
Double d3 = null;
switch (operator) {
case "+":
d3 = d1 + d2;
break;
case "-":
d3 = d1 - d2;
break;
case "*":
d3 = d1 * d2;
break;
case "/":
d3 = d1 / d2;
break;
}
return d3;
}
public static void main(String[] args) {
new Calculator();
}
}
热心网友
时间:2022-04-24 08:51
我已经写好了。需要的话,撩我
热心网友
时间:2022-04-24 10:42
不错,都有现成的了
~~
热心网友
时间:2022-04-24 12:50
你这是样本吗?不是已经写好了?
用JAVA编写的科学计算器源代码
这个科学计算器支持基本的四则运算(加、减、乘、除)以及一些科学运算(正弦、余弦、正切、对数、指数和平方根)。用户可以通过输入相应的操作和两个数字来执行计算。例如,输入“add 2 3”将计算2加3的结果。代码首先导入了`Scanner`类,用于从用户处获取输入。然后,在`main`方法中,创建...
java怎样写一个简单的计算器
import java.util.Scanner;public class Calculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("Enter first number:");double num1 = scanner.nextDouble();System.out.println("Enter second number:");double num2 = scanner.nex...
求JAVA编写的 简易计算器程序,附上注释
import java.awt.*;import java.awt.event.*;public class Calculator implements ActionListener { String s="",s1;double d1,d2;JFrame jf = new JFrame("小计算器by Graduate") ;JTextField tf = new JTextField();public void init()//实现计算器界面 { Container c=jf.getContentPane();...
用JAVA编写一个计算器
setLayout(new BorderLayout()); // 设定布局管理器边框布局add(text, BorderLayout.CENTER); // text放置在窗体的中间}}直接复制 保存成Jisuanqi .java可以直接运行了 已赞过 已踩过< 你对这个回答的评价是? 评论 收起 其他类似问题2012-07-02 用JAVA编写一个计算器 2005-12-25 如何用java编程做一个计...
采用java语言编写一个计算器,该计算器能对正数、负数(这里的正数、负数...
mh1.add(new MenuItem("帮助主题")); mh1.add(new MenuItem("关于计算器")); mh1.addActionListener(this); } int flag=0;//运算符有效标识 int num=0;//用于实现连续运算 double dot=0;//用于标识小数点 double first=0,second=0,result=0;//用于保存第一个数,第二个数和计算结果 double pus...
1 简单计算器。请编写一个程序计算表达式:datal op data2的值。其中op...
1,简单计算器代码如下:import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Jsq extends JFrame implements ActionListener{ JTextField txt = new JTextField(15);JPanel p = new JPanel();JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;JButton bAdd,bDif,bMul,b...
java编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 , A-F个字 ...
import java.awt.*;import javax.swing.*;public class MyCalculator extends JFrame{public MyCalculator() {String ss1 = "0123456789ABCDEF";String[] ss2 = {"+","-","*","/","√ ̄","aⁿ","CE","="};//有些字符不在GBK中,所以文本使用了UTF-8编码。如果要用GBK编码,...
java中写了一个简单计算器,计算0.3*3=0.8999...,有人说要用BigDecimal...
double d = 0.3;BigDecimal bd = new BigDecimal(d+"");BigDecimal bd2 = new BigDecimal(3);System.out.println(bd.multiply(bd2));--- 需要注意的是, BigDecimal bd = new BigDecimal(d+"");构造函数要传String类型,如果传double类型的话,结果也是0.8999......
用Java写的计算器的程序!不需要界面!
用java写的计算器的程序,主要是通过控制台输入,主要方法是使用scanner类来接收用户从键盘输入的一个算式,通过分解算式,存入两个字符串,判断中间的的符号,进行相应计算,如下代码:System.out.println("---"); System.out.println("请输入一个算术表达式,如:45*23"); Scanner in = new ...
用java编得计算器程序软件和源代码
java计算器import java.awt.*; import javax.swing.*; import java.awt.event.*; class calculation extends JFrame {public calculation() /*构造方法*/ {super("计数器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initTextPanel(); /*文本框*/ initControlPanel(); /*控制键*/ ...