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);
}
}