您的位置:首页 > 编程语言 > Java开发

JAVA作业 简单的计算器

2017-11-18 16:18 323 查看
public class Main_Class {
public static void main(String args[]) {
ComputerFrame frame=new ComputerFrame("MyComputer");
frame.setBounds(100,100,700,800);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComputerFrame extends JFrame {
JMenuBar menubar = new JMenuBar();
JMenu menu_help = new JMenu("帮助");
JTextField display = new JTextField(20);
JLabel m_status = new JLabel();
JPanel console = new JPanel(new BorderLayout());
JPanel north = new JPanel();
JPanel west = new JPanel(new GridLayout(5, 1));
JPanel center = new JPanel(new GridLayout(4, 3));
JPanel east = new JPanel(new GridLayout(4, 2));
private double result = 0;
private char op = '\0';
boolean append = false;
String memory = null;

ComputerFrame(String title) {
init();
this.setTitle(title);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

void init() {
menubar.add(menu_help);
this.setJMenuBar(menubar);
display.setText("0");
display.setFont(new Font("宋体", Font.BOLD, 32));
display.setHorizontalAlignment(JTextField.RIGHT);
this.add(display, BorderLayout.NORTH);
this.add(console);
init_north();
init_west();
init_center();
init_east();
this.add(console);
}

void init_north() {
JButton Backspace = new JButton("Backspace");
Backspace.setFont(new Font("宋体", Font.BOLD, 22));
JButton CE = new JButton("CE");
CE.setFont(new Font("宋体", Font.BOLD, 22));
JButton C = new JButton("C");
C.setFont(new Font("宋体", Font.BOLD, 22));
Backspace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
text = text.substring(0, text.length() - 1);
display.setText(text);
}
});
CE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText("0");
append=false;
}
});
C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText("0");
result = 0;
op = '\0';
append=false;
}
});
north.add(Backspace);
north.add(CE);
north.add(C);
console.add(north, BorderLayout.NORTH);
}

void init_west() {
JButton MC = new JButton("MC");
MC.setFont(new Font("宋体", Font.BOLD, 22));
JButton MR = new JButton("MR");
MR.setFont(new Font("宋体", Font.BOLD, 22));
JButton MS = new JButton("MS");
MS.setFont(new Font("宋体", Font.BOLD, 22));
JButton MPlus = new JButton("M+");
MPlus.setFont(new Font("宋体", Font.BOLD, 22));
MC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
memory = null;
m_status.setText(null);
}
});
MR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (memory != null) {
display.setText(memory);
append=false;
}
}
});
MS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!display.getText().equals("0")) {
memory = display.getText();
}
m_status.setText("M");
}
});
MPlus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double n = Double.valueOf(display.getText());
double m = Double.valueOf(memory);
memory = Double.toString(m + n);
m_status.setText("M");
append=false;
}
});
m_status.setFont(new Font("宋体", Font.BOLD, 40));
west.add(m_status);
west.add(MC);
west.add(MR);
west.add(MS);
west.add(MPlus);
console.add(west, BorderLayout.WEST);
}

void init_center() {
JButton number[] = new JButton[12];
for (int i = 0; i < 12; ++i) {
number[i] = new JButton();
number[i].setFont(new Font("宋体", Font.BOLD, 40));
}
for (int i = 2; i >= -1; --i)
for (int j = 1; j <= 3; ++j) {
int num;
if (i == -1) {// 用于增加按钮"0"
num = 0;
j = 3;
} else
num = i * 3 + j;
number[num].setText(Integer.toString(num));
number[num].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text;
if (append == true)
text = display.getText();
else
text = "";
text += e.getActionCommand();
display.setText(text);
append = true;
}
});
center.add(number[num]);
}
number[10].setText("+/-");
number[10].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
if (text.charAt(0) == '-')
text = "+".concat(text.substring(1, text.length()));
else if (text.charAt(0) == '+')
text = "-".concat(text.substring(1, text.length()));
else
text = "-".concat(text);
display.setText(text);
}
});
number[11].setText(".");
number[11].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
text += ".";
System.out.println(text);
display.setText(text);
}
});
center.add(number[10]);
center.add(number[11]);
console.add(center, BorderLayout.CENTER);
}

void init_east() {
JButton operator[] = new JButton[8];
operator[0] = new JButton("/");
operator[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
if (op != '\0') {
result = operate(result, cur_num, op);
display.setText(Double.toString(result));
} else
result = cur_num;
op = '/';
append=false;
}
});
operator[1] = new JButton("sqrt");
operator[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
cur_num = Math.sqrt(cur_num);
display.setText(Double.toString(cur_num));
append=false;
}
});
operator[2] = new JB
4000
utton("*");
operator[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
if (op != '\0') {
result = operate(result, cur_num, op);
display.setText(Double.toString(result));
} else
result = cur_num;
op = '*';
append=false;
}
});
operator[3] = new JButton("%");
operator[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
cur_num /= 100;
display.setText(Double.toString(cur_num));
append=false;
}
});
operator[4] = new JButton("-");
operator[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
if (op != '\0') {
result = operate(result, cur_num, op);
display.setText(Double.toString(result));
} else
result = cur_num;
op = '-';
append=false;
}
});
operator[5] = new JButton("1/x");
operator[5].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
if (cur_num == 0)
display.setText("WRONG!");
else {
cur_num = operate(1, cur_num, '/');
display.setText(Double.toString(cur_num));
}
append=false;
}
});
operator[6] = new JButton("+");
operator[6].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
if (op != '\0') {
result = operate(result, cur_num, op);
display.setText(Double.toString(result));
} else
result = cur_num;
op = '+';
append=false;
}
});
operator[7] = new JButton("=");
operator[7].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = display.getText();
double cur_num = Double.valueOf(text);
if (op != '\0') {
result = operate(result, cur_num, op);
display.setText(Double.toString(result));
}
op = '\0';
append=false;
}
});
for (int i = 0; i < 8; ++i) {
operator[i].setFont(new Font("宋体", Font.BOLD, 30));
east.add(operator[i]);
}
console.add(east, BorderLayout.EAST);
}

double operate(double a, double b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
default:
return a / b;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: