要求: 撰寫一個Java應用程式,采用GridLayout實作如下計算機器的布局及做相應的事件處理
代碼實作(本需求完全有個人實作):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
class MyComputerWin extends JFrame implements ActionListener {
JTextField expressText; //創建一個單行文本框參考
JPanel numPanel, operPanel;// 創建兩個面板參考
JPanel panel; //創建一個面板用于存放文本框與"="按鈕,
//創建按鈕陣列用于存放按鈕,并定義陣列的長度
JButton[] numBtn = new JButton[12];
JButton[] operBtn = new JButton[6];
String express = "";
public void init() {
expressText = new JTextField(1); //實體化單行文本框物件
//實體化面板物件
numPanel = new JPanel();
operPanel = new JPanel();
panel = new JPanel();
//設定面板為網格布局(GridLayout), 注意: 默認是流動布局(FlowLayout)
panel.setLayout(new BorderLayout());
numPanel.setLayout(new GridLayout(4, 3));
operPanel.setLayout(new GridLayout(5, 1));
//將數字按鈕添加到數字面板上
for (int i = 0; i < numBtn.length; i++) {
//創建數字按鈕
if(i>=0&&i<=2) {
numBtn[i] = new JButton(""+(7+i));
}else if(i>=3&&i<=5) {
numBtn[i] = new JButton(""+(i+1));
}else if(i>=6&&i<=8) {
numBtn[i] = new JButton(""+(i-5));
}else {
numBtn[9] = new JButton(""+"00");
numBtn[10] = new JButton(""+"0");
numBtn[11] = new JButton(""+"^");
}
numPanel.add(numBtn[i]);
numBtn[i].addActionListener(this);
}
//設定符號按鈕
operBtn[0] = new JButton("%");
operBtn[1] = new JButton("/");
operBtn[2] = new JButton("*");
operBtn[3] = new JButton("-");
operBtn[4] = new JButton("+");
operBtn[5] = new JButton("=");
//將符號按鈕添加到符號面板上
for (int i = 0; i < operBtn.length-1; i++) {
operPanel.add(operBtn[i]);
operBtn[i].addActionListener(this);
}
panel.add(expressText);
panel.add(operBtn[5],BorderLayout.EAST);
operBtn[5].addActionListener(this);
//將文本框添加到視窗上,并設定為邊界布局
this.add(panel, BorderLayout.NORTH);
//將面板添加到視窗上
this.add(numPanel, BorderLayout.CENTER);
this.add(operPanel, BorderLayout.EAST);
}
public MyComputerWin(String title) {
super(title);
init();
this.setVisible(true);
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.expressText.setBackground(Color.YELLOW);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand(); //獲得發生該事件的相關命令,在這里就是獲得按鈕事件上的數字,
express += s; //將獲得的命令保存起來
expressText.setText(express); //將獲得的命令放入文本框中,
//因為s代表的是單個按鈕事件上的命令,所以這里判斷s獲得的命令是不是"=",
if ("=".equals(s)) {
String[] nums = express.split("\\p{Punct}");// 5+6= 正則運算式 \\p{Punct}代表的是標點符號,這里就是通過標點符號(+,-,*,/.....)來進行分割,然后得到我們想要計算的數字,
String[] opers =express.split("\\d+"); //利用正則運算式中的多位數字來進行分解,然后得到我們想要的運算子號
String oper=opers[1]; //定義一個String型別的變數存盤符號
double num1=Integer.parseInt(nums[0]);
double num2=Integer.parseInt(nums[1]);
double result=1;
switch(oper) {
case "+":express+=num1+num2;break;
case "-":express+=num1-num2;break;
case "*":express+=num1*num2;break;
case "/":if(num2!=0) {
express+=num1/num2;
}else {
express+="分母為0,不能計算!!";
} break;
case "%":express+=num1/100*num2;break;
case "^":for(int i=0;i<num2;i++) {
result*=num1;
}
express+=result;break;
}
//express+=result;
expressText.setText(express);
express=""; //計算完之后將資料清空
}
}
}
public class MyComputer {
public static void main(String[] args) {
new MyComputerWin("我的計算器");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/241306.html
標籤:java
上一篇:使用python獲取天氣介面給指定微信好友發天氣預報
下一篇:Java面向考點復習
