天天看点

java做个计算器小程序_利用java的gui实现的一个计算器小程序

废话不多说 , 直接贴代码 , 有详细的注释 , 中间不懂的可以在底下留言评论 , 我也是刚学GUI没多久......

这个是效果图 :

java做个计算器小程序_利用java的gui实现的一个计算器小程序

package gui;

import java.awt.*; // 这个是java的gui编程里面一个很重要的包

import java.awt.event.*; // 用来处理事件所需要

import java.util.Stack; // 栈 , 我用来处理运算的

public class Calculator extends Frame implements ActionListener{

private static final long serialVersionUID = 1L;

int frame_width = 1000,frame_height = 400;

Panel panel_textfield,panel_number,panel_op,panel_other;

Button [] number_buttons;

Button [] op_buttons;

TextField textfield;

public Calculator() {

super("计算器");

init();

setLayout();

setBackground();

setBounds();

setFonts();

addButtons();

textfield.setEditable(false);

addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

);

setVisible(true);

}

public void init() {

panel_textfield = new Panel();

panel_number = new Panel();

panel_op = new Panel();

panel_other = new Panel();

textfield = new TextField(frame_width);

setResizable(false);

add(panel_textfield);

add(panel_other);

panel_textfield.add(textfield);

panel_other.add(panel_number);

panel_other.add(panel_op);

}

public void setLayout() {

setLayout(new GridLayout(2,1,4,4));

panel_textfield.setLayout(null);

panel_other.setLayout(new GridLayout(1,2,4,4));

panel_number.setLayout(new GridLayout(5,3,4,4));

panel_op.setLayout(new GridLayout(3,1,4,4));

}

public void setBackground() {

panel_textfield.setBackground(Color.red);

panel_number.setBackground(Color.green);

panel_op.setBackground(Color.blue);

}

public void setBounds() {

setBounds(0, 0, frame_width, frame_height);

textfield.setBounds(0, 0, frame_width, frame_height / 2);

}

public void addButtons() {

String [] titles1 = {"/", "*", "-",

"7", "8", "9",

"4", "5", "6",

"1", "2", "3",

"0", ".", "c"};

String [] titles2 = {"x", "+", "="};

number_buttons = new Button[15];

op_buttons = new Button[3];

for(int i = 0; i < this.number_buttons.length; i++) {

number_buttons[i] = new Button(titles1[i]);

panel_number.add(number_buttons[i]);

number_buttons[i].addActionListener(this);

}

for(int i = 0; i < this.op_buttons.length; i++) {

op_buttons[i] = new Button(titles2[i]);

panel_op.add(this.op_buttons[i]);

op_buttons[i].addActionListener(this);

}

}

@Override

public void actionPerformed(ActionEvent e) {

Button button = (Button) e.getSource();

for(int i = 0; i < 14; i++) {

if(button == number_buttons[i] || button == op_buttons[1]) {

textfield.setText(textfield.getText() + button.getLabel());

return;

}

}

if(button == number_buttons[14]) {

textfield.setText("");

return;

}

if(button == op_buttons[0]) {

String s = textfield.getText();

if(s.length() > 0)

textfield.setText(s.substring(0, s.length() - 1));

return;

}

if(button == op_buttons[2]) {

textfield.setText(getResult());

return;

}

}

public String getResult() {

String s = textfield.getText();

String num = "";

Stacknums = new Stack();

Stackops = new Stack();

for(int i = 0; i < s.length(); i++) {

String temp = s.charAt(i) + "";

if(temp.matches("[0-9]") || temp.matches("[.]")) {

num += temp;

}

else if(temp.matches("[*+]") || temp.matches("[-]") | temp.matches("[/]")) {

if(!num.equals(""))

nums.push(Double.parseDouble(num));

if(ops.isEmpty() || cmpLevel(temp,ops.peek())) {

ops.push(temp);

}

else {

Double num1 = nums.pop();

Double num2 = nums.pop();

String op2 = ops.pop();

nums.push(compute(num2,num1,op2));

i--;

}

num = "";

}

}

while(!ops.isEmpty()) {

if(!num.equals("")) {

nums.push(compute(nums.pop(),Double.parseDouble(num),ops.pop()));

num = "";

}

else {

Double num1 = nums.pop();

Double num2 = nums.pop();

nums.push(compute(num2,num1,ops.pop()));

}

}

return nums.pop().toString();

}

public Double compute(double num1,double num2,String op) {

if(op.equals("+")) {

return num1 + num2;

}

else if(op.equals("-")) {

return num1 - num2;

}

else if(op.equals("*")) {

return num1 * num2;

}

else

return num1 / num2;

}

public boolean cmpLevel(String s1,String s2) {

if(s1.equals("+") || s1.equals("-")) {

return false;

}

else {

if(s2.equals("+") || s2.equals("-"))

return true;

return false;

}

}

public void setFonts() {

panel_number.setFont(new Font("微软雅黑",Font.PLAIN,24));

panel_op.setFont(new Font("微软雅黑",Font.PLAIN,24));

panel_other.setFont(new Font("微软雅黑",Font.PLAIN,24));

textfield.setFont(new Font("微软雅黑",Font.PLAIN,48));

}

public static void main(String [] args) {

new Calculator();

}

}