📄 jisuanqi.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JiSuanQi
{
public static void main(String args[])
{
new JisuanqiFrame();
}
}
class JisuanqiFrame extends Frame
{
JFrame frame=new JFrame("Calculator");
Container container=frame.getContentPane();
JButton button1=new JButton("sin");
JButton button2=new JButton("MC");
JButton button3=new JButton("0");
JButton button4=new JButton("1");
JButton button5=new JButton("2");
JButton button6=new JButton("+");
JButton button7=new JButton("cos");
JButton button8=new JButton("MR");
JButton button9=new JButton("3");
JButton button10=new JButton("4");
JButton button11=new JButton("5");
JButton button12=new JButton("-");
JButton button13=new JButton("tan");
JButton button14=new JButton("MS");
JButton button15=new JButton("6");
JButton button16=new JButton("7");
JButton button17=new JButton("8");
JButton button18=new JButton("x");
JButton button19=new JButton("+/-");
JButton button20=new JButton("M+");
JButton button21=new JButton("9");
JButton button22=new JButton(".");
JButton button23=new JButton("=");
JButton button24=new JButton("/");
JTextField textField1=new JTextField(8);
JTextField textField2=new JTextField(18);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JPanel panel5=new JPanel();
JLabel label1=new JLabel(" ");
public JisuanqiFrame ()
{
container.setLayout(new GridLayout(5,1));
button1.addActionListener(new Me());
button2.addActionListener(new Me());
button3.addActionListener(new Me());
button4.addActionListener(new Me());
button5.addActionListener(new Me());
button6.addActionListener(new Me());
button7.addActionListener(new Me());
button8.addActionListener(new Me());
button9.addActionListener(new Me());
button10.addActionListener(new Me());
button11.addActionListener(new Me());
button12.addActionListener(new Me());
button13.addActionListener(new Me());
button14.addActionListener(new Me());
button15.addActionListener(new Me());
button16.addActionListener(new Me());
button17.addActionListener(new Me());
button18.addActionListener(new Me());
button19.addActionListener(new Me());
button20.addActionListener(new Me());
button21.addActionListener(new Me());
button22.addActionListener(new Me());
button23.addActionListener(new Me());
button24.addActionListener(new Me());
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
panel1.add(textField1);
panel1.add(label1);
panel1.add(textField2);
panel2.setLayout(new GridLayout(1,5));
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
panel2.add(button5);
panel2.add(button6);
panel3.setLayout(new GridLayout(1,5));
panel3.add(button7);
panel3.add(button8);
panel3.add(button9);
panel3.add(button10);
panel3.add(button11);
panel3.add(button12);
panel4.setLayout(new GridLayout(1,5));
panel4.add(button13);
panel4.add(button14);
panel4.add(button15);
panel4.add(button16);
panel4.add(button17);
panel4.add(button18);
panel5.setLayout(new GridLayout(1,5));
panel5.add(button19);
panel5.add(button20);
panel5.add(button21);
panel5.add(button22);
panel5.add(button23);
panel5.add(button24);
container.add(panel1);
container.add(panel2);
container.add(panel3);
container.add(panel4);
container.add(panel5);
frame.setBounds(350,350,450,200);
frame.show();
}
double temp1=0,temp2=0,temp=0;
char s;
int n=0;//记录数中小数点的个数
Stack s1 = new Stack();//栈12用于最初的四则存储
Stack s2 = new Stack();
Stack s3 = new Stack();//栈34用于加减存储
Stack s4 = new Stack();
double a=0,b=0,c=0;
String m;
class Me implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button3)
{
textField2.setText(textField2.getText()+"0");
textField1.setText(textField1.getText()+"0");
}
if(e.getSource()==button4)
{
textField2.setText(textField2.getText()+"1");
textField1.setText(textField1.getText()+"1");
}
if(e.getSource()==button5)
{
textField2.setText(textField2.getText()+"2");
textField1.setText(textField1.getText()+"2");
}
if(e.getSource()==button9)
{
textField2.setText(textField2.getText()+"3");
textField1.setText(textField1.getText()+"3");
}
if(e.getSource()==button10)
{
textField2.setText(textField2.getText()+"4");
textField1.setText(textField1.getText()+"4");
}
if(e.getSource()==button11)
{
textField2.setText(textField2.getText()+"5");
textField1.setText(textField1.getText()+"5");
}
if(e.getSource()==button15)
{
textField2.setText(textField2.getText()+"6");
textField1.setText(textField1.getText()+"6");
}
if(e.getSource()==button16)
{
textField2.setText(textField2.getText()+"7");
textField1.setText(textField1.getText()+"7");
}
if(e.getSource()==button17)
{
textField2.setText(textField2.getText()+"8");
textField1.setText(textField1.getText()+"8");
}
if(e.getSource()==button21)
{
textField2.setText(textField2.getText()+"9");
textField1.setText(textField1.getText()+"9");
}
if(e.getSource()==button22)
{
if(n==1)
JOptionPane.showMessageDialog( null, "error" );
else
{
textField2.setText(textField2.getText()+".");
textField1.setText(textField1.getText()+".");
n=1;
}
}
if(e.getSource()==button6)//+
{
textField2.setText(textField2.getText()+"+");
// temp1=Double.parseDouble(textField1.getText());
s1.push(textField1.getText());
s2.push("+");
textField1.setText(" ");
n=0;
}
if(e.getSource()==button12)//-
{
textField2.setText(textField2.getText()+"-");
// temp1=Double.parseDouble(textField1.getText());
s1.push(textField1.getText());
s2.push("-");
textField1.setText(" ");
n=0;
}
if(e.getSource()==button18)
{
textField2.setText(textField2.getText()+"*");
// temp1=Double.parseDouble(textField1.getText());
s1.push(textField1.getText());
s2.push("*");
textField1.setText(" ");
n=0;
}
if(e.getSource()==button24)
{
textField2.setText(textField2.getText()+"/");
// temp1=Double.parseDouble(textField1.getText());
s1.push(textField1.getText());
s2.push("/");
textField1.setText(" ");
n=0;
}
/* if(e.getSource()==button1)//sin
{
temp2=Double.parseDouble(textField1.getText());
temp1=Math.sin(temp1);
s1.push(temp1+"");
textField1.setText(" ");
}
if(e.getSource()==button7)//cos
{
temp2=Double.parseDouble(textField1.getText());
temp1=Math.cos(temp1);
s1.push(temp1+"");
textField1.setText(" ");
}
if(e.getSource()==button1)//tan
{
temp2=Double.parseDouble(textField1.getText());
temp1=Math.tan(temp1);
s1.push(temp1+"");
textField1.setText(" ");
}
*/if(e.getSource()==button2)//清零
{
textField1.setText(" ");
textField2.setText(" ");
}
if(e.getSource()==button23)//=
{
textField2.setText(textField2.getText()+"=");
// temp1=Double.parseDouble(textField1.getText());
s1.push(textField1.getText());
while(!s2.isEmpty())
{
if(s2.pop()=="*")
{
a=Double.parseDouble(s1.pop()+"");
b=Double.parseDouble(s1.pop()+"");
c=a*b;
s1.push(c+"");
}
if(s2.pop()=="/")//异常:空堆栈
{
a=Double.parseDouble(s1.pop()+"");
b=Double.parseDouble(s1.pop()+"");
c=a/b;
s1.push(c+"");
}
if(s2.pop()=="+")
{
s4.push("+");
m=s1.pop()+"";
s3.push(m);
}
if(s2.pop()=="-")
{
s4.push("-");
m=s1.pop()+"";
s3.push(m);
}
}
m=s1.pop()+"";
s3.push(m);
while(!s4.isEmpty())
{
if(s4.pop()=="+")
{
a=Double.parseDouble(s3.pop()+"");
b=Double.parseDouble(s3.pop()+"");
c=a+b;
s3.push(c+"");
}
if(s4.pop()=="-")
{
a=Double.parseDouble(s3.pop()+"");
b=Double.parseDouble(s3.pop()+"");
c=a+b;
s3.push(c+"");
}
}
m=s3.pop()+"";
textField2.setText(textField2.getText()+m);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -