###### tags: `Java`
> [name=王灝珽]
# Java自學紀錄 - 計算機實作
# 構想
寫一個視窗程式,讓使用者輸入2個數,並由使用者點選 ==+== ==-== ==*== ==/== ==%== 按鈕,在輸出其結果。接著可以點選 ==C== ==Exit== 歸零或結束執行。
# 設計步驟
1. 先規畫表單。
2. 版面配置,每一個位置先放置Panel元件。
3. 每一個Panel元件再版面配置,再放入元件。
# 先備技術
* 基本語法
* ***Panel類別***
Panel類別可以建構一個透明容器元件,在較複雜的巢狀版面設計時,可以先整體版面配置,每個位置都放一個Panel元件,然後每一個Panel可再版面配置,再放置元件。
```java=
//宣告
Panel p1 = new Panel();
```
* ***GridLayout方法***
GridLayout的功能是可以規定表單的行數列數以及其間距。
```java=
//做一列數為3,行數為1,且水平其間距為10,垂直間距為5
GridLayout lay1 = new GridLayout(3,1,10,5);
this.setLayout(lay1);
```
* Frame (Label,Button,TextField)
* ActionListener
# Code
```java=
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class Java_calc extends Frame implements ActionListener{
String str1;
String str2;
/*Panel 類別*/
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txf1 = new TextField(str1);
TextField txf2 = new TextField(str2);
Label lblop = new Label("");
Label lbequ = new Label("=");
Label lbout = new Label("");
Button Add = new Button("+");
Button Sub = new Button("-");
Button Mul = new Button("*");
Button Div = new Button("/");
Button Mod = new Button("%");
Button C = new Button("C");
Button Exit = new Button("Exit");
Java_calc(){
this.setBackground(Color.cyan);
this.setTitle("Easy Calculator");
this.setSize(500,500);
/*GridLayout 方法*/
GridLayout lay1 = new GridLayout(3,1,5,5);
this.setLayout(lay1);
this.add(p1);
this.add(p2);
this.add(p3);
GridLayout lay2 = new GridLayout(1,5,5,5);
p1.setLayout(lay2);
p1.add(txf1);
p1.add(lblop);
p1.add(txf2);
p1.add(lbequ);
p1.add(lbout);
GridLayout lay3 = new GridLayout(1,5,5,5);
p2.setLayout(lay3);
p2.add(Add);
p2.add(Sub);
p2.add(Mul);
p2.add(Div);
p2.add(Mod);
GridLayout lay4 = new GridLayout(1,2,5,5);
p3.setLayout(lay4);
p3.add(C);
p3.add(Exit);
Add.addActionListener(this);
Sub.addActionListener(this);
Mul.addActionListener(this);
Div.addActionListener(this);
Mod.addActionListener(this);
C.addActionListener(this);
Exit.addActionListener(this);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str1 = in.nextLine();
String str2 = in.nextLine();
Java_calc frm = new Java_calc();
frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frm.setVisible(true); //開啟表單
}
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
String s1 = txf1.getText(); //拿取字串
int t1 = Integer.parseInt(s1); //字串轉數字
String s2 = txf2.getText();
int t2 = Integer.parseInt(s2);
int ans = 0;
if(e.getSource() == Exit) { //return 0
System.exit(0);
}
else if(e.getSource() == C) { //歸零(空字串)
str1 = "";
str2 = "";
}
if(e.getSource() == Add) {
ans = t1 + t2;
lblop.setText("+");
}
else if(e.getSource() == Sub) {
ans = t1 - t2;
lblop.setText("-");
}
else if(e.getSource() == Mul) {
ans = t1 * t2;
lblop.setText("*");
}
else if(e.getSource() == Div) {
ans = t1 / t2;
lblop.setText("/");
}
else if(e.getSource() == Mod) {
ans = t1 % t2;
lblop.setText("%");
}
lbout.setText(Integer.toString(ans));
}
}
```
---
>顯示結果
:::info
初始畫面,可點擊框框輸入數字
:::

:::info
輸入後點擊按鍵,並輸出結果
:::

:::info
按 ==C== 之後的畫面
:::

:::info
任意縮放視窗一樣可保持彼此相對位置
:::

# 參考資料
從零開始! Java程式設計入門
> [name=洪國勝]