描述:一个简单的计算器小程序 可以实现+、-、*、/运算。具有清零和退格的功能。
主要是熟悉一下javaGUI编程以及内部类的相关知识
计算器面板类实现如下:
package calculator;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CalculatorPanel extends JPanel {
private JLabel display;
private JPanel buttonPanel;
private JPanel toolPanel;
private JPanel panel;
private double result;
private String lastCommand;
private boolean cls;
public CalculatorPanel(){
setLayout(new BorderLayout());
result = 0; //结果
lastCommand = "="; //上一个运算符
cls = true; //清屏标识符
//add display
display = new JLabel("0");
add(display, BorderLayout.NORTH);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
//add the button to a panel
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
addButton(panel,"7", insert);
addButton(panel,"8", insert);
addButton(panel,"9", insert);
addButton(panel,"/", command);
addButton(panel,"4", insert);
addButton(panel,"5", insert);
addButton(panel,"6", insert);
addButton(panel,"*", command);
addButton(panel,"1", insert);
addButton(panel,"2", insert);
addButton(panel,"3", insert);
addButton(panel,"-", command);
addButton(panel,"0", insert);
addButton(panel,".", insert);
addButton(panel,"=", command);
addButton(panel,"+", command);
//create toolPanel
toolPanel = new JPanel();
toolPanel.setLayout(new GridLayout(1, 2));
addButton(toolPanel,"AC", command);
addButton(toolPanel,"BACK", command);
//add panel and toolPanel to buttonPanel
buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(toolPanel, BorderLayout.NORTH);
buttonPanel.add(panel,BorderLayout.CENTER);
//add buttonPanel to CalculatorPanel
add(buttonPanel,BorderLayout.CENTER);
}
private void addButton(JPanel panel,String label,ActionListener listener)
{
JButton button =new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String input = e.getActionCommand();
if(cls){
if(!input.equals("."))//输入不是.才清屏
display.setText("");
cls = false;
}
display.setText(display.getText() + input);
}
}
private class CommandAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String command = e.getActionCommand();
if(command.equals("AC")){
display.setText("0");
result =0;
lastCommand = "=";
cls = true;
return;
}else if(command.equals("BACK")){
String str = display.getText();
str = str.substring(0, str.length()-1);
if(str.isEmpty()) {str = "0";cls = true;}
display.setText(str);
return;
}
if(cls){
if(command.equals("-"))
{
display.setText(command);
cls = false;
}
else lastCommand = command;
}
else{
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
cls = true;
}
}
}
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);
}
}
效果图: