久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

java簡單實現計算器

瀏覽:3日期:2022-08-19 13:38:18

本文實例為大家分享了java簡單實現計算器的具體代碼,供大家參考,具體內容如下

public class Calculator { static ScriptEngine jse = new ScriptEngineManager().getEngineByName('JavaScript'); private static void CreateFrame() { JFrame f = new JFrame('計算器'); f.setSize(600, 500); f.setVisible(true); f.setLayout(new BorderLayout()); f.setLayout(new GridLayout(6, 3)); f.setLocation(300, 150); JTextArea text = new JTextArea(20, 0); f.add(text, BorderLayout.NORTH); JButton but1 = new JButton('CE'); f.add(but1, BorderLayout.PAGE_END); String a[] = { '=', '7', '8', '9', '4', '5', '6', '1', '2', '3', '0', '+', '-', '*', '/', '.' }; JButton btn[] = new JButton[a.length]; for (int i = 0; i < a.length; i++) { btn[i] = new JButton(a[i]); f.add(btn[i]); } // 功能實現 for (int i = 0; i < a.length; i++) { // 如果不是等于號 if (i != 0) { int j = i; btn[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s = btn[j].getText();// 獲取文本框內容 text.append(s); } }); } else { // 如果點擊等于號 btn[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { // 獲取文本框內容 String gongshi = text.getText(); // 計算獲取的文本框中的內容 String jieguo = jse.eval(gongshi).toString(); text.setText('='); text.setText(jieguo); } catch (Exception t) { text.setText(''); } } }); // CE按鈕 but1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == but1) { text.setText(''); } } }); } } } public static void main(String[] args) { SwingUtilities.invokeLater(Calculator::CreateFrame); }}

效果圖:

java簡單實現計算器

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 文化| 岳阳市| 普宁市| 常德市| 潮州市| 庐江县| 女性| 彭阳县| 灵丘县| 华容县| 香河县| 吴忠市| 泊头市| 百色市| 神木县| 佛山市| 青冈县| 清丰县| 长宁县| 平泉县| 萝北县| 长兴县| 长沙市| 仁化县| 湛江市| 建瓯市| 古田县| 垣曲县| 通化县| 朝阳区| 庆城县| 南华县| 泸溪县| 宁波市| 天津市| 彭水| 凭祥市| 饶阳县| 垫江县| 文安县| 鹤壁市|