Java實戰(四)定義一個GUI程序,實現簡易的算術運算

定義一個GUI程序,實現簡易的算術運算

(1)定義窗口類,Grid佈局,第一行輸入第一個數據,第二行通過choice選擇 + - * /,第三行輸入第二個數據,第四行按鈕,計算和退出
(2)定義事件處理程序,實現計算和退出功能(計算結果可以採用System.out.println()輸出)
(3)定義測試類

實現代碼:

package CurriculumDesign;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/*
3.定義一個GUI程序,實現簡易的算術運算。
(1)定義窗口類,Grid佈局,第一行輸入第一個數據,第二行通過choice選擇 + - * /,第三行輸入第二個數據,第四行按鈕,計算和退出
(2)定義事件處理程序,實現計算和退出功能(計算結果可以採用System.out.println()輸出)
(3)定義測試類
 */

class MyCalc extends Frame {
    //組件準備
    //第一個數據輸入文本域
    TextField parameter1 = new TextField();
    //操作符選擇下拉框
    Choice sexChoice = new Choice();
    //第二個數據輸入文本域
    TextField parameter2 = new TextField();
    //計算按鈕
    Button doButton = new Button("開始計算");
    //退出按鈕
    Button exitButton = new Button("退出程序");

    //定義構造函數
    public MyCalc(int x, int y, int width, int height, Color color) {
        //設置窗口名稱
        super("MyFrame");

        //設置可見性
        setVisible(true);

        //彈出的初始位置
        setLocation(x, y);

        //設置窗口大小
        setSize(width, height);

        //設置背景顏色
        setBackground(color);

        //設置窗口大小不可改變
        setResizable(false);

        //初始化窗口
        init();
    }

    //窗口初始化
    public void init()
    {
        //網格佈局,4行2列
        setLayout(new GridLayout(4,2));

        //第一行,一個標籤 + 一個文本域
        add(new Label("請輸入第一個數"));
        add(parameter1);

        //第二行,一個標籤 + 選擇框
        add(new Label("請選擇操作符"));
        sexChoice.add("+");
        sexChoice.add("-");
        sexChoice.add("*");
        sexChoice.add("/");
        add(sexChoice);

        //第三行,一個標籤 + 一個文本域
        add(new Label("請輸入第二個數"));
        add(parameter2);

        //第四行,兩個按鈕
        add(doButton);
        add(exitButton);

        //爲按鈕添加監聽器
        doButton.addActionListener(new MyLintener());
        exitButton.addActionListener(new MyLintener());
    }

    //定義監聽器內部類(內部類方便使用外層類屬性和方法)
    class MyLintener implements ActionListener {

        //實現接口
        @Override
        public void actionPerformed(ActionEvent e) {

            //這裏實現了利用一個監聽器監聽多個按鈕對象
            //這裏當點擊按鈕時會按照相應按鈕指令信息進入不同代碼塊
            //如果上面不使用方法setActionCommand("xx")設置按鈕指令信息的話
            //這裏返回的ActionEvent對象獲得的指令默認爲按鈕名稱

            //若點擊的是【開始計算按鈕】
            if (e.getActionCommand() == "開始計算")
            {
                //獲取文本域中的信息
                String str1= parameter1.getText();
                String str2= parameter2.getText();
                //爲了進行計算,需要轉化爲int型
                int p1 = Integer.parseInt(str1);
                int p2 = Integer.parseInt(str2);

                //獲得操作符信息
                String operate = sexChoice.getSelectedItem();

                //用於承接計算結果
                int result = 0;

                //根據操作符不同進行不同的操作
                switch (operate.charAt(0))
                {
                    case '+': result = p1 + p2; break;
                    case '-': result = p1 - p2; break;
                    case '*': result = p1 * p2; break;
                    case '/': result = p1 / p2; break;
                    default: break;
                }

                //打印結果
                System.out.println("運算結果爲:" + result);
            }
            //若點擊的是【退出程序按鈕】
            else if (e.getActionCommand() == "退出程序")
            {
                //退出程序
                System.exit(0);
            }
        }
    }
}

public class Test4 {

    //把關閉窗口的功能拿出來單獨寫一個方法,就可以點窗口上的X關閉窗口了
    //這個方法題目並沒有要求,但是習慣上加上一個
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        //這裏創建一個MyFrame對象並直接傳進靜態方法windowClose()中
        windowClose(new MyCalc(300,300,300,200,Color.pink));
    }
}

運行結果:

  • 窗口樣式:

在這裏插入圖片描述

  • 進行操作( 3+3=6、3-3=0、3*3=9、3/3=1 ):

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

  • 點擊退出程序後成功關閉窗口
    在這裏插入圖片描述
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章