📄 jisuan.java
字号:
// jisuan: Calculator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class jisuan extends JApplet implements ActionListener
{
private JTextField jtf = new JTextField(10);
private boolean newNumber = true;
private int result = 0;
private String op = "=";
Stack cunchu;
public void init()
{
//布局管理,设定按钮
JButton bt;
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
cunchu=new Stack();
JPanel westPanel = new JPanel();
westPanel.setLayout(new GridLayout(5, 0));
westPanel.add(new JButton(" "));
westPanel.add(bt=new JButton("MC"));
bt.addActionListener(this);
westPanel.add(bt=new JButton("MR"));
bt.addActionListener(this);
westPanel.add(bt=new JButton("MS"));
bt.addActionListener(this);
westPanel.add(bt=new JButton("M+"));
bt.addActionListener(this);
Panel centerPanel = new Panel();
centerPanel.setLayout(new BorderLayout());
Panel p1 = new Panel();
Panel p2 = new Panel();
//设定按钮,处理事件
p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
p1.add(bt=new JButton("Back"));
bt.addActionListener(this);
p1.add(bt=new JButton("CE"));
bt.addActionListener(this);
p1.add(bt=new JButton("C"));
bt.addActionListener(this);
p2.setLayout(new GridLayout(4, 5));
p2.add(bt = new JButton("7"));
bt.addActionListener(this);
p2.add(bt = new JButton("8"));
bt.addActionListener(this);
p2.add(bt = new JButton("9"));
bt.addActionListener(this);
p2.add(bt = new JButton("/"));
bt.addActionListener(this);
p2.add(bt = new JButton("4"));
bt.addActionListener(this);
p2.add(bt = new JButton("5"));
bt.addActionListener(this);
p2.add(bt = new JButton("6"));
bt.addActionListener(this);
p2.add(bt = new JButton("*"));
bt.addActionListener(this);
p2.add(bt = new JButton("1"));
bt.addActionListener(this);
p2.add(bt = new JButton("2"));
bt.addActionListener(this);
p2.add(bt = new JButton("3"));
bt.addActionListener(this);
p2.add(bt = new JButton("-"));
bt.addActionListener(this);
p2.add(bt=new JButton("0"));
bt.addActionListener(this);
p2.add(bt = new JButton("%"));
bt.addActionListener(this);
p2.add(bt = new JButton("+"));
bt.addActionListener(this);
p2.add(bt = new JButton("="));
bt.addActionListener(this);
centerPanel.add(p2, BorderLayout.CENTER);
centerPanel.add(p1, BorderLayout.NORTH);
p.add(centerPanel, BorderLayout.CENTER);
p.add(westPanel, BorderLayout.WEST);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p, BorderLayout.CENTER);
getContentPane().add(jtf, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e)
{ //事件处理
String actionCommand = e.getActionCommand();
if(actionCommand.equals("MS"))//对MS进行操作,进栈
{ if(!jtf.getText().equals("")) {
while(!cunchu.empty())cunchu.pop();
cunchu.push(jtf.getText());
}
}
if(actionCommand.equals("MC"))//对MC进行操作,出栈
{
while(!cunchu.empty())cunchu.pop();
}
if(actionCommand.equals("M+"))//对M+进行操作,对栈顶元素乘2
{
if(!cunchu.empty())
{
int abc=new Integer(cunchu.pop().toString()).intValue();
cunchu.push(""+(2*abc));
}
}
if(actionCommand.equals("MR"))//对MR进行操作,返回栈顶元素
{
if(!cunchu.empty()){
jtf.setText(cunchu.peek().toString());
newNumber=false;
result=new Integer(cunchu.peek().toString()).intValue();
}
}
if(actionCommand.charAt(0)=='B')//对Back操作,取消上一位输入的数字
jtf.setText(jtf.getText().substring(0,jtf.getText().length()-1));
if(actionCommand.charAt(0)=='C')//对C操作,清零
{ newNumber=true;
jtf.setText("");
result=0;
}
else if(actionCommand.substring(0,1).equals("CE"))
//对CE操作,取消上一次输入的数字
{
jtf.setText("");
newNumber=true;
}
else if ('0' <= actionCommand.charAt(0) &&
actionCommand.charAt(0) <= '9')//如果输入为数字
{
if (newNumber)
{
jtf.setText(actionCommand);
newNumber = false;
}
else
{
jtf.setText(jtf.getText() + actionCommand);
}
}//是否为加减乘除操作符
else
if(actionCommand.length()==1 && actionCommand.charAt(0)!='C'){
if (newNumber)
{
if (actionCommand.equals("-"))
{
jtf.setText("-");
newNumber = false;
}
else
op = actionCommand;
}
else
{
execute(); //运算操作
op = actionCommand;
}
}
}
void execute()//运算操作
{
int number = new Integer(jtf.getText()).intValue();
System.out.println("number "+op);
switch (op.charAt(0))
{
case '+': result += number; break;
case '-': result -= number; break;
case '*': result *= number; break;
case '/': result /= number; break;
case '%': result %= number; break;
case '=': result = number;
newNumber=false;
}
System.out.println("result "+result);
jtf.setText(new Integer(result).toString());
newNumber = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -