Java计算器
发布网友
发布时间:2022-04-22 07:01
我来回答
共3个回答
热心网友
时间:2022-06-17 03:12
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JiSuan implements ActionListener{
static JTextField txt;
static JTextField txt1;
static double yi,jieg;
static int fh = 2;
public static void main(String agrs[])
{
JFrame f = new JFrame("计算机");
f.setSize(380, 320);
f.setLocation(300,300);
//f.setBackground(Color.red);
//Container c = f.getContentPane(); //内容面板
JPanel p = 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();
//p.setBackground(Color.blue);//面板颜色
//p1.setBackground(Color.black);
//p2.setBackground(Color.yellow);
//p3.setBackground(Color.green);
//p4.setBackground(Color.cyan);
txt = new JTextField(24);//文本框
txt.setHorizontalAlignment(JTextField.RIGHT);
txt1 = new JTextField(3);
txt1.setEditable(false);
txt1.setEnabled(false);
JButton bt = new JButton("MC");//按钮
JButton bt1 = new JButton("MR");
JButton bt2 = new JButton("MS");
JButton bt3 = new JButton("M+");
JButton bt4 = new JButton("Backspace");
JButton bt5 = new JButton("CE");
JButton bt6 = new JButton("C");
JButton bt7 = new JButton("7");
JButton bt8 = new JButton("8");
JButton bt9 = new JButton("9");
JButton bt0 = new JButton("/");
JButton bt11 = new JButton("sqrt");
JButton bt12 = new JButton("4");
JButton bt13 = new JButton("5");
JButton bt14 = new JButton("6");
JButton bt15 = new JButton("*");
JButton bt16 = new JButton("%");
JButton bt17 = new JButton("1");
JButton bt18 = new JButton("2");
JButton bt19 = new JButton("3");
JButton bt10 = new JButton("-");
JButton bt21 = new JButton("1/x");
JButton bt22 = new JButton("0");
JButton bt23 = new JButton("+/-");
JButton bt24 = new JButton(".");
JButton bt25 = new JButton("+");
JButton bt26 = new JButton("=");
bt.addActionListener(new JiSuan());bt1.addActionListener(new JiSuan());
bt2.addActionListener(new JiSuan());bt3.addActionListener(new JiSuan());
bt4.addActionListener(new JiSuan());bt5.addActionListener(new JiSuan());
bt6.addActionListener(new JiSuan());bt7.addActionListener(new JiSuan());
bt8.addActionListener(new JiSuan());bt9.addActionListener(new JiSuan());
bt0.addActionListener(new JiSuan());bt11.addActionListener(new JiSuan());
bt12.addActionListener(new JiSuan());bt13.addActionListener(new JiSuan());
bt14.addActionListener(new JiSuan());bt15.addActionListener(new JiSuan());
bt16.addActionListener(new JiSuan());bt17.addActionListener(new JiSuan());
bt18.addActionListener(new JiSuan());bt19.addActionListener(new JiSuan());
bt10.addActionListener(new JiSuan());bt21.addActionListener(new JiSuan());
bt22.addActionListener(new JiSuan());bt23.addActionListener(new JiSuan());
bt24.addActionListener(new JiSuan());bt25.addActionListener(new JiSuan());
bt26.addActionListener(new JiSuan());
f.setLayout(new BorderLayout(12,2));//边界布局
f.add(p,BorderLayout.NORTH);
f.add(p1,BorderLayout.WEST);
f.add(p2);
p.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(5,1,10,10));
p2.add(p3);
p2.add(p4);
p2.add(p5);
p2.add(p6);
p2.add(p7);
p3.setLayout(new GridLayout(1,3,10,10));
p3.add(bt4);
p3.add(bt5);
p3.add(bt6);
p4.setLayout(new GridLayout(1,5,10,10));
p4.add(bt7);
p4.add(bt8);
p4.add(bt9);
p4.add(bt0);
p4.add(bt11);
p5.setLayout(new GridLayout(1,5,10,10));
p5.add(bt12);
p5.add(bt13);
p5.add(bt14);
p5.add(bt15);
p5.add(bt16);
p6.setLayout(new GridLayout(1,5,10,10));
p6.add(bt17);
p6.add(bt18);
p6.add(bt19);
p6.add(bt10);
p6.add(bt21);
p7.setLayout(new GridLayout(1,5,10,10));
p7.add(bt22);
p7.add(bt23);
p7.add(bt24);
p7.add(bt25);
p7.add(bt26);
p.add(txt);
p1.setLayout(new GridLayout(5,1,10,10));//网格布局
p1.add(txt1);
p1.add(bt);
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
f.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("1"))
{
if(txt.getText().equals("0"))
txt.setText("1");
else
txt.setText(txt.getText()+"1");
}
if(arg0.getActionCommand().equals("2"))
{
if(txt.getText().equals("0"))
txt.setText("2");
else
txt.setText(txt.getText()+"2");
}
if(arg0.getActionCommand().equals("3"))
{
if(txt.getText().equals("0"))
txt.setText("3");
else
txt.setText(txt.getText()+"3");
}
if(arg0.getActionCommand().equals("4"))
{
if(txt.getText().equals("0"))
txt.setText("4");
else
txt.setText(txt.getText()+"4");
}
if(arg0.getActionCommand().equals("5"))
{
if(txt.getText().equals("0"))
txt.setText("5");
else
txt.setText(txt.getText()+"5");
}
if(arg0.getActionCommand().equals("6"))
{
if(txt.getText().equals("0"))
txt.setText("6");
else
txt.setText(txt.getText()+"6");
}
if(arg0.getActionCommand().equals("7"))
{
if(txt.getText().equals("0"))
txt.setText("7");
else
txt.setText(txt.getText()+"7");
}
if(arg0.getActionCommand().equals("8"))
{
if(txt.getText().equals("0"))
txt.setText("8");
else
txt.setText(txt.getText()+"8");
}
if(arg0.getActionCommand().equals("9"))
{
if(txt.getText().equals("0"))
txt.setText("9");
else
txt.setText(txt.getText()+"9");
}
if(arg0.getActionCommand().equals("0"))
{
if(txt.getText().equals("0"))
txt.setText("0");
else
txt.setText(txt.getText()+"0");
}
if(arg0.getActionCommand().equals("."))
{
if(txt.getText().indexOf(".") != -1)
txt.setText(txt.getText());
else
txt.setText(txt.getText()+".");
}
if(arg0.getActionCommand().equals("MC"))
{}
if(arg0.getActionCommand().equals("MR"))
{}
if(arg0.getActionCommand().equals("MS"))
{}
if(arg0.getActionCommand().equals("M+"))
{}
if(arg0.getActionCommand().equals("+/-"))
{}
if(arg0.getActionCommand().equals("Backspace"))//退格
{
int i = txt.getText().length();
txt.setText(txt.getText().substring(0,i-1));
}
if(arg0.getActionCommand().equals("CE"))
{}
if(arg0.getActionCommand().equals("C"))//清空
{
txt.setText("");
yi = 0;fh = 0;
}
if(arg0.getActionCommand().equals("/"))
{
yi = Double.valueOf(txt.getText());
fh = 3;
txt.setText("");
}
if(arg0.getActionCommand().equals("*"))
{
yi = Double.valueOf(txt.getText());
fh = 2;
txt.setText("");
}
if(arg0.getActionCommand().equals("+"))
{
yi = Double.valueOf(txt.getText());
fh = 0;
txt.setText("");
}
if(arg0.getActionCommand().equals("-"))
{
yi = Double.valueOf(txt.getText());
fh = 1;
txt.setText("");
}
if(arg0.getActionCommand().equals("sqrt"))//平方根
{
yi = Double.valueOf(txt.getText());
double jie = Math.sqrt(yi);
txt.setText(String.valueOf(jie));
}
if(arg0.getActionCommand().equals("%"))
{}
if(arg0.getActionCommand().equals("1/x"))
{
double dou = Double.valueOf(txt.getText());
txt.setText(String.valueOf(1.0/dou));
}
if(arg0.getActionCommand().equals("="))
{
double er = Double.valueOf(txt.getText());
switch(fh)
{
case 0: jieg = (yi + er);break;
case 1: jieg = (yi - er);break;
case 2: jieg = (yi * er);break;
case 3: jieg = (yi / er);break;
}
txt.setText(String.valueOf(jieg));
}
}}
热心网友
时间:2022-06-17 03:12
有两处错误了 都属于局部变量覆盖了成员变量
{JTextField out=new JTextField(5); //此处的JTextField 去掉
out.setHorizontalAlignment(JTextField.RIGHT);
out.setBounds(10,10,340,40);
out.setEditable(false);
out.setText("0");
add(out);
}
{JButton b1=new JButton("1"); //此处的JButton 去掉
b1.setBounds(10,60,60,30);
b1.addActionListener(this);
add(b1);
}
去掉的那两处都是重新定义了一个同名变量
热心网友
时间:2022-06-17 03:12
你没有给Button"1"加入监听,所以按Button“1”不能触发事件,输出1。