import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public abstract class Calculator implements ActionListener{
   JFrame jf=new JFrame("计算器");   JTextField tf=new JTextField("0.0");   public void init()
        {           Container c=jf.getContentPane();
           //jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           tf.setHorizontalAlignment(JTextField.RIGHT);
           c.add(tf,"NORTH");
           JPanel pn1=new JPanel();
           c.add(pn1,"Center");           pn1.setLayout(new GridLayout(4,4));
           JButton bnt1=new JButton("1");
           bnt1.addActionListener(this);
           pn1.add(bnt1);           JButton bnt2=new JButton("2");
           bnt2.addActionListener(this);
           pn1.add(bnt2);           JButton bnt3=new JButton("3");
           bnt3.addActionListener(this);
           pn1.add(bnt3);           JButton bntadd=new JButton("+");
           bntadd.addActionListener(this);
           pn1.add(bntadd);           JButton bnt4=new JButton("4");
           bnt4.addActionListener(this);
           pn1.add(bnt4);           JButton bnt5=new JButton("5");
           bnt5.addActionListener(this);
           pn1.add(bnt5);           JButton bnt6=new JButton("6");
           bnt6.addActionListener(this);
           pn1.add(bnt6);           JButton bnt_=new JButton("-");
           bnt_.addActionListener(this);
           pn1.add(bnt_);           JButton bnt7=new JButton("7");
           bnt7.addActionListener(this);
           pn1.add(bnt7);           JButton bnt8=new JButton("8");
           bnt8.addActionListener(this);
           pn1.add(bnt8);           JButton bnt9=new JButton("9");
           bnt9.addActionListener(this);
           pn1.add(bnt9);           JButton bntce=new JButton("*");
           bntce.addActionListener(this);
           pn1.add(bntce);           JButton bnt0=new JButton("0");
           bnt0.addActionListener(this);
           pn1.add(bnt0);           JButton bnt10=new JButton(".");
           bnt10.addActionListener(this);
           pn1.add(bnt10);           JButton bnt11=new JButton("=");
           bnt11.addActionListener(this);
           pn1.add(bnt11);           JButton bnt12=new JButton("/");
           bnt12.addActionListener(this);
           pn1.add(bnt12);           jf.setSize(200,300);
           jf.setVisible(true);          }public void actionPerformed(ActionEvent e)
          {
          tf.setText(tf.getText()+e.getActionCommand());
          }
public static void main(String[] args)
      {
       new  Calculator().init();      }
}

解决方案 »

  1.   

    我改了一下,能出界面了,你看看!
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;class Calculator implements ActionListener{
       JFrame jf=new JFrame("计算器");   JTextField tf=new JTextField("0.0");   public void init()
            {           Container c=jf.getContentPane();
               //jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               tf.setHorizontalAlignment(JTextField.RIGHT);
               c.add(tf,"North");
               JPanel pn1=new JPanel();
               c.add(pn1,"Center");           pn1.setLayout(new GridLayout(4,4));
               JButton bnt1=new JButton("1");
               bnt1.addActionListener(this);
               pn1.add(bnt1);           JButton bnt2=new JButton("2");
               bnt2.addActionListener(this);
               pn1.add(bnt2);           JButton bnt3=new JButton("3");
               bnt3.addActionListener(this);
               pn1.add(bnt3);           JButton bntadd=new JButton("+");
               bntadd.addActionListener(this);
               pn1.add(bntadd);           JButton bnt4=new JButton("4");
               bnt4.addActionListener(this);
               pn1.add(bnt4);           JButton bnt5=new JButton("5");
               bnt5.addActionListener(this);
               pn1.add(bnt5);           JButton bnt6=new JButton("6");
               bnt6.addActionListener(this);
               pn1.add(bnt6);           JButton bnt_=new JButton("-");
               bnt_.addActionListener(this);
               pn1.add(bnt_);           JButton bnt7=new JButton("7");
               bnt7.addActionListener(this);
               pn1.add(bnt7);           JButton bnt8=new JButton("8");
               bnt8.addActionListener(this);
               pn1.add(bnt8);           JButton bnt9=new JButton("9");
               bnt9.addActionListener(this);
               pn1.add(bnt9);           JButton bntce=new JButton("*");
               bntce.addActionListener(this);
               pn1.add(bntce);           JButton bnt0=new JButton("0");
               bnt0.addActionListener(this);
               pn1.add(bnt0);           JButton bnt10=new JButton(".");
               bnt10.addActionListener(this);
               pn1.add(bnt10);           JButton bnt11=new JButton("=");
               bnt11.addActionListener(this);
               pn1.add(bnt11);           JButton bnt12=new JButton("/");
               bnt12.addActionListener(this);
               pn1.add(bnt12);           jf.setSize(200,300);
               jf.setVisible(true);          }public void actionPerformed(ActionEvent e)
              {
              tf.setText(tf.getText()+e.getActionCommand());
              }
    public static void main(String[] args)
          {
           new  Calculator().init();      }
    }