java實現(xiàn)簡單的計算器類實例
更新時間:2015年10月06日 15:25:28 作者:路人甲123
這篇文章主要介紹了java實現(xiàn)簡單的計算器類,涉及java針對鍵盤監(jiān)聽及數(shù)字運(yùn)算的處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了java實現(xiàn)簡單的計算器類。分享給大家供大家參考。具體如下:
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;//計算區(qū)域
private JButton buttonBk,buttonC;//退格鍵和清零鍵
private JButton button[];//數(shù)字鍵組
private JButton buttonDot,buttonAddSub,buttonAdd,buttonSub,buttonMul,buttonDiv,button1,button2,button3,buttonEqual;
private double result;//計算結(jié)果
private final short ADD = 1;
private final short SUB = 2;
private final short MUL = 3;
private final short DIV = 4;
private short operator = -1;//運(yùn)算符
public Calculator(){
frame = new JFrame("計算機(jī)");
frame.setSize(400, 250);
panel = new JPanel();//全局面板
panel.setVisible(true);
frame.setVisible(true);
frame.getContentPane().add(panel);
ActionListener listener = new ComputerActionListener();//按鍵監(jiān)聽器
//計算區(qū)
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("");
}
//數(shù)字鍵
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程序設(shè)計有所幫助。
相關(guān)文章
JAVA中使用FileWriter寫數(shù)據(jù)到文本文件步驟詳解
這篇文章主要介紹了JAVA中使用FileWriter寫數(shù)據(jù)到文本文件步驟詳解,FileWriter類提供了多種寫入字符的方法,包括寫入單個字符、寫入字符數(shù)組和寫入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下2023-10-10
基于Jenkins自動打包并部署docker環(huán)境的操作過程
這篇文章主要介紹了基于Jenkins自動打包并部署docker環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
Java根據(jù)表達(dá)式獲取對象中的值及設(shè)置值的例子
這篇文章主要介紹了Java根據(jù)表達(dá)式獲取對象中的值及設(shè)置值的例子,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-03-03
java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實例詳解
這篇文章主要介紹了java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
使用Spring Cache時設(shè)置緩存鍵的注意事項詳解
在現(xiàn)代的Web應(yīng)用中,緩存是提高系統(tǒng)性能和響應(yīng)速度的重要手段之一,Spring框架提供了強(qiáng)大的緩存支持,通過??@Cacheable??、??@CachePut??、??@CacheEvict??等注解可以方便地實現(xiàn)緩存功能,本文給大家介紹了使用Spring Cache時設(shè)置緩存鍵的注意事項2025-01-01
Java8時間接口LocalDateTime詳細(xì)用法
最近看別人項目源碼,發(fā)現(xiàn)Java8新的日期時間API很方便強(qiáng)大,所以整理了這篇文章,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下2021-05-05
request如何獲取body的json數(shù)據(jù)
這篇文章主要介紹了request如何獲取body的json數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

