主题:一个只实现整数的计算器,求高手帮我看下哪里错了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame{
JButton jbt1=new JButton("1");
JButton jbt2=new JButton("2");
JButton jbt3=new JButton("3");
JButton jbt4=new JButton("4");
JButton jbt5=new JButton("5");
JButton jbt6=new JButton("6");
JButton jbt7=new JButton("7");
JButton jbt8=new JButton("8");
JButton jbt9=new JButton("9");
JButton jbt0=new JButton("0");
JButton jbtj=new JButton("+");
JButton jbti=new JButton("-");
JButton jbtc=new JButton("*");
JButton jbtu=new JButton("/");
JButton jbtd=new JButton("=");
JTextField jtf=new JTextField();
JButton jbtq=new JButton("esc");
int a;
int result;
String c;
CommanddActionListener commandlistener=new CommanddActionListener();
CalculatorActionListener calculatorActionListener=new CalculatorActionListener();
LastcommandActionListener LastcommandActionListener=new LastcommandActionListener();
public static void main(String args[])
{
Calculator c=new Calculator();
c.setSize(400,300);
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLocationRelativeTo(null);
c.setTitle("计算器");
}
public Calculator()
{
Container container=getContentPane();
container.setLayout(new BorderLayout());
JPanel p1=new JPanel(new GridLayout(5,3));
p1.add(jbt1);
p1.add(jbt2);
p1.add(jbt3);
p1.add(jbt4);
p1.add(jbt5);
p1.add(jbt6);
p1.add(jbt7);
p1.add(jbt8);
p1.add(jbt9);
p1.add(jbt0);
p1.add(jbtj);
p1.add(jbti);
p1.add(jbtc);
p1.add(jbtu);
p1.add(jbtd);
container.add(jtf,BorderLayout.NORTH);
container.add(p1,BorderLayout.CENTER);
container.add(jbtq,BorderLayout.WEST);
jbt1.addActionListener(commandlistener);
jbt2.addActionListener(commandlistener);
jbt3.addActionListener(commandlistener);
jbt4.addActionListener(commandlistener);
jbt5.addActionListener(commandlistener);
jbt6.addActionListener(commandlistener);
jbt7.addActionListener(commandlistener);
jbt8.addActionListener(commandlistener);
jbt9.addActionListener(commandlistener);
jbt0.addActionListener(commandlistener);
jbtj.addActionListener(calculatorActionListener);
jbti.addActionListener(calculatorActionListener);
jbtc.addActionListener(calculatorActionListener);
jbtu.addActionListener(calculatorActionListener);
jbtd.addActionListener(LastcommandActionListener);
}
class CommanddActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(jtf.getText()!=null)
{
String input=e.getActionCommand();
a=Integer.parseInt(input);
jtf.setText(String.valueOf(a));
}
else
{
String input=e.getActionCommand();
result=Integer.parseInt(input);
jtf.setText(String.valueOf(result));
}
}
}
class CalculatorActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
c=e.getActionCommand();
jtf.setText(c);
}
}
class LastcommandActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(c=="+")
result+=a;
if(c=="-");
result-=a;
if(c=="*")
result*=a;
if(c=="/")
result/=a;
jtf.setText(String.valueOf(result));
}
}
}
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JFrame{
JButton jbt1=new JButton("1");
JButton jbt2=new JButton("2");
JButton jbt3=new JButton("3");
JButton jbt4=new JButton("4");
JButton jbt5=new JButton("5");
JButton jbt6=new JButton("6");
JButton jbt7=new JButton("7");
JButton jbt8=new JButton("8");
JButton jbt9=new JButton("9");
JButton jbt0=new JButton("0");
JButton jbtj=new JButton("+");
JButton jbti=new JButton("-");
JButton jbtc=new JButton("*");
JButton jbtu=new JButton("/");
JButton jbtd=new JButton("=");
JTextField jtf=new JTextField();
JButton jbtq=new JButton("esc");
int a;
int result;
String c;
CommanddActionListener commandlistener=new CommanddActionListener();
CalculatorActionListener calculatorActionListener=new CalculatorActionListener();
LastcommandActionListener LastcommandActionListener=new LastcommandActionListener();
public static void main(String args[])
{
Calculator c=new Calculator();
c.setSize(400,300);
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLocationRelativeTo(null);
c.setTitle("计算器");
}
public Calculator()
{
Container container=getContentPane();
container.setLayout(new BorderLayout());
JPanel p1=new JPanel(new GridLayout(5,3));
p1.add(jbt1);
p1.add(jbt2);
p1.add(jbt3);
p1.add(jbt4);
p1.add(jbt5);
p1.add(jbt6);
p1.add(jbt7);
p1.add(jbt8);
p1.add(jbt9);
p1.add(jbt0);
p1.add(jbtj);
p1.add(jbti);
p1.add(jbtc);
p1.add(jbtu);
p1.add(jbtd);
container.add(jtf,BorderLayout.NORTH);
container.add(p1,BorderLayout.CENTER);
container.add(jbtq,BorderLayout.WEST);
jbt1.addActionListener(commandlistener);
jbt2.addActionListener(commandlistener);
jbt3.addActionListener(commandlistener);
jbt4.addActionListener(commandlistener);
jbt5.addActionListener(commandlistener);
jbt6.addActionListener(commandlistener);
jbt7.addActionListener(commandlistener);
jbt8.addActionListener(commandlistener);
jbt9.addActionListener(commandlistener);
jbt0.addActionListener(commandlistener);
jbtj.addActionListener(calculatorActionListener);
jbti.addActionListener(calculatorActionListener);
jbtc.addActionListener(calculatorActionListener);
jbtu.addActionListener(calculatorActionListener);
jbtd.addActionListener(LastcommandActionListener);
}
class CommanddActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(jtf.getText()!=null)
{
String input=e.getActionCommand();
a=Integer.parseInt(input);
jtf.setText(String.valueOf(a));
}
else
{
String input=e.getActionCommand();
result=Integer.parseInt(input);
jtf.setText(String.valueOf(result));
}
}
}
class CalculatorActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
c=e.getActionCommand();
jtf.setText(c);
}
}
class LastcommandActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(c=="+")
result+=a;
if(c=="-");
result-=a;
if(c=="*")
result*=a;
if(c=="/")
result/=a;
jtf.setText(String.valueOf(result));
}
}
}