java映像介面開發簡單一實例-JButton及事件的簡單應用

來源:互聯網
上載者:User

java映像介面開發簡單一實例

JButton及事件的簡單應用,做一個計算機的小例子,代碼如下: 

import java.awt.BorderLayout;
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;

/**
 * 計算機執行個體
 * @author 左傑 jdk5.0
 */
public class CalculatorFrame extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public CalculatorFrame() {
        setTitle("計算機");
        CalculatorPanel panel = new CalculatorPanel();
        add(panel);
        pack();
    }

    public static void main(String[] args) {
        CalculatorFrame frame = new CalculatorFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

/**
 * 建立顯示計算機的面板
 */
class CalculatorPanel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private JButton display;//用於顯示結果
    private JPanel panel;//建立用於顯示數字及符號按鈕的面板
    private double result;//用於儲存計算結果
    private String lastCommand;//記錄點擊的符號
    private boolean start;//開始標誌
    
    public CalculatorPanel() {
        setLayout(new BorderLayout());
        result = 0;
        lastCommand = "=";
        start = true;

        // 添加
        display = new JButton("0");
        display.setEnabled(false);
        add(display, BorderLayout.NORTH);
        
        //建立事件偵聽對象
        ActionListener insert = new InsertAction();
        ActionListener command = new CommandAction();

        //添加數字及符號按鈕的面板面板
        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));//設定面板布局為4行4列
        //添加數字及符號按鈕
        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);

        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("*", command);

        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("-", command);

        addButton("0", insert);
        addButton(".", insert);
        addButton("=", command);
        addButton("+", command);

        add(panel, BorderLayout.CENTER);
    }

    /**
     * 用來設定按鈕及添加相應的監聽
     * 
     * @param label      按鈕顯示的內容
     * @param listener   相應事件偵聽對象
     *            
     */
    private void addButton(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel.add(button);
    }

    /**
     * 點擊數字按鈕事件,用於顯示數字及將start標誌置為false;
     */
    private class InsertAction implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String input = event.getActionCommand();
            if (start) {
                display.setText("");
                start = false;
            }
            display.setText(display.getText() + input);
        }
    }

    /**
     * 點擊符號按鈕事件,並進行計算
     */
    private class CommandAction implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String command = event.getActionCommand();
            //如果start為true,表示開始那麼點擊-號表示輸入負數
            if (start) {
                if (command.equals("-")) {
                    display.setText(command);
                    start = false;
                } else
                    lastCommand = command;
            } else {//start為false,進行計算
                calculate(Double.parseDouble(display.getText()));
                lastCommand = command;
                start = true;
            }
        }
    }

    /**
     * 計算方法更具輸入的資料和符號進行計算
     * 
     * @param x 輸入的數字
     *
     */
    public void calculate(double x) {
        if (lastCommand.equals("+"))
            result += x;
        else if (lastCommand.equals("-"))
            result -= x;
        else if (lastCommand.equals("*"))
            result *= x;
        else if (lastCommand.equals("/"))
            result /= x;
        else if (lastCommand.equals("="))
            result = x;
        display.setText("" + result);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.