7.一個簡單的四則運算器(Java swing 入門)
阿新 • • 發佈:2020-12-13
技術標籤:Java swing入門之路javaswing前端
package pageDesign;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class calPage {
public void getInfo(String infoAll,JLabel jl){
for (int i =0;i<count;i++ ) {
infoAll = infoAll + click.get(i);
}
jl.setText(infoAll);
}//獲取陣列資訊並顯示在JLabel中
public void getValue(JButton jb,JLabel jl){
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String infoAll = "";
ArrayList<Double> intNone = new ArrayList<>();//空Double陣列,方便置零
ArrayList<String> none = new ArrayList<>();//空String陣列,方便置零
String input = jb.getText();//獲取點選按鈕的文字
if (input == "CE") {
click = none;
infoAll = "";
jl.setText(infoAll);
}//按到清空鍵後的函式
else if(input == "Del") {
if(count>0)//當陣列內有資料
{
infoAll="";
click.remove(--count);
getInfo(infoAll,jl);
}
else {//當陣列內無資料
jl.setText("目前輸入為空");
}
}//按到刪除鍵後的函式
else if(input != "Del"&&input!="CE"&&input!="+"&&input!="-"&&input!="*"&&input!="/"&&input!="="){
click.add(input);
count++;
getInfo(infoAll,jl);
}//正常識別數字按鈕數值
else if(input=="+"||input=="-"||input=="*"||input=="/"){
click.add(input);
double s = 0;
String extra="";
for(int i = count-1;i>=0;i--)
{
extra = extra + click.get(i);
}//獲取前一段數字
s = Double.parseDouble(extra);
cal.add(s);
count1 = count;
count++;
getInfo(infoAll,jl);
}//加減乘除四則運算
else if(input == "="){
double s = 0;
String extra="";
for(int i = count1+1;i<count;i++)
{
extra = extra + click.get(i);
}//獲取後一段數字
s = Double.parseDouble(extra);
cal.add(s);
switch (click.get(count1))
{
case "+":s = cal.get(0)+cal.get(1);break;
case "-":s = cal.get(0)-cal.get(1);break;
case "*":s = cal.get(0)*cal.get(1);break;
case "/":s = cal.get(0)/cal.get(1);break;
}//switch判斷加減乘除符號
infoAll = Double.toString(s);
jl.setText(infoAll);
cal=intNone;
click=none;
count = 0;
count1 = 0;//全部置零
}//等於鍵得數值
}//新增按鈕監聽內容
});
}//獲取按鈕數值並進行進一步的計算
int count = 0;
int count1 = 0;
static public ArrayList<Double> cal = new ArrayList<Double>();//需要計算的數值
private JFrame calAll = new JFrame("計算器 作者:大W的幻想");//主容器
private JPanel numberScr = new JPanel();//顯示數字屏
private JPanel buttonScr = new JPanel();//顯示按鈕屏
private JLabel screen = new JLabel();//顯示計算公式
static public ArrayList<String> click = new ArrayList<String>();//儲存點選後的資料
static public String[] sign = {"7","8","9","+","Del","4","5","6","-","CE","1","2","3","*","(","0",".","=","/",")"};//按鈕對應數值
static private JButton buList = new JButton();//按鈕樣式
public calPage(){
calAll.setBounds(300,200,350,300);
buttonScr.setLayout(new GridLayout(4,5,5,5));
for(String info:sign){
buList = new JButton(info);
buttonScr.add(buList);
getValue(buList,screen);
}//構造所有按鈕
numberScr.add(screen);
calAll.add(numberScr,BorderLayout.NORTH);
calAll.add(buttonScr, BorderLayout.SOUTH);
calAll.setVisible(true);
calAll.setResizable(false);
calAll.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}//構造
}
差點忘了,這是主函式的內容
package com.company;
import pageDesign.calPage;
public class Main {
public static void main(String[] args) {
new calPage();
}
}
因為我兩個函式分開來寫的
程式碼如上,一些新功能的新增會放在後面的部落格中。這個計算器還有()的功能未加入,不過我會適時的修改以達到正常的使用