如何用java做如图计算器
展开全部
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() {
super("简易计算器");
// 初始化变量
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);
}
}
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() {
super("简易计算器");
// 初始化变量
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);
}
}
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CalculatorPanel extends JPanel
implements ActionListener
{
private JTextField display = new JTextField("0");
private double arg = 0;
private double last = 0;
private String op = "=";
private boolean start = true;
private JButton backButton = new JButton("BackSpace");
private JButton clearButton = new JButton("Clear");
public CalculatorPanel()
{
this.setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(2, 1));
JPanel p2 = new JPanel(new FlowLayout());
display.setEditable(false);
display.setBackground(Color.white);
display.setFont(new Font("Courier New", Font.BOLD, 18));
backButton.setForeground(Color.red);
backButton.addActionListener(this);
clearButton.setForeground(Color.red);
clearButton.addActionListener(this);
p2.add(backButton);
p2.add(clearButton);
p1.add(display);
p1.add(p2);
this.add(p1, "North");
JPanel p3 = new JPanel(new GridLayout(4, 5));
String buttonStr = "789/A456*B123-C0.D+=";
for (int i = 0; i < buttonStr.length(); i++)
this.addButton(p3, buttonStr.substring(i, i + 1));
this.add(p3, "Center");
}
private void addButton(Container c, String s)
{
JButton b = new JButton(s);
if (s.equals("A"))
b.setText("sqrt");
else if (s.equals("B"))
b.setText("1/x");
else if (s.equals("C"))
b.setText("%");
else if (s.equals("D"))
b.setText("+/-");
b.setForeground(Color.blue);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
if ('1' <= cmd.charAt(0) && cmd.charAt(0) <= '9' && !cmd.equals("1/x"))
{
if (start || (display.getText().charAt(0) == '0' && display.getText().charAt(1) != '.'))
display.setText(cmd);
else
display.setText(display.getText() + cmd);
start = false;
}
else if (cmd.equals("0"))
{
if (start || display.getText().charAt(0) == '0')
display.setText(cmd);
else
display.setText(display.getText() + cmd);
start = false;
}
else if (cmd.equals("."))
{
if (start)
display.setText(cmd);
else if (display.getText().indexOf('.') == -1)
display.setText(display.getText() + cmd);
start = false;
}
else if (cmd.equals("BackSpace"))
{
if (display.getText().length() > 1)
display.setText(display.getText().substring(0, display.getText().length() - 1));
else
{
display.setText("0");
start = true;
}
}
else if (cmd.equals("Clear"))
{
display.setText("0");
start = true;
}
else if (cmd.equals("+/-"))
{
if (display.getText().indexOf('-') == -1)
display.setText("-" + display.getText());
else
display.setText(display.getText().substring(1, display.getText().length()));
}
else if (cmd.equals("sqrt"))
{
arg = Double.parseDouble(display.getText());
if (arg >= 0)
display.setText(String.valueOf(Math.sqrt(arg)));
else
display.setText("Input Error, Parameters Must > 0!");
start = true;
}
else if (cmd.equals("1/x"))
{
arg = Double.parseDouble(display.getText());
display.setText(String.valueOf(1.0/arg));
start = true;
}
else
{
last = Double.parseDouble(display.getText());
start = true;
if (!cmd.equals("="))
{
op = cmd;
arg = Double.parseDouble(display.getText());
}
else
calculate();
start = true;
}
}
public void calculate()
{
if (op.equals("+"))
arg += last;
else if (op.equals("-"))
arg -= last;
else if (op.equals("*"))
arg *= last;
else if (op.equals("/"))
arg /= last;
else if (op.equals("%"))
arg %= last;
display.setText(String.valueOf(arg));
}
}
public class CalculatorApplet extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
import java.awt.event.*;
import javax.swing.*;
class CalculatorPanel extends JPanel
implements ActionListener
{
private JTextField display = new JTextField("0");
private double arg = 0;
private double last = 0;
private String op = "=";
private boolean start = true;
private JButton backButton = new JButton("BackSpace");
private JButton clearButton = new JButton("Clear");
public CalculatorPanel()
{
this.setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(2, 1));
JPanel p2 = new JPanel(new FlowLayout());
display.setEditable(false);
display.setBackground(Color.white);
display.setFont(new Font("Courier New", Font.BOLD, 18));
backButton.setForeground(Color.red);
backButton.addActionListener(this);
clearButton.setForeground(Color.red);
clearButton.addActionListener(this);
p2.add(backButton);
p2.add(clearButton);
p1.add(display);
p1.add(p2);
this.add(p1, "North");
JPanel p3 = new JPanel(new GridLayout(4, 5));
String buttonStr = "789/A456*B123-C0.D+=";
for (int i = 0; i < buttonStr.length(); i++)
this.addButton(p3, buttonStr.substring(i, i + 1));
this.add(p3, "Center");
}
private void addButton(Container c, String s)
{
JButton b = new JButton(s);
if (s.equals("A"))
b.setText("sqrt");
else if (s.equals("B"))
b.setText("1/x");
else if (s.equals("C"))
b.setText("%");
else if (s.equals("D"))
b.setText("+/-");
b.setForeground(Color.blue);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
String cmd = event.getActionCommand();
if ('1' <= cmd.charAt(0) && cmd.charAt(0) <= '9' && !cmd.equals("1/x"))
{
if (start || (display.getText().charAt(0) == '0' && display.getText().charAt(1) != '.'))
display.setText(cmd);
else
display.setText(display.getText() + cmd);
start = false;
}
else if (cmd.equals("0"))
{
if (start || display.getText().charAt(0) == '0')
display.setText(cmd);
else
display.setText(display.getText() + cmd);
start = false;
}
else if (cmd.equals("."))
{
if (start)
display.setText(cmd);
else if (display.getText().indexOf('.') == -1)
display.setText(display.getText() + cmd);
start = false;
}
else if (cmd.equals("BackSpace"))
{
if (display.getText().length() > 1)
display.setText(display.getText().substring(0, display.getText().length() - 1));
else
{
display.setText("0");
start = true;
}
}
else if (cmd.equals("Clear"))
{
display.setText("0");
start = true;
}
else if (cmd.equals("+/-"))
{
if (display.getText().indexOf('-') == -1)
display.setText("-" + display.getText());
else
display.setText(display.getText().substring(1, display.getText().length()));
}
else if (cmd.equals("sqrt"))
{
arg = Double.parseDouble(display.getText());
if (arg >= 0)
display.setText(String.valueOf(Math.sqrt(arg)));
else
display.setText("Input Error, Parameters Must > 0!");
start = true;
}
else if (cmd.equals("1/x"))
{
arg = Double.parseDouble(display.getText());
display.setText(String.valueOf(1.0/arg));
start = true;
}
else
{
last = Double.parseDouble(display.getText());
start = true;
if (!cmd.equals("="))
{
op = cmd;
arg = Double.parseDouble(display.getText());
}
else
calculate();
start = true;
}
}
public void calculate()
{
if (op.equals("+"))
arg += last;
else if (op.equals("-"))
arg -= last;
else if (op.equals("*"))
arg *= last;
else if (op.equals("/"))
arg /= last;
else if (op.equals("%"))
arg %= last;
display.setText(String.valueOf(arg));
}
}
public class CalculatorApplet extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
图在哪呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询