java完成簡略的盤算器類實例。本站提示廣大學習愛好者:(java完成簡略的盤算器類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成簡略的盤算器類實例正文
本文實例講述了java完成簡略的盤算器類。分享給年夜家供年夜家參考。詳細以下:
package chap; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Calculator { private JFrame frame; private JPanel panel,panelKeys,panelKeys_up,panelKeys_down; private JTextField textComputer;//盤算區域 private JButton buttonBk,buttonC;//退格鍵和清零鍵 private JButton button[];//數字鍵組 private JButton buttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual; private double result;//盤算成果 private final short ADD = 1; private final short SUB = 2; private final short MUL = 3; private final short DIV = 4; private short operator = -1;//運算符 public Calculator(){ frame = new JFrame("盤算機"); frame.setSize(400, 250); panel = new JPanel();//全局勢板 panel.setVisible(true); frame.setVisible(true); frame.getContentPane().add(panel); ActionListener listener = new ComputerActionListener();//按鍵監聽器 //盤算區 textComputer = new JTextField(15); textComputer.setText(""); textComputer.setEditable(false); textComputer.setBackground(new Color(255,255,255)); //功效鍵上半部門 panelKeys_up = new JPanel(); panelKeys_up.setLayout(new FlowLayout(FlowLayout.RIGHT)); buttonBk = new JButton("Backspace"); buttonBk.setForeground(new Color(255,0,0)); buttonC = new JButton("C"); buttonC.setForeground(new Color(255,0,0)); buttonBk.addActionListener(listener); buttonC.addActionListener(listener); panelKeys_up.add(buttonBk); panelKeys_up.add(buttonC); //功效鍵下半部門 panelKeys_down = new JPanel(); panelKeys_down.setLayout(new GridLayout(4,5)); button = new JButton[10]; for(int i = 0;i < button.length;i++){ button[i] = new JButton(Integer.toString(i)); button[i].setForeground(new Color(255,0,0)); } buttonAddSub = new JButton("+/-"); buttonAddSub.setForeground(new Color(255,0,0)); buttonAdd = new JButton("+"); buttonAdd.setForeground(new Color(255,0,0)); buttonSub = new JButton("-"); buttonSub.setForeground(new Color(255,0,0)); buttonMul = new JButton("*"); buttonMul.setForeground(new Color(255,0,0)); buttonDiv = new JButton("/"); buttonDiv.setForeground(new Color(255,0,0)); button1 = new JButton(); button2 = new JButton(); button3 = new JButton(); button1.setForeground(new Color(255,0,0)); button2.setForeground(new Color(255,0,0)); button3.setForeground(new Color(255,0,0)); buttonEqual = new JButton("="); buttonEqual.setForeground(new Color(255,0,0)); buttonAddSub.addActionListener(listener); buttonAdd.addActionListener(listener); buttonSub.addActionListener(listener); buttonMul.addActionListener(listener); buttonDiv.addActionListener(listener); buttonEqual.addActionListener(listener); for(int i = 0 ; i <=9 ; i++){ button[i].addActionListener(listener); } for(int i = 0 ; i <=9 ; i++){ panelKeys_down.add(button[i]); } panelKeys_down.add(buttonAddSub); panelKeys_down.add(buttonAdd); panelKeys_down.add(buttonSub); panelKeys_down.add(buttonMul); panelKeys_down.add(buttonDiv); panelKeys_down.add(buttonEqual); panel.setLayout(new BorderLayout()); panel.add(textComputer,BorderLayout.NORTH); panel.add(panelKeys_up,BorderLayout.CENTER); panel.add(panelKeys_down,BorderLayout.SOUTH); } class ComputerActionListener implements ActionListener{ @Override public void actionPerformed(ActionEvent event) { // TODO Auto-generated method stub Object keyButton = event.getSource(); String text = textComputer.getText(); DecimalFormat df = new DecimalFormat("0.###########"); //Backspace if(keyButton == buttonBk && text.length() > 0){ textComputer.setText(text.substring(0,text.length()-1)); } //C鍵 if(keyButton == buttonC){ result = 0; textComputer.setText(""); } //數字鍵 for(int i=0;i<10;i++){ if(keyButton == button[i]){ textComputer.setText(text+i); } } if(keyButton == buttonAdd){ operator = 1; } if(keyButton == buttonSub){ operator = 2; } if(keyButton == buttonMul){ operator = 3; } if(keyButton == buttonDiv){ operator = 4; } //符號鍵 if(keyButton == buttonAdd || keyButton == buttonSub || keyButton == buttonMul || keyButton == buttonDiv || keyButton == buttonEqual){ switch(operator){ case ADD: result += Double.parseDouble(text); break; case SUB: result -=Double.parseDouble(text); break; case MUL: result *=Double.parseDouble(text); break; case DIV: result /=Double.parseDouble(text); break; default: result = Double.parseDouble(text); } textComputer.setText(""); } if(keyButton == buttonEqual){ textComputer.setText(String.valueOf(result)); } } } public static void main(String args[]){ new Calculator(); } }
願望本文所述對年夜家的java法式設計有所贊助。