Java圖形化界面編程實現簡單計算器
1.簡單計算器
使用AWT
編程,FrameNORTH
區域放置TextField
組件,將指定為4行5列GridLayout佈局管理器的Panel放置於Frame
中部區域,其中填充操作符和操作數按鈕。
按鈕觸發事件源,對按鈕添加ActionListener
註冊監聽。自定義NumListener(操作數監聽類)、OperatListener
(操作符監聽類)、EqualListener
(’=‘符監聽類)和匿名內部類(如b[11]’-'符監聽類)分情況對按鈕事件進行監聽並響應。
註意:整數、浮點、負數以及連續運算均可以。
界面效果:
簡單計算一下 -12 + 3 = -9
代碼:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import static java.awt.Color.blue; public class Calculation { //操作數 double x,y; String op; boolean flag; Frame frame = new Frame("智子的計算器!"); TextField tf = new TextField(30); Button[] b = new Button[20]; public void init(){ //北部區域放置文本框 frame.add(tf,BorderLayout.NORTH); Panel panel = new Panel(); panel.setLayout(new GridLayout(4,5,2,2)); //設置按鈕 String s = "+-*/%"; for(int i=0;i<10;i++) {//運算數 b[i] = new Button(i + ""); b[i].setForeground(blue); } for(int i=0;i<5;i++) {//運算符 b[i+10]=new Button(s.charAt(i)+""); b[i+10].setForeground(blue); } String[] t = {"sqrt","^2","^3","=","."}; for(int i=0;i<5;i++){ b[i+15]=new Button(t[i]); b[i+15].setForeground(blue); } //按鈕註冊監聽 for (int i = 0; i < 10; i++) {//操作數註冊監聽 b[i].addActionListener(new NumListener()); } for (int i = 10; i < 18; i++) {//操作符註冊監聽 if(i==11) continue; b[i].addActionListener(new OperatListener()); } b[11].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!flag){ tf.setText("-"); flag = true; } else { x = Double.parseDouble(tf.getText()); op = e.getActionCommand(); flag = false; } } }); //“=”註冊監聽 b[18].addActionListener(new EqualListener()); //“.”註冊監聽 b[19].addActionListener(new NumListener()); //將按鈕加入panel for (int i = 0; i < 20; i++) { panel.add(b[i]); } //設置中部按鈕 frame.add(panel); //窗口監聽器 註冊監聽 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); //設置窗口最優並可見 frame.pack(); frame.setVisible(true); } //數字按鈕監聽器類 public class NumListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String t = e.getActionCommand(); String s = tf.getText(); if(flag==false) tf.setText(t); else tf.setText(s+t); flag = true; } } //操作符按鈕監聽器類 public class OperatListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { x = Double.parseDouble(tf.getText()); op = e.getActionCommand(); flag = false; } } //等號按鈕監聽器類 public class EqualListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { y = Double.parseDouble(tf.getText()); flag = true; switch(op){ case "+":tf.setText(x+y+""); break; case "-":tf.setText(x-y+""); break; case "*":tf.setText(x*y+""); break; case "/": if(y!=0) tf.setText(x/y+""); else tf.setText("inf"); break; case "%":tf.setText(x%y+""); break; case "sqrt":tf.setText((int)Math.sqrt(x)+""); break; case "^2":tf.setText(y*y+""); break; case "^3":tf.setText(y*y*y+""); break; } } } public static void main(String[] args) { new Calculation().init(); } }
到此這篇關於 Java
圖形化界面編程實現簡單計算器的文章就介紹到這瞭,更多相關 Java圖形化界面編程實現計算器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!