bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bNumber = new JButton("3");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("-");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("1/x");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
pDown.add(bOperator);
bNumber = new JButton("0");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3,3,3,3));
pDown.add(bNumber);
bOperator = new JButton("+/-");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton(".");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("+");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
bOperator = new JButton("=");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3,3,3,3));
pDown.add(bOperator);
pRight.add(pUp,BorderLayout.NORTH);
pRight.add(pDown,BorderLayout.SOUTH);
pBottom.add(pRight,BorderLayout.EAST);
contentPane.add(pBottom,BorderLayout.SOUTH);
f.setSize(new Dimension(320,256));
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
/************************Java计算器 计算方法区***************************/
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("复制(C)"))
{
String temp = tResult.getText().trim();
copyBoard.replace(0, copyBoard.length(), temp);
mPaste.setEnabled(true);
}
else if(s.equals("粘贴(p)"))
{
tResult.setText(copyBoard.toString());
}
else if(s.equals("CE"))
{
//如果是CE则清除文本框
tResult.setText("0.");
}
else if(s.equals("BackSpace"))
{
if(!tResult.getText().trim().equals("0."))
{
//如果文本框中有内容
if(str.length()!=1str.length()!=0)
{
tResult.setText(str.delete(str.length()-1,str.length()).toString());
}
else
{
tResult.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(tResult.getText().trim());
}
else if(s.equals("C"))
{
//如果是C删除当前计算
tResult.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
}
else if(s.equals("MC"))
{
//如果是MC则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
}
else if(s.equals("MR"))
{
//如果按键为MR则恢复缓冲区的数到文本框
tResult.setText(memory.toString());
}
else if(s.equals("MS"))
{
//如果按键为MS则将文本框的数存入缓冲区
String s1 = tResult.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
}
else if(s.equals("M+"))
{
//如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
String temp1 = tResult.getText().trim();
推荐阅读
- flutter控件覆盖,flutter嵌套
- 小米k50怎么降级鸿蒙系统的简单介绍
- 新生电商如何发展,电商如何快速发展
- 江苏专业网站搭建费用低,专业建设网站的公司
- linux性能指标命令 linux常用性能命令
- ChatGPT免费破解最新版,chatcraft pro 破解版
- 包含代理软件的词条
- 公众号文章最后的阅读量,公众号文章最后的阅读量怎么算
- php数据传递js php传数据给ajax的实例