用java编写一个简单计算器
就按照我给的题目,写出图片一样的程序 展开
2018-01-18 · 知道合伙人互联网行家
/*
* @(#)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();
}
}
运行界面:
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,reduceButton,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("+");
reduceButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
addButton.addActionListener(myActionListener);
reduceButton.addActionListener(myActionListener);
mulButton.addActionListener(myActionListener);
divButton.addActionListener(myActionListener);
dJPanel.add(addButton);
dJPanel.add(reduceButton);
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();
}
}
咋给我
看我的名字。
2018-01-17
~~
哪有?那些都是很复杂的,我并不需要那么复杂的
你什么要求