题目就这样:
设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。设计的计算器应用程序可以完成加法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。
听了几节课了,我就看着书本的某些形式自己凑着写的,真的有好多不懂,写的很笨拙,上网查了蛮多相关程序的,有太多东西不认识。
我自己的代码如下,界面可以显示,不能进行运算:
import java.awt.*;
public class app7 extends Frame
{
    static Panel pan=new Panel();//创建面板
    static Label lab=new Label();//创建标签
    static Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bp,ba,bs,bm,bd,be,bl,bb,bq,bz;
    public static void main(String[] args)
    {
        app7 frm=new app7();
        b0=new Button("0");b0.setForeground(Color.blue);
        b1=new Button("1");b1.setForeground(Color.blue);
        b2=new Button("2");b2.setForeground(Color.blue);
        b3=new Button("3");b3.setForeground(Color.blue);
        b4=new Button("4");b4.setForeground(Color.blue);
        b5=new Button("5");b5.setForeground(Color.blue);
        b6=new Button("6");b6.setForeground(Color.blue);
        b7=new Button("7");b7.setForeground(Color.blue);
        b8=new Button("8");b8.setForeground(Color.blue);
        b9=new Button("9");b9.setForeground(Color.blue);
        bp=new Button(".");bp.setForeground(Color.red);
        ba=new Button("+");ba.setForeground(Color.red);
        bs=new Button("-");bs.setForeground(Color.red);
        bm=new Button("*");bm.setForeground(Color.red);
        bd=new Button("/");bd.setForeground(Color.red);
        be=new Button("=");be.setForeground(Color.red);
        bl=new Button("C");bl.setForeground(Color.red);
        bb=new Button("退格");bb.setForeground(Color.red);
        bq=new Button("1/x");bq.setForeground(Color.blue);
        bz=new Button("+/-");bz.setForeground(Color.blue);
        frm.setTitle("计算器");
        frm.setLayout(null);//取消窗口的页面设置
        frm.setSize(250,270);
        frm.setResizable(false);//设置窗口的大小不可改变
        GridLayout grid=new GridLayout(4,5);//创建4行5列的页面设置
        pan.setLayout(grid);//将面板对象pan的布局策略设为网格布局方式
        lab.setBounds(0,0,250,70);//设置标签的位置和大小
        pan.setBounds(0,70,250,200);//设置面板的位置和大小
        lab.setBackground(Color.white);//设置标签的颜色
        pan.add(b1);
        pan.add(b2);
        pan.add(b3);
        pan.add(ba);
        pan.add(bl);
        pan.add(b4);
        pan.add(b5);
        pan.add(b6);
        pan.add(bs);
        pan.add(bb);
        pan.add(b7);
        pan.add(b8);
        pan.add(b9);
        pan.add(bm);
        pan.add(bq);
        pan.add(b0);
        pan.add(bz);
        pan.add(bp);
        pan.add(bd);
        pan.add(be);
        frm.add(lab);//添加标签
        frm.add(pan);//添加面板
        frm.setVisible(true);
    }}
ActionEvent不知道怎么写、、、求真心解答

解决方案 »

  1.   

    你在程序后面加上这样一段
      b0.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    lab.setText(b0.getLabel());
    }
    });这样你按0这个键,就会显示0了。
    你写成一个完整的计算器,代码还要做大修改,这里我给你一个比较好的例子吧,不要浮躁,一点一点自己模仿,琢磨,就会了
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;/**
     * @version 1.33 2007-06-12
     * @author Cay Horstmann
     */
    public class Calculator
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(new Runnable()
             {
                public void run()
                {
                   CalculatorFrame frame = new CalculatorFrame();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setVisible(true);
                }
             });
       }
    }/**
     * A frame with a calculator panel.
     */
    class CalculatorFrame extends JFrame
    {
       public CalculatorFrame()
       {
          setTitle("Calculator");
          CalculatorPanel panel = new CalculatorPanel();
          add(panel);
          pack();
       }
    }/**
     * A panel with calculator buttons and a result display.
     */
    class CalculatorPanel extends JPanel
    {
       public CalculatorPanel()
       {
          setLayout(new BorderLayout());      result = 0;
          lastCommand = "=";
          start = true;      // add the display      display = new JButton("0");
          display.setEnabled(false);
          add(display, BorderLayout.NORTH);      ActionListener insert = new InsertAction();
          ActionListener command = new CommandAction();      // add the buttons in a 4 x 4 grid      panel = new JPanel();
          panel.setLayout(new GridLayout(4, 4));      addButton("7", insert);
          addButton("8", insert);
          addButton("9", insert);
          addButton("/", command);      addButton("4", insert);
          addButton("5", insert);
          addButton("6", insert);
          addButton("*", command);      addButton("1", insert);
          addButton("2", insert);
          addButton("3", insert);
          addButton("-", command);      addButton("0", insert);
          addButton(".", insert);
          addButton("=", command);
          addButton("+", command);      add(panel, BorderLayout.CENTER);
       }   /**
        * Adds a button to the center panel.
        * @param label the button label
        * @param listener the button listener
        */
       private void addButton(String label, ActionListener listener)
       {
          JButton button = new JButton(label);
          button.addActionListener(listener);
          panel.add(button);
       }   /**
        * This action inserts the button action string to the end of the display text.
        */
       private class InsertAction implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             String input = event.getActionCommand();
             if (start)
             {
                display.setText("");
                start = false;
             }
             display.setText(display.getText() + input);
          }
       }   /**
        * This action executes the command that the button action string denotes.
        */
       private class CommandAction implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             String command = event.getActionCommand();         if (start)
             {
                if (command.equals("-"))
                {
                   display.setText(command);
                   start = false;
                }
                else lastCommand = command;
             }
             else
             {
                calculate(Double.parseDouble(display.getText()));
                lastCommand = command;
                start = true;
             }
          }
       }   /**
        * Carries out the pending calculation.
        * @param x the value to be accumulated with the prior result.
        */
       public void calculate(double x)
       {
          if (lastCommand.equals("+")) result += x;
          else if (lastCommand.equals("-")) result -= x;
          else if (lastCommand.equals("*")) result *= x;
          else if (lastCommand.equals("/")) result /= x;
          else if (lastCommand.equals("=")) result = x;
          display.setText("" + result);
       }   private JButton display;
       private JPanel panel;
       private double result;
       private String lastCommand;
       private boolean start;
    }
      

  2.   

    刚学都这样的,J2SE都学完了,还是迷迷糊糊的,慢慢来