基于Java swing組件實現簡易計算器
本文記錄了筆者的第一個Java程序,基于Java抽象窗口工具(abstract window toolkit , AWT)和Swing(Swing屬于Java Foundation Classes的一部分)實現的建議計算器,由于筆者經驗有限,初學Java,代碼略帶bug,無法實現7+5×8之類式子的計算,只能實現算術運算符按從高到低的式子運算,部分代碼略顯冗雜,希望大家在評論區積極討論完善代碼!
計算器示意圖
一、代碼相關知識簡介
JFrame(框架)
使用JFrame frame = new JFrame('My Frame');可以創建一個名為My Frame的windows框架
import javax.swing.*;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame('My Frame');frame.setSize(300,300); frame.setVisible(true); }}
JButton(按鈕)
使用JButton b = new JButtton('My Button');可創建一個按鈕組件。
import java.awt.*;import javax.swing.*;public class Test { JFrame frame; public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame('My Frame'); JButton b = new JButton('My Button');frame.getContentPane().add(b,BorderLayout.CENTER); //將按鈕放在frame框架中央 frame.setSize(300,300); frame.setVisible(true); }}
JPanel(面板)
面板是一個容器,與頂層容器不同,JPanel不能獨立存在,必須放在其他容器的內部,下面代碼創建了含有一個按鈕的紅色面板。
import java.awt.*;import javax.swing.*;public class Test { JFrame frame; public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame('My Frame'); JButton b = new JButton('My Button'); JPanel panel = new JPanel();panel.add(b); panel.setBackground(Color.red); frame.getContentPane().add(panel,BorderLayout.SOUTH); //將面板放在frame框架南方 frame.setSize(300,300); frame.setVisible(true); }}
JTextArea(文本輸入框)
使用 JTextArea 類可實現一個文本域,其常用構造方法如下。
①JTextArea():創建一個默認的文本域。
②JTextArea(int rows,int columns):創建一個具有指定行數和列數的文本域。
③JTextArea(String text):創建一個包含指定文本的文本域。
④JTextArea(String text,int rows,int columns):創建一個既包含指定文本,又包含指定行數和列數的多行文本域。
出相關組件介紹外與實現計算器還需對布局有簡單了解,本文僅使用GridLayout布局管理器,因此只對此做出介紹,若讀者需要還可自行理解其他布局管理器。
GridLayout是一種網絡式的布局管理器,將容器空間化為幾行幾列的形式網格,可將每個組件放在其中一格。
GridLayout定義在java.awt包中,有如下三種構造方法public GridLayout()public GridLayout(int rows , int cols) //定義的布局有rows行cools列public GridLayout(int rows , int cols,int h , int w) ////定義的布局有rows行cools列,水平間距為h,垂直間距為w
二、計算器功能
可實現加、減、乘、除功能,但由于筆者目前能力有限,若使用加、減、乘、除混合功能時需按運算符優先級,從高到小輸入式子如7×8+5而不能按5+7×8輸入,源代碼如下:
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Calculator implements ActionListener{ JFrame frame; JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bd,be,bf,bg,bh,b0,Clear; JTextArea ta; String Textcontent ='',sum=''; double result=0; public static void main(String[] args) { // TODO Auto-generated method stub Calculator cl = new Calculator(); cl.go(); } public void go() { frame = new JFrame('Calculator'); ta = new JTextArea(1,20);//設置文本框大小為1行20列 ta.setBackground(Color.lightGray); JPanel cp = new JPanel(); cp.setLayout(new GridLayout(4,4,5,5)); //四行四列,邊距為5 JPanel c = new JPanel(); c.setLayout(new GridLayout(1,2,5,5)); //一行兩列,邊距為5 b0 = new JButton('0'); b0.addActionListener(this); //為每個按鈕添加監聽接口b1 = new JButton('1'); b1.addActionListener(this);b2 = new JButton('2'); b2.addActionListener(this);b3 = new JButton('3'); b3.addActionListener(this);b4 = new JButton('4'); b4.addActionListener(this);b5 = new JButton('5'); b5.addActionListener(this);b6 = new JButton('6'); b6.addActionListener(this);b7 = new JButton('7'); b7.addActionListener(this);b8 = new JButton('8'); b8.addActionListener(this);b9 = new JButton('9'); b9.addActionListener(this);ba = new JButton('.'); ba.addActionListener(this);bd = new JButton('+'); bd.addActionListener(this);be = new JButton('-'); be.addActionListener(this);bf = new JButton('×'); bf.addActionListener(this);bg = new JButton('/'); bg.addActionListener(this);bh = new JButton('='); bh.addActionListener(this);Clear= new JButton('Clear'); Clear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {Textcontent ='';result=0;sum='';ta.setText(''); } });c.add(ta); c.add(Clear); c.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); cp.add(b7); cp.add(b8); cp.add(b9); cp.add(bd); cp.add(b4); cp.add(b5); cp.add(b6); cp.add(be); cp.add(b1); cp.add(b2); cp.add(b3); cp.add(bf); cp.add(b0); cp.add(ba); cp.add(bh); cp.add(bg); cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));Container f = frame.getContentPane(); f.add(c,BorderLayout.NORTH); f.add(cp,BorderLayout.SOUTH);frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { String content = e.getActionCommand(); ta.append(e.getActionCommand()); getTextContent(content); } public void getTextContent(String content) { if(content.equals('+')||content.equals('-')||content.equals('×')||content.equals('/')) { Textcontent = Textcontent+' '+content+' '; } else if(content.equals('=')) { Textcontent = Textcontent+' '+content; sum=GetResult(Textcontent); } else { Textcontent = Textcontent+content; } ta.append(sum); } public String GetResult(String Textcontent) { String n=Textcontent; String []content=n.split(' '); result = Double.valueOf(content[0]); for(int i=1;i<content.length;i++) { switch(content[i]) { case '+': result = result+Double.valueOf(content[i+1]);break; case '-': result = result-Double.valueOf(content[i+1]);break; case '×':result = result*Double.valueOf(content[i+1]);break; case '/': result = result/Double.valueOf(content[i+1]);break; case '=':break; } } return result+''; }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: