基于Java swing組件實現簡易計算器
本文記錄了筆者的第一個Java程序,基于Java抽象窗口工具(abstract window toolkit , AWT)和Swing(Swing屬于Java Foundation Classes的一部分)實現的建議計算器,由于筆者經驗有限,初學Java,代碼略帶bug,無法實現7+5×8之類式子的計算,只能實現算術運算符按從高到低的式子運算,部分代碼略顯冗雜,希望大家在評論區(qū)積極討論完善代碼!
計算器示意圖

一、代碼相關知識簡介
JFrame(框架)
使用JFrame frame = new JFrame("My Frame");可以創(chuàng)建一個名為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");可創(chuàng)建一個按鈕組件。
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不能獨立存在,必須放在其他容器的內部,下面代碼創(chuàng)建了含有一個按鈕的紅色面板。
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():創(chuàng)建一個默認的文本域。
②JTextArea(int rows,int columns):創(chuàng)建一個具有指定行數和列數的文本域。
③JTextArea(String text):創(chuàng)建一個包含指定文本的文本域。
④JTextArea(String text,int rows,int columns):創(chuàng)建一個既包含指定文本,又包含指定行數和列數的多行文本域。
出相關組件介紹外與實現計算器還需對布局有簡單了解,本文僅使用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
二、計算器功能
可實現加、減、乘、除功能,但由于筆者目前能力有限,若使用加、減、乘、除混合功能時需按運算符優(yōu)先級,從高到小輸入式子如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); //為每個按鈕添加監(jiān)聽接口
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+"";
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ByteArrayOutputStream簡介和使用_動力節(jié)點Java學院整理
ByteArrayOutputStream 是字節(jié)數組輸出流。它繼承于OutputStream。這篇文章主要介紹了ByteArrayOutputStream簡介和使用,需要的朋友可以參考下2017-05-05
FluentMybatis實現mybatis動態(tài)sql拼裝和fluent api語法
本文主要介紹了FluentMybatis實現mybatis動態(tài)sql拼裝和fluent api語法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
詳解Java中的File文件類以及FileDescriptor文件描述類
在Java中File類可以用來新建文件和目錄對象,而FileDescriptor類則被用來表示文件或目錄的可操作性,接下來我們就來詳解Java中的File文件類以及FileDescriptor文件描述類2016-06-06
解決mybatis 中collection嵌套collection引發(fā)的bug
這篇文章主要介紹了解決mybatis 中collection嵌套collection引發(fā)的bug,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
SpringBoot使用validation-api實現參數校驗的示例
這篇文章主要介紹了SpringBoot使用validation-api實現參數校驗的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

