📄 java_4.java
字号:
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculator extends JApplet implements ActionListener
{
Frame frm;
JTextField txt;
JMenuItem itmcpy;
JMenuItem itmpas;
JMenuItem itmabout;
JMenuItem itmart;
int yuna;
String tf;
String g;
int key = 0;
double yun=0;
public void init()
{
frm=new Frame("计算器");
frm.setLayout(null);
frm.setSize(265,248);
frm.setBackground(Color.LIGHT_GRAY);
JMenuBar menu=new JMenuBar();
menu.setSize(265, 20);
menu.setLocation(0, 30);
frm.add(menu);
JMenu fileMenu=new JMenu("编辑");
JMenu filehelp=new JMenu("帮助");
menu.add(fileMenu);
menu.add(filehelp);
itmcpy=new JMenuItem("复制");
itmcpy.addActionListener(this);
itmpas=new JMenuItem("粘贴");
itmpas.addActionListener(this);
fileMenu.add(itmcpy);
fileMenu.add(itmpas);
itmart=new JMenuItem("帮助主题");
itmart.addActionListener(this);
itmabout=new JMenuItem("关于");
itmabout.addActionListener(this);
filehelp.add(itmart);
filehelp.add(itmabout);
txt=new JTextField(20);
txt.setSize(252, 21);
txt.setLocation(6, 52);
txt.setHorizontalAlignment(txt.RIGHT);
frm.add(txt);
txt.addActionListener(this);
JButton btnc=new JButton("C");
btnc.setSize(58,28);
btnc.setLocation(193,76);
frm.add(btnc);
btnc.addActionListener(this);
JButton btnce=new JButton("CE");
btnce.setSize(65,28);
btnce.setLocation(123,76);
frm.add(btnce);
btnce.addActionListener(this);
JButton btnesc=new JButton("Backspace");
btnesc.setSize(106,28);
btnesc.setLocation(12,76);
frm.add(btnesc);
btnesc.addActionListener(this);
JButton btnsqr=new JButton("sqrt");
btnsqr.setSize(60,28);
btnsqr.setLocation(190,110);
frm.add(btnsqr);
btnsqr.addActionListener(this);
JButton btnm=new JButton("/");
btnm.setSize(38,28);
btnm.setLocation(149,110);
frm.add(btnm);
btnm.addActionListener(this);
JButton btn9=new JButton("9");
btn9.setSize(42,28);
btn9.setLocation(105,110);
frm.add(btn9);
btn9.addActionListener(this);
JButton btn8=new JButton("8");
btn8.setSize(42,28);
btn8.setLocation(60,110);
frm.add(btn8);
btn8.addActionListener(this);
JButton btn7=new JButton("7");
btn7.setSize(42,28);
btn7.setLocation(14,110);
frm.add(btn7);
btn7.addActionListener(this);
JButton btn4=new JButton("4");
btn4.setSize(42,28);
btn4.setLocation(14,140);
frm.add(btn4);
btn4.addActionListener(this);
JButton btn5=new JButton("5");
btn5.setSize(42,28);
btn5.setLocation(60,140);
frm.add(btn5);
btn5.addActionListener(this);
JButton btn6=new JButton("6");
btn6.setSize(42,28);
btn6.setLocation(105,140);
frm.add(btn6);
btn6.addActionListener(this);
JButton btnn=new JButton("*");
btnn.setSize(39,28);
btnn.setLocation(150,140);
frm.add(btnn);
btnn.addActionListener(this);
JButton btnl=new JButton("%");
btnl.setSize(58,28);
btnl.setLocation(192,140);
frm.add(btnl);
btnl.addActionListener(this);
JButton btnk=new JButton("1/x");
btnk.setSize(58,28);
btnk.setLocation(192,170);
frm.add(btnk);
btnk.addActionListener(this);
JButton btnv=new JButton("-");
btnv.setSize(39,28);
btnv.setLocation(150,170);
frm.add(btnv);
btnv.addActionListener(this);
JButton btn3=new JButton("3");
btn3.setSize(42,28);
btn3.setLocation(105,170);
frm.add(btn3);
btn3.addActionListener(this);
JButton btn2=new JButton("2");
btn2.setSize(42,28);
btn2.setLocation(60,170);
frm.add(btn2);
btn2.addActionListener(this);
JButton btn1=new JButton("1");
btn1.setSize(42,28);
btn1.setLocation(14,170);
frm.add(btn1);
btn1.addActionListener(this);
JButton btn0=new JButton("0");
btn0.setSize(42,28);
btn0.setLocation(14,200);
frm.add(btn0);
btn0.addActionListener(this);
JButton btnq=new JButton("+/-");
btnq.setSize(50,28);
btnq.setLocation(60,200);
frm.add(btnq);
btnq.addActionListener(this);
JButton btna=new JButton(".");
btna.setSize(40,28);
btna.setLocation(112,200);
frm.add(btna);
btna.addActionListener(this);
JButton btnb=new JButton("+");
btnb.setSize(42,28);
btnb.setLocation(155,200);
frm.add(btnb);
btnb.addActionListener(this);
JButton btne=new JButton("=");
btne.setSize(50,28);
btne.setLocation(200,200);
frm.add(btne);
btne.addActionListener(this);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int i;
String s = e.getActionCommand();
tf=txt.getText().trim();
int l = txt.getText().trim().length();
if(e.getSource()==itmcpy)
{
txt.copy();
}
else if(e.getSource() == itmpas)
{
txt.setText("");
txt.paste();
}
else if(e.getSource() == itmabout)
{
txt.setText("" + "0400310506 陈旭");
}
else if(e.getSource() ==itmart)
{
txt.setText("" + "仿Windoos XP下的计算器");
}
if(s.equals("."))
{
for (int o = 0; o < l; o++)
{
if ('.' == tf.charAt(o))
{
txt.setText("已经输入'.'不能重复输入");
}
else
{
txt.setText(txt.getText() + '.');
}
}
}
for(i=0;i<=9;i++)
if(s.equals("" + i))
{
if(g!="")
{
txt.setText("");
txt.setText("" + e.getActionCommand());
g = "";
}
else txt.setText("" + txt.getText() + e.getActionCommand());
}
if(s.equals("Backspace"))
{
yuna = txt.getText().trim().length();
txt.setText(txt.getText().trim().substring(0,yuna - 1));
}
else if(s.equals("CE"))
{
txt.setText("0");
}
else if(s.equals("C"))
{
txt.setText("0");
yun = 0;
key = 0;
}
else if(s.equals("+"))
{
yun = Double.parseDouble(txt.getText().trim());
key = 1;
g = "+";
}
else if(s.equals("-"))
{
yun = Double.parseDouble(txt.getText().trim());
key = 2;
g = "-";
}
else if(s.equals("*"))
{
yun = Double.parseDouble(txt.getText().trim());
key = 3;
g = "*";
}
else if(s.equals("/"))
{
yun = Double.parseDouble(txt.getText().trim());
key = 4;
g = "/";
}
else if(s.equals("%"))
{
yun = (Double.parseDouble(txt.getText().trim())) / 100;
g = "%";
txt.setText("" +yun);
}
else if(s.equals("1/x"))
{
if(Double.parseDouble(txt.getText().trim())!=0)
{
yun = 1 / (Double.parseDouble(txt.getText().trim()));
g = "1/x";
txt.setText("" + yun);
}
else txt.setText("" + "0不能求倒数");
}
else if(s.equals("+/-"))
{
yun = - (Double.parseDouble(txt.getText().trim()));
g = "+/-";
txt.setText("" + yun);
}
else if(s.equals("sqrt"))
{
yun = Math.sqrt(Double.parseDouble(txt.getText().trim()));
g = "sqrt";
txt.setText("" + yun);
}
else if(s.equals("="))
{
switch(key)
{
case 1:
yun += Double.parseDouble(txt.getText().trim());
txt.setText("" + yun);
break;
case 2:
yun -= Double.parseDouble(txt.getText().trim());
txt.setText("" + yun);
break;
case 3:
yun *= Double.parseDouble(txt.getText().trim());
txt.setText("" + yun);
break;
case 4:
if(Double.parseDouble(txt.getText().trim()) == 0)
{
yun=0;
txt.setText("除数不能为空!");
}
else
{
yun /= Double.parseDouble(txt.getText().trim());
txt.setText("" + yun);
}
break;
}
g = "e";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -