手把手教你用Java的swing制作计算器
生活随笔
收集整理的這篇文章主要介紹了
手把手教你用Java的swing制作计算器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
其實學到Java這一塊很多人會覺得很復雜實際上學會使用方法其實很簡單
話不多說直接貼源碼,如下:
package cn.sjy.calculator;import javax.swing.*; import java.awt.*;/*** 簡易計算器* @author 石俊熠* 2020.7.13 11:24* 注:仿照某Java大佬的源碼改之*/ public class Test {private String str=" ";//輸入輸出框顯示內(nèi)容private JTextField input;//輸出框private JPanel button;//按鈕區(qū)域private String[] addbutton={"AC","求根","取反","/","7","8","9","*","4","5","6","-","1","2","3","+","+/-","0",".","="};private String showTextFiledNew;//設置主窗體public Test() {//初始化窗體JFrame f = new JFrame("計算器");Container c = f.getContentPane();c.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));//設置排列布局為Y軸排列f.setLocation(200, 300);//f.setSize(500, 600);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);input = new JTextField(30);//設置輸入框的長度為30input.setHorizontalAlignment(JTextField.RIGHT);//設置輸入框內(nèi)容的對齊方式,設置為右對齊JPanel jPanel = new JPanel();//建立一個面板jPanel.add(input);c.add(jPanel);GridBagLayout gridBagLayout = new GridBagLayout();GridBagConstraints cs = new GridBagConstraints();button = new JPanel();button.setLayout(gridBagLayout);for (int i = 0; i < addbutton.length; i++) {if ((i + 1) % 4 == 0) {cs.gridwidth = GridBagConstraints.REMAINDER;} else {cs.fill = GridBagConstraints.BOTH;//使組件完全填充其顯示區(qū)域cs.weightx = 1.0;cs.gridwidth = 1;}JButton b = new JButton(addbutton[i]);gridBagLayout.setConstraints(b, cs);//設置組件的約束b.addActionListener(e -> {String command = e.getActionCommand();setShowTextFiledNew(command);});button.add(b);}c.add(button);f.pack();}/*** 設置顯示內(nèi)容得窗體* @param command 按鈕點擊命令* 如果按下=執(zhí)行計算命令* 如果按下運算符 則格式為 空格+運算符+空格* 如果按下數(shù)字 則直接拼接* 需要更多資料可以加群:756584822*/public void setShowTextFiledNew(String command) {if(command.equals("=")){str = getResult(str);}else if(command.equals("+")||command.equals("-")||command.equals("*")||command.equals("/")||command.equals("求根")||command.equals("取反")){str = str+" "+command+" ";}else if(command.equals("AC")){str = "";}else{str = str+command;}input.setText(str);//將按鈕的信息返回到input中}/*** 計算模塊* @param str 需要計算的字符串* 根據(jù)空格進行分割成字符串數(shù)組* 然后判斷是哪種類型進行計算*通過一個result來存放那個結(jié)果*/private String getResult(String str) {Double result = 0.0;//用來存放結(jié)果String[] need = str.split(" ");//進行字符串分割for (String j : need) {System.out.print(j+" ");}for (int i = 0; i < need.length; i++) {switch (need[i]){case "+":if (result == 0.0 && i==1){result=result+(Double.parseDouble(need[i-1])+Double.parseDouble(need[i+1]));break;}else{result=result+Double.parseDouble(need[i+1]);break;}case "-":if(Double.parseDouble(need[i-1])>=Double.parseDouble(need[i+1])){result = (Double.parseDouble(need[i-1])-Double.parseDouble(need[i+1]));break;}else {result = +(Double.parseDouble(need[i-1])-Double.parseDouble(need[i+1]));break;}case "*":result=result+(Double.parseDouble(need[i-1])*Double.parseDouble(need[i+1]));break;case "/":result=result+(Double.parseDouble(need[i-1])/Double.parseDouble(need[i+1]));break;case "求根":result=result+(Math.sqrt(Double.parseDouble(need[i-1])));break;case "取反":result=result+(-Double.parseDouble(need[i-1]));break;}}return result+"";}//設置主方法,調(diào)用Test方法public static void main(String[] args ){new Test();} }效果如下總結(jié)
以上是生活随笔為你收集整理的手把手教你用Java的swing制作计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大神讲解Java for循环的几种用法
- 下一篇: Java Dao模式通过JDBC连接数据