Java實(shí)現(xiàn)簡(jiǎn)單計(jì)算器小程序
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單計(jì)算器小程序的具體代碼,供大家參考,具體內(nèi)容如下
這學(xué)期沒事學(xué)了一點(diǎn)點(diǎn)Java,想寫個(gè)程序練手,因?yàn)橹粚W(xué)了一點(diǎn)點(diǎn),所以暫時(shí)只能先寫個(gè)實(shí)現(xiàn)簡(jiǎn)單功能的計(jì)算器練練。
由于期末來了,沒太多時(shí)間,所以暫時(shí)就實(shí)現(xiàn)最簡(jiǎn)單的功能。
個(gè)人不喜歡用大量文字記敘程序,而且該程序也相對(duì)簡(jiǎn)單,所以直接畫了張程序框架圖,再配合代碼的注釋,就明白程序的原理了。

圖 計(jì)算器程序框架
代碼如下:
package tst.jframe;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
public class javaAP {
/**
* 各控件對(duì)象的建立
*
* */
private JFrame frame;
private JTextField result;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9;
private JButton button_0;
private JButton Button_equal;
private JButton button_dot;
private JButton button_d0;
private JButton button_plus;
private JButton button_sub;
private JButton button_clr;
private JButton button_dlt;
private JButton button_mul;
private JButton button_div;
/**
* 幾個(gè)變量,用于計(jì)算、判定,存放結(jié)構(gòu)等。
* */
private boolean numflag = false; //用于標(biāo)識(shí)是否輸入數(shù)字
private boolean opflag = false; //用于標(biāo)識(shí)是否輸入運(yùn)算操作符
private String txt = null; //顯示結(jié)果的文本框的文本內(nèi)容,用于存放計(jì)算結(jié)果的字符串形式
private String num_txt = ""; //每次輸入一個(gè)數(shù),以字符的形式加到該字符創(chuàng)變量,后面運(yùn)算直接將該字符創(chuàng)轉(zhuǎn)成double類型進(jìn)行運(yùn)算
private String op = " "; //存放輸入的運(yùn)算操作符
private double num1 = 0; //num1和num2用作運(yùn)算
private double num2 = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
javaAP window = new javaAP();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 調(diào)用控件初始化函數(shù),創(chuàng)建程序。
*/
public javaAP() {
initialize();
}
/**
* 初始化計(jì)算器的各個(gè)控件。
*/
private void initialize() {
frame = new JFrame(); //-----------------------------------------------
frame.setResizable(false); //
frame.setBounds(100, 100, 371, 371); // 窗口各個(gè)屬性的初始化
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
frame.getContentPane().setLayout(null); //-----------------------------------------------
result = new JTextField(); //-----------------------------------------------
result.setFont(new Font("Lao UI", Font.BOLD, 15)); //
result.setHorizontalAlignment(SwingConstants.RIGHT); //
result.setBounds(29, 28, 308, 50); // 顯示結(jié)果的文本框的各個(gè)屬性的初始化
frame.getContentPane().add(result); //
result.setColumns(10); //
result.setText(txt); //-----------------------------------------------
/**
* 0-9, 小數(shù)點(diǎn),00 各個(gè)控件的初始化,給每個(gè)控件添加動(dòng)作監(jiān)聽事件,每個(gè)控件被按下則調(diào)用numBtnAction函數(shù)。
*
* */
button_0 = new JButton("0");
button_0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("0");
}
});
button_0.setBounds(99, 273, 50, 50);
frame.getContentPane().add(button_0);
button_1 = new JButton("1");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("1");
}
});
button_1.setBounds(99, 213, 50, 50);
frame.getContentPane().add(button_1);
button_2 = new JButton("2");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("2");
}
});
button_2.setBounds(159, 213, 50, 50);
frame.getContentPane().add(button_2);
button_3 = new JButton("3");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("3");
}
});
button_3.setBounds(219, 213, 50, 50);
frame.getContentPane().add(button_3);
button_4 = new JButton("4");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("4");
}
});
button_4.setBounds(99, 157, 50, 50);
frame.getContentPane().add(button_4);
button_5 = new JButton("5");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("5");
}
});
button_5.setBounds(159, 157, 50, 50);
frame.getContentPane().add(button_5);
button_6 = new JButton("6");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("6");
}
});
button_6.setBounds(219, 157, 50, 50);
frame.getContentPane().add(button_6);
button_7 = new JButton("7");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("7");
}
});
button_7.setBounds(99, 97, 50, 50);
frame.getContentPane().add(button_7);
button_8 = new JButton("8");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("8");
}
});
button_8.setBounds(159, 97, 50, 50);
frame.getContentPane().add(button_8);
button_9 = new JButton("9");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("9");
}
});
button_9.setBounds(219, 97, 50, 50);
frame.getContentPane().add(button_9);
button_dot = new JButton(".");
button_dot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction(".");
}
});
button_dot.setFont(new Font("宋體", Font.BOLD, 15));
button_dot.setBounds(159, 273, 50, 50);
frame.getContentPane().add(button_dot);
button_d0 = new JButton("00");
button_d0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numBtnAction("00");
}
});
button_d0.setBounds(219, 273, 50, 50);
frame.getContentPane().add(button_d0);
/**
* 運(yùn)算符 =,+,-,*,/ 的初始化。
* */
Button_equal = new JButton("=");
Button_equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(opflag == true){
num2 = Double.parseDouble(num_txt); //如果此時(shí)opflag為true的話,證明已經(jīng)輸入運(yùn)算符,這時(shí)候剛輸入的數(shù)為另外一個(gè)操作數(shù),所以賦給num2。
calc(op); //num1和num2進(jìn)行運(yùn)算。
}
result.setText(new Double(num1).toString()); //顯示結(jié)果。
}
});
Button_equal.setBounds(279, 213, 50, 110);
frame.getContentPane().add(Button_equal);
button_plus = new JButton("+");
button_plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opBtnAction("+");
}
});
button_plus.setBounds(279, 97, 50, 50);
frame.getContentPane().add(button_plus);
button_sub = new JButton("-");
button_sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opBtnAction("-");
}
});
button_sub.setBounds(279, 157, 50, 50);
frame.getContentPane().add(button_sub);
button_mul = new JButton("*");
button_mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opBtnAction("*");
}
});
button_mul.setBounds(39, 157, 50, 50);
frame.getContentPane().add(button_mul);
button_div = new JButton("/");
button_div.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
opBtnAction("/");
}
});
button_div.setBounds(39, 97, 50, 50);
frame.getContentPane().add(button_div);
/**
* 功能鍵clr:清空 和 dlt回刪 兩個(gè)按鍵的初始化。
*
* */
button_clr = new JButton("clr");
button_clr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
result.setText(null); //清楚文本框和num_txt,num1和num2全部置為0,
txt = null; //numflag和opflag均置為false,全部置為程序開始的默認(rèn)值。
numflag = opflag = false;
num1 = num2 = 0;
num_txt = "";
}
});
button_clr.setBounds(39, 273, 50, 50);
frame.getContentPane().add(button_clr);
button_dlt = new JButton("dlt");
button_dlt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(num_txt != "" && num_txt.length() > 0) {
num_txt = num_txt.substring(0, num_txt.length() - 1); //通過截取字符串的方式回刪一個(gè)字符
}
if(txt != null && txt.length() > 0) {
txt = result.getText().substring(0,txt.length() - 1); //結(jié)果文本框的回刪
result.setText(txt);
}
}
});
button_dlt.setBounds(39, 213, 50, 50);
frame.getContentPane().add(button_dlt);
}
/**
* 每個(gè)數(shù)字按鍵按下后觸發(fā)的功能。
*
* @param num 傳入字符串形式的數(shù)字
*
* */
private void numBtnAction(String num) {
if(num_txt == "" && opflag == false) { //判斷是否num_txt是否為空,opflag是否為false,若滿足這兩個(gè)
result.setText(null); //條件,則為一次運(yùn)算完畢,切新運(yùn)算不以該次結(jié)果繼續(xù)做運(yùn)算。
num_txt = "";
}
numflag = true; //只要輸入一個(gè)數(shù),則把numflag置為true,表示已有數(shù)字輸入
num_txt += num; //把輸入的數(shù)加入到最終要轉(zhuǎn)換成都double運(yùn)算的字符串
txt = result.getText() + num; //加到txt,使輸入的數(shù)顯示到文本框中。
result.setText(txt);
}
/**
* 點(diǎn)下運(yùn)算符鍵觸發(fā)的動(dòng)作
*
* @param operator 點(diǎn)擊的運(yùn)算符
*
* */
private void opBtnAction(String operator) {
if(opflag == false && num_txt != "") { //進(jìn)行判斷,如果opflag為false,則表示還沒輸入運(yùn)算符,
num1 = Double.parseDouble(num_txt); //這個(gè)時(shí)候把num_txt轉(zhuǎn)換成double賦給num1。
}
if(opflag == true && num_txt != ""){
num2 = Double.parseDouble(num_txt); //如果opflag為true,表示第一個(gè)數(shù)輸入完畢,且已輸入相應(yīng)的運(yùn)算符,這個(gè)時(shí)候是在輸入第二個(gè)要參與運(yùn)算的數(shù),則加到num2
calc(op); //先計(jì)算兩數(shù)結(jié)果,賦給num1,num2置為0,以便后面多層混合運(yùn)算使用。
}
numflag = false; //numflag置為0,表示當(dāng)前需要輸入數(shù)進(jìn)行運(yùn)算
op = operator; //把新輸入的運(yùn)算符賦給op
result.setText(operator + " "); //在文本框顯示
opflag = true; //opflag置為true,表示當(dāng)前已輸入一個(gè)或多個(gè)操作數(shù)。
num_txt = ""; //num_txt置為空,為了存儲(chǔ)下一個(gè)操作數(shù)。
}
/**
* 進(jìn)行 +、-、*、/ 的運(yùn)算
* @param op 運(yùn)算符
*
* */
private void calc(String op) {
switch(op){
case "+" :
num1 = num1 + num2;
num2 = 0;
opflag = false;
num_txt = "";
break;
case "-" :
num1 = num1 - num2;
num2 = 0;
opflag = false;
num_txt = "";
break;
case "*" :
if(num2 != 0) {
num1 = num1 * num2;
num2 = 0;
opflag = false;
num_txt = "";
}
break;
case "/" :
if(num2 != 0) {
num1 = num1 / num2;
num2 = 0;
opflag = false;
num_txt = "";
}
break;
default: opflag = false;
num_txt = "";
break;
}
}
}
關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA使用SVN分支的簡(jiǎn)單介紹
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA使用SVN分支的簡(jiǎn)單介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
使用JPA中@Query 注解實(shí)現(xiàn)update 操作方法(必看)
下面小編就為大家?guī)硪黄褂肑PA中@Query 注解實(shí)現(xiàn)update 操作方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Spring中的@ModelAttribute模型屬性綁定詳解
這篇文章主要介紹了Spring中的@ModelAttribute模型屬性綁定詳解,@ModelAttribute用于將方法參數(shù)或返回值綁定到Model屬性上,并公開給Web視圖,支持使用@RequestMapping注釋的Controller類,需要的朋友可以參考下2024-02-02
Java實(shí)現(xiàn)簡(jiǎn)單的模板渲染
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單的模板渲染的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類示例詳解
這篇文章主要介紹了Java經(jīng)緯度小數(shù)與度分秒相互轉(zhuǎn)換工具類,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Spring Boot整合JWT的實(shí)現(xiàn)步驟
本文主要介紹了Spring Boot整合JWT,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Intellij IDEA下Spring Boot熱切換配置
這篇文章主要介紹了Intellij IDEA下Spring Boot熱切換配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08

