本文主要是介绍Java.awt 计算器小程序源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package myPacket;import java.awt.*;
import java.awt.event.*;public class Calculator
{private Frame f = new Frame("计算器"); // 主窗口 显示计算器主界面private Frame info = new Frame("关于"); // 介绍窗口private Button[] b = new Button[10]; // 声明数字按钮private Button[] cal = new Button[8]; // 声明功能按钮private MenuBar mb = new MenuBar(); // 菜单工具栏private Menu option = new Menu("操作"); // 操作菜单按钮private Menu other = new Menu("Other"); // 其他菜单按钮private MenuItem about = new MenuItem("about");private MenuItem reset = new MenuItem("reset");private MenuItem exit = new MenuItem("Exit");private Label information = new Label();private TextField text = new TextField(30); // 输入文本框private int flag = 0; // 重置标记 private int flag2 = -1; // 对文本框中是否有内容进行标记 有则设置为 -1 数字键标记private int Key = -1; // + - * / 运算标记String load1 = new String();String load2 = new String();public void init() // 初始化{Panel p = new Panel();NumListener numListen = new NumListener(); // 数字键CalListener calListen = new CalListener(); // 功能键load1 = load2 = null;for(int i = 0; i <= 9; i++)b[i] = new Button(""+i); // 初始化数字键按钮info.setSize(200,400); // 初始化信息窗口大小information.setText("Java Calculator"); // 设置信息内容for(int i = 0; i <= 9; i++)b[i].addActionListener(numListen); // 给按钮添加数字键的事件监听器cal[0] = new Button("+");cal[1] = new Button("-");cal[2] = new Button("*");cal[3] = new Button("/");cal[4] = new Button("=");cal[5] = new Button("退格"); // 退格键cal[6] = new Button(".");cal[7] = new Button("AC");cal[0].addActionListener(calListen);cal[1].addActionListener(calListen);cal[2].addActionListener(calListen);cal[3].addActionListener(calListen);cal[4].addActionListener(calListen);cal[5].addActionListener(e->{if (text.getText().length() == 0) {System.out.println("无字符");}else {text.setText(text.getText().substring(0,text.getText().length()-1)); }});cal[6].addActionListener(e->{text.setText(text.getText()+"."); // 添加小数点});// 在面板上依次添加数据按钮和功能按钮p.add(b[1]);p.add(b[2]);p.add(b[3]);p.add(cal[0]);p.add(b[4]);p.add(b[5]);p.add(b[6]);p.add(cal[1]);p.add(b[7]);p.add(b[8]);p.add(b[9]);p.add(cal[2]);p.add(cal[5]);p.add(b[0]);p.add(cal[6]);p.add(cal[3]);p.add(cal[7]);p.add(cal[4]);text.setEditable(false); // 禁止对文本框进行编辑exit.addActionListener(e->System.exit(0)); // 给菜单按钮添加事件监听 退出reset.addActionListener(e->{load1 = load2 = null;Key = -1;flag2 = -1;flag = 0;text.setText(""); // 重置时清空输入窗格});cal[7].addActionListener(e-> // AC 键的功能为重置{load1 = load2 = null;Key = -1;flag2 = -1;flag = 0;text.setText(""); });about.addActionListener(e->{info.setVisible(true); // 弹出信息窗格});option.add(reset);option.add(exit);other.add(about);mb.add(option);mb.add(other); f.setMenuBar(mb); // 设置工具条f.add(text,BorderLayout.NORTH); // 设置文本框的布局p.setLayout(new GridLayout(5,4,4,4)); // 设置布局 5 行 4 列 ;水平 4 个 垂直 4 个info.add(information);info.addWindowListener(new WindowListener()); // 设置对窗口事件的监听器f.addWindowListener(new WindowListener());f.add(p); // 将面板添加到窗口f.setSize(640,480); // 设置计算器窗口大小f.setVisible(true);}// 窗口事件监听器class WindowListener extends WindowAdapter{public void windowClosing(WindowEvent e) // 对关闭窗口事件进行响应{if(e.getSource() == f)System.exit(0);else if(e.getSource() == info)info.setVisible(false);}}// 数字键的事件监听器class NumListener implements ActionListener {public void actionPerformed(ActionEvent e) {if(e.getSource() == b[0]){if(flag2 == -1) // 文本框中已有内容时text.setText(text.getText()+"0");else{text.setText("0"); // 数字 0flag2 = -1;}}else if(e.getSource() == b[1]){if(flag2 == -1)text.setText(text.getText()+"1");else{text.setText("1"); // 数字 1flag2 = -1;}}else if(e.getSource() == b[2]){if(flag2 == -1)text.setText(text.getText()+"2");else{text.setText("2"); // 数字 2flag2 = -1;}}else if(e.getSource() == b[3]){if(flag2 == -1)text.setText(text.getText()+"3");else{text.setText("3"); // 数字 3 flag2 = -1;}}else if(e.getSource() == b[4]){if(flag2 == -1)text.setText(text.getText()+"4");else{text.setText("4"); // 数字 4flag2 = -1;}}else if(e.getSource() == b[5]){if(flag2 == -1)text.setText(text.getText()+"5");else{text.setText("5"); // 数字 5flag2 = -1;}}else if(e.getSource() == b[6]){if(flag2 == -1)text.setText(text.getText()+"6");else{text.setText("6"); // 数字 6flag2 = -1;}}else if(e.getSource() == b[7]){if(flag2 == -1)text.setText(text.getText()+"7");else{text.setText("7"); // 数字 7flag2 = -1;}}else if(e.getSource() == b[8]){if(flag2 == -1)text.setText(text.getText()+"8");else{text.setText("8"); // 数字 8flag2 = -1;}}else if(e.getSource() == b[9]){if(flag2 == -1)text.setText(text.getText()+"9");else{text.setText("9"); // 数字 9flag2 = -1;}}} }class CalListener implements ActionListener // 功能按键的事件监听{public void actionPerformed(ActionEvent e) {if(e.getSource() == cal[0]) // + 按钮{if(flag == 0){load1 = text.getText();flag = 1;flag2 = Key = 0;}else if(flag == 1){load2 = text.getText();text.setText(String.valueOf(Double.parseDouble(load1) + Double.parseDouble(load2)));load1 = text.getText();load2 = null;//flag = 0;}}else if(e.getSource() == cal[1]) // - 按钮{if(flag == 0){load1 = text.getText();flag = 1;flag2 = Key = 1;}else if(flag == 1){load2 = text.getText();text.setText(String.valueOf(Double.parseDouble(load1) - Double.parseDouble(load2)));load1 = text.getText();load2 = null;//flag = 0;}}else if(e.getSource() == cal[2]) // * 按钮{if(flag == 0){load1 = text.getText();flag = 1;flag2 = Key = 2;}else if(flag == 1){load2 = text.getText();text.setText(String.valueOf(Double.parseDouble(load1) * Double.parseDouble(load2)));load1 = text.getText();load2 = null;//flag = 0;}}else if(e.getSource() == cal[3]) // 除法 按钮{if(flag == 0){load1 = text.getText();flag = 1;flag2 = Key = 3;}else if(flag == 1){load2 = text.getText();text.setText(String.valueOf(Double.parseDouble(load1) / Double.parseDouble(load2)));load1 = text.getText();load2 = null;//flag = 0;}}else if(e.getSource() == cal[4]) // = 按钮{if(load1 != null && load2 == null){load2 = text.getText();if(Key == 0)text.setText(String.valueOf(Double.parseDouble(load1) + Double.parseDouble(load2)));else if(Key == 1)text.setText(String.valueOf(Double.parseDouble(load1) - Double.parseDouble(load2)));else if(Key == 2)text.setText(String.valueOf(Double.parseDouble(load1) * Double.parseDouble(load2)));else if(Key == 3)text.setText(String.valueOf(Double.parseDouble(load1) / Double.parseDouble(load2)));Key = -1;flag = 0;flag2 = -1;load1 = text.getText();load2 = null;}}}}public static void main(String[] args){ new Calculator().init();}
}
这篇关于Java.awt 计算器小程序源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!