java影象介面開發簡單例項-JButton及事件的簡單應用
阿新 • • 發佈:2018-12-30
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
*/publicclass CalculatorFrame extends JFrame {
/**
*
*/ privatestaticfinallong serialVersionUID =1L;
public CalculatorFrame() {
setTitle("計算器");
CalculatorPanel panel =new CalculatorPanel();
add(panel);
pack();
}
publicstaticvoid main(String[] args) {
CalculatorFrame frame =new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible( true);
}
}
/**
* 建立顯示計算器的面板
*/class CalculatorPanel extends JPanel {
/**
*
*/privatestaticfinallong serialVersionUID =1L;
private JButton display;//用於顯示結果private JPanel panel;//建立用於顯示數字及符號按鈕的面板privatedouble result;//用於儲存計算結果private String lastCommand;//記錄點選的符號privateboolean 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 相應事件偵聽物件
*
*/privatevoid addButton(String label, ActionListener listener) {
JButton button =new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
/**
* 點選數字按鈕事件,用於顯示數字及將start標誌置為false;
*/privateclass InsertAction implements ActionListener {
publicvoid actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
display.setText("");
start =false;
}
display.setText(display.getText() + input);
}
}
/**
* 點選符號按鈕事件,並進行計算
*/privateclass CommandAction implements ActionListener {
publicvoid 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 輸入的數字
*
*/publicvoid calculate(double x) {
if (lastCommand.equals("+"))
result += x;
elseif (lastCommand.equals("-"))
result -= x;
elseif (lastCommand.equals("*"))
result *= x;
elseif (lastCommand.equals("/"))
result /= x;
elseif (lastCommand.equals("="))
result = x;
display.setText(""+ result);
}
}
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
*/publicclass CalculatorFrame extends JFrame {
/**
*
*/
public CalculatorFrame() {
setTitle("計算器");
CalculatorPanel panel =new CalculatorPanel();
add(panel);
pack();
}
publicstaticvoid main(String[] args) {
CalculatorFrame frame =new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(
}
}
/**
* 建立顯示計算器的面板
*/class CalculatorPanel extends JPanel {
/**
*
*/privatestaticfinallong serialVersionUID =1L;
private JButton display;//用於顯示結果private JPanel panel;//建立用於顯示數字及符號按鈕的面板privatedouble result;//用於儲存計算結果private String lastCommand;//記錄點選的符號privateboolean start;//開始標誌public CalculatorPanel() {
setLayout(
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 相應事件偵聽物件
*
*/privatevoid addButton(String label, ActionListener listener) {
JButton button =new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
/**
* 點選數字按鈕事件,用於顯示數字及將start標誌置為false;
*/privateclass InsertAction implements ActionListener {
publicvoid actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
display.setText("");
start =false;
}
display.setText(display.getText() + input);
}
}
/**
* 點選符號按鈕事件,並進行計算
*/privateclass CommandAction implements ActionListener {
publicvoid 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 輸入的數字
*
*/publicvoid calculate(double x) {
if (lastCommand.equals("+"))
result += x;
elseif (lastCommand.equals("-"))
result -= x;
elseif (lastCommand.equals("*"))
result *= x;
elseif (lastCommand.equals("/"))
result /= x;
elseif (lastCommand.equals("="))
result = x;
display.setText(""+ result);
}
}