public class NewClass1 extends JPanel { //计算器界面类    private JLabel display; //显示结果
    private JPanel panel;
    private double result;
    private String lastCommand;
    private boolean start;
   JPanel[] JButtonPanel=new JPanel[16];
    private int count=0;
    public NewClass1() {
        setLayout(new BorderLayout());        result = 0;
        lastCommand = "=";
        start = true;        display = new JLabel(  "计算结果:                                                                                                     0 " , SwingConstants.RIGHT    );
        display.setForeground(Color.black);    //设置前景颜色        display.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.black),
                BorderFactory.createEmptyBorder(6, 6, 6, 6)));        add(display, BorderLayout.NORTH);
        ActionListener insert = new InsertAction();
        ActionListener command = new CommandAction();
    
       
        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4)); //计算器按钮        addButton("1", insert);
        addButton("2", insert);
        addButton("3", insert);
        addButton("4", insert);
        addButton("5", insert);
        addButton("6", insert);
        addButton("7", insert);
        addButton("8", insert);
        addButton("9", insert);
        addButton("/", command);
        addButton("*", command);
        addButton("-", command);
        addButton("0", insert);
        addButton(".", insert);
        addButton("=", command);
        addButton("+", command);
        add(panel, BorderLayout.CENTER);    }    private void addButton(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
         button.setSize(25, 20);
         button.setLayout(null);
        //JButtonPanel=new JPanel[count];
         JButtonPanel[count].add(button);
        
         panel.add(JButtonPanel[count]);
          count++;
       
     
    }    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);
        }
    }    private class CommandAction implements ActionListener { //点击按钮监听器类        public void actionPerformed(ActionEvent evt) {
            String command = evt.getActionCommand();            if (start) {
               lastCommand = command;
            }
            else {
                calculate(Double.parseDouble(display.getText()));
                lastCommand = command;
                start = true;
            }
        }
    }    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);
    }
}ublic class NewClass extends JFrame {//顶层容器类    public NewClass() {
        setTitle("计算器");
        Container contentPane = getContentPane();
        NewClass1 panel = new NewClass1();
        contentPane.add(panel);
        setSize(400,400);
        setVisible(true);
        setResizable(false);
    }
}
public class Main {    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
   
    
        NewClass frame = new NewClass();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    
    }
}

解决方案 »

  1.   

    你不给加注释,我也不给你加。你自己看看改了什么地方。只有一点我要说一下,文件一定要保存成Main.java你自己跑跑看,我改完反正能跑起来。import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.BorderFactory;
    import java.awt.event.*;
    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Container;class NewClass1 extends JPanel {  //计算器界面类  private JLabel display;  //显示结果
      private JPanel panel;
      private double result;
      private String lastCommand;
      private boolean start;
      JPanel[] jButtonPanel=new JPanel[16];
      private int count=0;
      public NewClass1() {
      setLayout(new BorderLayout());  result = 0;
      lastCommand = "=";
      start = true;  display = new JLabel("计算结果: 0", JLabel.RIGHT );
      display.setForeground(Color.black); //设置前景颜色  display.setBorder(BorderFactory.createCompoundBorder(
      BorderFactory.createLineBorder(Color.black),
      BorderFactory.createEmptyBorder(6, 6, 6, 6)));  add(display, BorderLayout.NORTH);
      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();
        
        
      panel = new JPanel();
      panel.setLayout(new GridLayout(4, 4)); //计算器按钮  addButton("1", insert);
      addButton("2", insert);
      addButton("3", insert);
      addButton("4", insert);
      addButton("5", insert);
      addButton("6", insert);
      addButton("7", insert);
      addButton("8", insert);
      addButton("9", insert);
      addButton("/", command);
      addButton("*", command);
      addButton("-", command);
      addButton("0", insert);
      addButton(".", insert);
      addButton("=", command);
      addButton("+", command);
      add(panel, BorderLayout.CENTER);  }  private void addButton(String label, ActionListener listener) {
      JButton button = new JButton(label);
      button.addActionListener(listener);
      button.setSize(25, 20);
      button.setLayout(null);
      //JButtonPanel=new JPanel[count];
      jButtonPanel[count] = new JPanel();
      jButtonPanel[count].add(button);
        
      panel.add(jButtonPanel[count]);
      count++;
        
        
      }  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);
      }
      }  private class CommandAction implements ActionListener { //点击按钮监听器类  public void actionPerformed(ActionEvent evt) {
      String command = evt.getActionCommand();  if (start) {
      lastCommand = command;
      }
      else {
      calculate(Double.parseDouble(display.getText()));
      lastCommand = command;
      start = true;
      }
      }
      }  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);
      }
    }class NewClass extends JFrame {//顶层容器类  public NewClass() {
      setTitle("计算器");
      Container contentPane = getContentPane();
      NewClass1 panel = new NewClass1();
      contentPane.add(panel);
      setSize(400,400);
      setVisible(true);
      setResizable(false);
      }
    }
    public class Main {  /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
      // TODO code application logic here
        
        
      NewClass frame = new NewClass();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        
      }
    }
      

  2.   

    NewClass1.javapublic class NewClass1 extends JPanel { // 计算器界面类 private JLabel display; // 显示结果
    private JPanel panel;
    private double result;
    private String lastCommand;
    private boolean start;
    JPanel[] JButtonPanel = new JPanel[16];
    private int count = 0; public NewClass1() {
    setLayout(new BorderLayout()); result = 0;
    lastCommand = "=";
    start = true; display = new JLabel("计算结果: 0 ", SwingConstants.RIGHT);
    display.setForeground(Color.black); // 设置前景颜色 display.setBorder(BorderFactory.createCompoundBorder(BorderFactory
    .createLineBorder(Color.black), BorderFactory
    .createEmptyBorder(6, 6, 6, 6))); add(display, BorderLayout.NORTH);
    ActionListener insert = new InsertAction();
    ActionListener command = new CommandAction(); panel = new JPanel();
    panel.setLayout(new GridLayout(4, 4)); // 计算器按钮 addButton("1", insert);
    addButton("2", insert);
    addButton("3", insert);
    addButton("4", insert);
    addButton("5", insert);
    addButton("6", insert);
    addButton("7", insert);
    addButton("8", insert);
    addButton("9", insert);
    addButton("/", command);
    addButton("*", command);
    addButton("-", command);
    addButton("0", insert);
    addButton(".", insert);
    addButton("=", command);
    addButton("+", command);
    add(panel, BorderLayout.CENTER); } private void addButton(String label, ActionListener listener) {
    JButton button = new JButton(label);
    button.addActionListener(listener);
    button.setSize(25, 20);
    button.setLayout(null);
    // JButtonPanel=new JPanel[count];

                     if(JButtonPanel[count] == null)
    JButtonPanel[count] = new JPanel();

                     JButtonPanel[count].add(button); panel.add(JButtonPanel[count]);
    count++; } 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);
    }
    } private class CommandAction implements ActionListener { // 点击按钮监听器类 public void actionPerformed(ActionEvent evt) {
    String command = evt.getActionCommand(); if (start) {
    lastCommand = command;
    } else {
    calculate(Double.parseDouble(display.getText()));
    lastCommand = command;
    start = true;
    }
    }
    } 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);
    }
    }
    NewClass.javapublic class NewClass extends JFrame {// 顶层容器类 public NewClass() {
    setTitle("计算器");
    Container contentPane = getContentPane();
    NewClass1 panel = new NewClass1();
    contentPane.add(panel);
    setSize(400, 400);
    setVisible(true);
    setResizable(false);
    }
    }
    MainClass.java (替代了之前的Main类,仅类名修改,并建议LZ以后最好不要这样进行类命名)public class MainClass { /**
     * @param args
     */
    public static void main(String[] args) {
    NewClass frame = new NewClass();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  3.   

    修改位置:

                     if(JButtonPanel[count] == null)
                JButtonPanel[count] = new JPanel();