?? calculator.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Calculator
{
public static void main(String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
A frame with a calculator panel.
*/
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
super("Calculator");
Container contentPane = getContentPane();
setSize(400,300);
setLocation(0,0);
CalculatorPanel panel = new CalculatorPanel();
contentPane.add(panel);
}
}
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());//設定布局
init();
//顯示區域
display = new JTextField("0.");
display.setHorizontalAlignment(JTextField.RIGHT );
display.setBackground(Color.white);
display.setEditable(false);
add(display,BorderLayout.CENTER);
//鍵盤區域
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();
panel.setLayout(new GridLayout(5,6,6,15));
dispM=new JTextField("");
dispM.setEditable(false);
dispM.setBackground(Color.white.brighter());
panel.add(dispM);
addButton("C", command);
addButton("CE",command);
addButton("退格",command);
addButton("=", command);
addButton(" ",command);
addButton("MC",command);
addButton("7", insert);
addButton("8", insert);
addButton("9", insert);
addButton("/", command);
addButton("sqrt",command);
addButton("MR",command);
addButton("4", insert);
addButton("5", insert);
addButton("6", insert);
addButton("*", command);
addButton("%",command);
addButton("MS",command);
addButton("1", insert);
addButton("2", insert);
addButton("3", insert);
addButton("-", command);
addButton("1/x",command);
addButton("M+", command);
addButton("0", insert);
addButton("+/-", command);
addButton(".", insert);
addButton("+",command);
addButton("ran", command);
add(panel,BorderLayout.SOUTH);
}
private void init()
{
result = 0;
lastCommand = "=";
start = true;
tag=true;
}
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
button.setBackground(Color.white.brighter());
panel.add(button);
}
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 evt)
{
String command = evt.getActionCommand();
if (command.equals("C"))
{
init();//之前的數被清空
display.setText("0");
}
else if(command.equals("CE"))
{
display.setText(" ");//之前的數還在result中
start=true;
}
else if(command.equals("退格"))
{
String tem=display.getText();
String t=tem.substring(0,tem.length()-1);
display.setText(t);
}
else if(command.equals("ran"))
{
display.setText(""+Math.random());
}
else if(command.equals("MS"))
{
mem=Double.parseDouble(display.getText());
dispM.setText("M");
start=true;
}
else if(command.equals("MR"))
{
display.setText(""+mem);
}
else if(command.equals("MC"))
{
dispM.setText("");
mem=0;
}
else if(command.equals("M+"))
{
dispM.setText("M");
double p=Double.parseDouble(display.getText());
mem+=p;
display.setText(""+mem);
start=true;
}
else if (command.equals("+/-"))
{
String tmp=display.getText();
char sign=tmp.charAt(0);
if (sign=='-')tag=false;
else tag=true;
if(tag)
{
display.setText("-"+display.getText());
tag=false;
}
else
{
String t=tmp.substring(1,tmp.length());
display.setText(t);
tag=true;
}
}
else
{
calculate(Double.parseDouble(display.getText()));//將運算符前的字符串轉化成數字
lastCommand = command;
start = 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 =0.01*x;
else if (lastCommand.equals("1/x")) result=1/x;
else if (lastCommand.equals("sqrt")) result=Math.sqrt(x);
else if (lastCommand.equals("=")) result = x;
display.setText("" + result);
}
private JTextField display;
private JTextField dispM;
private JPanel panel;
private double result;
private boolean tag;
private String lastCommand;
private boolean start;
private double mem;
private double copy;
}
/*使用存儲的數
要存儲顯示的數據,請單擊“MS”。
要重新調用存儲的數據,請單擊“MR”。
要清除內存,請單擊“MC”。
把所顯示的數字與內存中的數字相加,請單擊“M+”。要查看新數據,請單擊“MR”。
注意
存儲數據時,存儲選項上方的框中會顯示出“M”。存入其他數據時,存儲器中的數據將被替換。
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -