我使用了这段代码,但在如何使用运算符的最基本步骤中遇到了问题。此外,我什至遇到超过 1 位数的问题。如果你请把遗漏的陈述加起来,这对我有帮助。在给定的代码中,我删除了那些在actionPerformed
功能中产生问题的步骤
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class calculator1 implements ActionListener
{
private JFrame f;
private JButton a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
JTextField tf;
String msg="";
public calculator1()
{
f=new JFrame("Calculator");
f.setLayout(null);
a=new JButton("1");
a.setActionCommand("1");
a1=new JButton("2");
a1.setActionCommand("2");
a2=new JButton("3");
a2.setActionCommand("3");
a3=new JButton("4");
a3.setActionCommand("4");
a4=new JButton("5");
a4.setActionCommand("5");
a5=new JButton("6");
a5.setActionCommand("6");
a6=new JButton("7");
a6.setActionCommand("7");
a7=new JButton("8");
a7.setActionCommand("8");
a8=new JButton("9");
a8.setActionCommand("9");
a9=new JButton("0");
a9.setActionCommand("0");
a10=new JButton("+");
a10.setActionCommand("+");
a11=new JButton("-");
a11.setActionCommand("-");
a12=new JButton("*");
a12.setActionCommand("*");
a13=new JButton("/");
a13.setActionCommand("/");
a14=new JButton("=");
a14.setActionCommand("=");
a15=new JButton("00");
a15.setActionCommand("00");
tf= new JTextField(30);
}
public void launchframe()
{
f.setSize(500,600);
a.setBounds(100,200,50,50);
a.addActionListener(this);
a1.setBounds(160,200,50,50);
a1.addActionListener(this);
a2.setBounds(220,200,50,50);
a2.addActionListener(this);
a3.setBounds(100,300,50,50);
a3.addActionListener(this);
a4.setBounds(160,300,50,50);
a4.addActionListener(this);
a5.setBounds(220,300,50,50);
a5.addActionListener(this);
a6.setBounds(100,400,50,50);
a6.addActionListener(this);
a7.setBounds(160,400,50,50);
a7.addActionListener(this);
a8.setBounds(220,400,50,50);
a8.addActionListener(this);
a9.setBounds(100,500,50,50);
a9.addActionListener(this);
a10.setBounds(300,200,50,50);
a10.addActionListener(this);
a11.setBounds(300,300,50,50);
a11.addActionListener(this);
a12.setBounds(300,400,50,50);
a12.addActionListener(this);
a13.setBounds(300,500,50,50);
a13.addActionListener(this);
a14.setBounds(160,500,50,50);
a14.addActionListener(this);
a15.setBounds(220,500,50,50);
a15.addActionListener(this);
f.add(a);
f.add(a1);
f.add(a2);
f.add(a3);
f.add(a4);
f.add(a5);
f.add(a6);
f.add(a7);
f.add(a8);
f.add(a9);
f.add(a10);
f.add(a11);
f.add(a12);
f.add(a13);
f.add(a14);
f.add(a15);
tf.setBounds(100,150,250,30);
f.add(tf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
tf.setText(s);
}
public static void main(String[]arg)
{
calculator1 c1=new calculator1();
c1.launchframe();
}
}