📄 jlab12.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JLab12 implements ActionListener{
JFrame f = new JFrame("计算器");
JPanel p0 = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
JPanel p8 = new JPanel();
JPanel p9 = new JPanel();
JPanel p10 = new JPanel();
JPanel p11 = new JPanel();
JTextField t = new JTextField("0.",32);
JLabel l= new JLabel(" ");
JButton b1 = new JButton("7");
JButton b2 = new JButton("8");
JButton b3 = new JButton("9");
JButton b4 = new JButton("/");
JButton b5 = new JButton("sqrt");
JButton b6 = new JButton("4");
JButton b7 = new JButton("5");
JButton b8 = new JButton("6");
JButton b9 = new JButton("*");
JButton b10 = new JButton("%");
JButton b11 = new JButton("1");
JButton b12 = new JButton("2");
JButton b13 = new JButton("3");
JButton b14 = new JButton("-");
JButton b15 = new JButton("1/x");
JButton b16 = new JButton("0");
JButton b17 = new JButton("+/-");
JButton b18 = new JButton(".");
JButton b19 = new JButton("+");
JButton b20 = new JButton("=");
JButton b21 = new JButton("Backspace");
JButton b22 = new JButton("CE");
JButton b23 = new JButton("C");
JButton b24 = new JButton("MC");
JButton b25 = new JButton("MR");
JButton b26 = new JButton("MS");
JButton b27 = new JButton("M+");
String str,str1,str2;//str、str1存放文本框内容,str2存放记忆
double x,y;//用于输入源操作数和目的操作数
int c=0,p=0;//c控制"="的有效性,p控制"."的有效性
int s=0,w=0;//s控制文本框内容,w控制错误状态
public JLab12(){
f.setBackground(Color.white);
t.setFocusable(false);
t.setHorizontalAlignment(JTextField.RIGHT);
l.setBackground(Color.white);
l.setHorizontalAlignment(JLabel.LEFT);
l.setVerticalAlignment(JLabel.EAST);
l.setHorizontalTextPosition(JLabel.CENTER);
l.setVerticalTextPosition(JLabel.CENTER);
l.setBorder(BorderFactory.createLoweredBevelBorder());
f.setLayout(new BorderLayout());
p0.setLayout(new BorderLayout());
p1.add(t);
p2.setLayout(new BorderLayout());
p5.setLayout(new BorderLayout());
p10.setLayout(new GridLayout(4,5,10,10));
p11.setLayout(new GridLayout(4,1,10,10));
b1.setForeground(Color.blue);
b1.setFocusable(false);
b2.setForeground(Color.blue);
b2.setFocusable(false);
b3.setForeground(Color.blue);
b3.setFocusable(false);
b4.setForeground(Color.red);
b4.setFocusable(false);
b5.setForeground(Color.blue);
b5.setFocusable(false);
b6.setForeground(Color.blue);
b6.setFocusable(false);
b7.setForeground(Color.blue);
b7.setFocusable(false);
b8.setForeground(Color.blue);
b8.setFocusable(false);
b9.setForeground(Color.red);
b9.setFocusable(false);
b10.setForeground(Color.blue);
b10.setFocusable(false);
b11.setForeground(Color.blue);
b11.setFocusable(false);
b12.setForeground(Color.blue);
b12.setFocusable(false);
b13.setForeground(Color.blue);
b13.setFocusable(false);
b14.setForeground(Color.red);
b14.setFocusable(false);
b15.setForeground(Color.blue);
b15.setFocusable(false);
b16.setForeground(Color.blue);
b16.setFocusable(false);
b17.setForeground(Color.blue);
b17.setFocusable(false);
b18.setForeground(Color.blue);
b18.setFocusable(false);
b19.setForeground(Color.red);
b19.setFocusable(false);
b20.setForeground(Color.red);
b20.setFocusable(false);
b21.setForeground(Color.red);
b21.setFocusable(false);
b22.setForeground(Color.red);
b22.setFocusable(false);
b23.setForeground(Color.red);
b23.setFocusable(false);
b24.setForeground(Color.red);
b24.setFocusable(false);
b25.setForeground(Color.red);
b25.setFocusable(false);
b26.setForeground(Color.red);
b26.setFocusable(false);
b27.setForeground(Color.red);
b27.setFocusable(false);
p10.add(b1);
p10.add(b2);
p10.add(b3);
p10.add(b4);
p10.add(b5);
p10.add(b6);
p10.add(b7);
p10.add(b8);
p10.add(b9);
p10.add(b10);
p10.add(b11);
p10.add(b12);
p10.add(b13);
p10.add(b14);
p10.add(b15);
p10.add(b16);
p10.add(b17);
p10.add(b18);
p10.add(b19);
p10.add(b20);
p11.add(b24);
p11.add(b25);
p11.add(b26);
p11.add(b27);
p3.add(b21);
p3.add(b22);
p3.add(b23);
p4.add(l);
p5.add(p4,"West");
p5.add(p3,"East");
p2.add(p10,"East");
p2.add(p11,"West");
p0.add(p1,"North");
p0.add(p2,"South");
p0.add(p5,"Center");
p0.add(p4,"West");
f.getContentPane().add(p0,"Center");
f.getContentPane().add(p6,"North");
f.getContentPane().add(p7,"East");
f.getContentPane().add(p8,"West");
f.getContentPane().add(p9,"South");
f.setSize(420,272);
f.setLocation(100,100);
f.setVisible(true);
f.setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
b20.addActionListener(this);
b21.addActionListener(this);
b22.addActionListener(this);
b23.addActionListener(this);
b24.addActionListener(this);
b25.addActionListener(this);
b26.addActionListener(this);
b27.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(w==0){
if((e.getActionCommand()).equals(".")){
if(p==0){
if(s==0){
t.setText("0.");
s=1;
}
else t.setText(t.getText()+".");
}
p=1;
}
else if((e.getActionCommand()).equals("CE")){
t.setText("0.");
w=0;
}
else if((e.getActionCommand()).equals("C")){
t.setText("0.");
w=0;
}
else if((e.getActionCommand()).equals("Backspace")){
str=t.getText();
if((str.length()>1)&&!(str.equals("0."))){
s=1;
str=str.substring(0,str.length()-1);
t.setText(str);
}
else{
s=0;
t.setText("0.");
}
}
else if((e.getActionCommand()).equals("MS")){
str2=t.getText();
l.setText(" M ");
}
else if((e.getActionCommand()).equals("MC")){
str2=null;
l.setText(" ");
}
else if((e.getActionCommand()).equals("MR")){
if(str2!=null) t.setText(str2);
s=0;
}
else if((e.getActionCommand()).equals("M+")){
if(!(t.getText().equals("0."))){
x=Double.valueOf(str2);
y=Double.valueOf(t.getText());
x+=y;
str2=Double.toString(y);
}
else{
x=Double.valueOf(str2);
x*=2;
str2=Double.toString(x);
}
}
else if((e.getActionCommand()).equals("+/-")){
x = Double.parseDouble(t.getText());
s=0;
str1=Double.toString((-x));
t.setText(str1);
}
else if((e.getActionCommand()).equals("1/x")){
x=Double.parseDouble(t.getText());
s=0;
if(x==0){
t.setText("Error");
w=1;
}
else{
str1=Double.toString((Math.pow(x,-1)));
t.setText(str1);
}
}
else if((e.getActionCommand()).equals("sqrt")){
x=Double.parseDouble(t.getText());
s=0;
if(x<0){
t.setText("Error");
w=1;
}
else{
str1=Double.toString((Math.sqrt(x)));
t.setText(str1);
}
}
else if((e.getActionCommand()).equals("%")){
x=Double.parseDouble(t.getText());
s=0;
c=1;
p=0;
str1=e.getActionCommand();
}
else if((e.getActionCommand()).equals("+")){
x=Double.parseDouble(t.getText());
s=0;
c=1;
p=0;
str1=e.getActionCommand();
}
else if((e.getActionCommand()).equals("-")){
x=Double.parseDouble(t.getText());
s=0;
c=1;
p=0;
str1=e.getActionCommand();
}
else if((e.getActionCommand()).equals("*")){
x=Double.parseDouble(t.getText());
s=0;
c=1;
p=0;
str1=e.getActionCommand();
}
else if((e.getActionCommand()).equals("/")){
x=Double.parseDouble(t.getText());
s=0;
c=1;
p=0;
str1=e.getActionCommand();
}
else if((e.getActionCommand()).equals("=")){
if(c==1){
if(str1.equals("%")){
y=Double.parseDouble(t.getText());
str1=Double.toString(x%y);
t.setText(str1);
s=0;
}
if(str1.equals("+")){
y=Double.parseDouble(t.getText());
str1=Double.toString(x+y);
t.setText(str1);
s=0;
}
if(str1.equals("-")){
y=Double.parseDouble(t.getText());
str1=Double.toString(x-y);
t.setText(str1);
s=0;
}
if(str1.equals("*")){
y=Double.parseDouble(t.getText());
str1=Double.toString(x*y);
t.setText(str1);
s=0;
}
if(str1.equals("/")){
y=Double.parseDouble(t.getText());
if(y==0){
t.setText("Error");
w=1;
}
else{
str1=Double.toString(x/y);
t.setText(str1);
}
s=0;
}
c=0;
}
}
else{
str=e.getActionCommand();
if(s==0){
s=1;
t.setText(str);
}
else t.setText(t.getText()+str);
}
}
else if((e.getActionCommand()).equals("CE")){
t.setText("0.");
w=0;
}
}
public static void main(String[] args){
new JLab12();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -