import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.String.*;
public class calculator
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("calculator");
setSize(WIDTH,HEIGHT);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
setResizable(false);
}
public static final int WIDTH=300;
public static final int HEIGHT=300;
}class ButtonPanel extends JPanel
{   
  int result=0;
  int num1=0;
  static int j=0;
  static int i=1;
JTextArea area=null;
int a[]=new int[100];
String b[]=new String [50];
public ButtonPanel()
{  for (int s=0;s<100;s++)
  {
  a[s]=0;
  }
  
  for(int t=0;t<50;t++)
  {
  b[t]="+";
  }
  
area=new JTextArea(1,25);
  area.setEditable(false);
  JScrollPane pane=new JScrollPane(area);
  add(pane);
  System.out.println();
  JButton Button0=new JButton("0");
JButton Button1=new JButton("1");
JButton Button2=new JButton("2");
JButton Button3=new JButton("3");
JButton Button4=new JButton("4");
JButton Button5=new JButton("5");
JButton Button6=new JButton("6");
JButton Button7=new JButton("7");
JButton Button8=new JButton("8");
JButton Button9=new JButton("9");
JButton Button10=new JButton("vc");
JButton Button11=new JButton("+");
JButton Button12=new JButton("-");
JButton Button13=new JButton("*");
JButton Button14=new JButton("/");
JButton Button15=new JButton("=");
add(Button0);
add(Button1);
add(Button2);
add(Button3);
add(Button4);
add(Button5);
add(Button6);
add(Button7);
add(Button8);
add(Button9);
add(Button10);
add(Button11);
add(Button12);
add(Button13);
add(Button14);
add(Button15);ButtonActionA ButtonAction0=new ButtonActionA("0");
ButtonActionA ButtonAction1=new ButtonActionA("1");
ButtonActionA ButtonAction2=new ButtonActionA("2");
ButtonActionA ButtonAction3=new ButtonActionA("3");
ButtonActionA ButtonAction4=new ButtonActionA("4");
ButtonActionA ButtonAction5=new ButtonActionA("5");
ButtonActionA ButtonAction6=new ButtonActionA("6");
ButtonActionA ButtonAction7=new ButtonActionA("7");
ButtonActionA ButtonAction8=new ButtonActionA("8");
ButtonActionA ButtonAction9=new ButtonActionA("9");
ButtonActionD ButtonAction10=new ButtonActionD("0");
ButtonActionB ButtonAction11=new ButtonActionB("+");
ButtonActionB ButtonAction12=new ButtonActionB("-");
ButtonActionB ButtonAction13=new ButtonActionB("*");
ButtonActionB ButtonAction14=new ButtonActionB("/");
ButtonActionC ButtonAction15=new ButtonActionC("=");
Button0.addActionListener(ButtonAction0);
Button1.addActionListener(ButtonAction1);
Button2.addActionListener(ButtonAction2);
Button3.addActionListener(ButtonAction3);
Button4.addActionListener(ButtonAction4);
Button5.addActionListener(ButtonAction5);
Button6.addActionListener(ButtonAction6);
Button7.addActionListener(ButtonAction7);
Button8.addActionListener(ButtonAction8);
Button9.addActionListener(ButtonAction9);
Button10.addActionListener(ButtonAction10);
Button11.addActionListener(ButtonAction11);
Button12.addActionListener(ButtonAction12);
Button13.addActionListener(ButtonAction13);
Button14.addActionListener(ButtonAction14);
Button15.addActionListener(ButtonAction15);
  
}
  public void paintComponent(Graphics g)
  {
super.paintComponent(g);
area.setText(ButtonText);
  System.out.println();
  }
  private class ButtonActionA implements ActionListener //
  {
  public ButtonActionA(String text)  
  {
  clicktext = text;
  }
  public void actionPerformed(ActionEvent event)
  {
  //ButtonText=clicktext;
  //repaint();
  /*int count=0;
  if (count==0)
  {
  ButtonText=clicktext;
  rapain();
  count++;
  }
  if()*/
    
  
    
  ////
  num1=(num1*10)+Integer.parseInt(clicktext);
  
  ButtonText =new String(num1+"");
  a[i]=num1;
  i++;
  repaint();
  String string=ButtonText;
  if(string.startsWith("0"))
  {
  ButtonText=clicktext;
  }
  if (! ButtonText.matches("^\\d"))  
  { //这样也可以判断
  ButtonText = ButtonText.replaceAll("^\\D*", "");
  }
    
    
    
    
    
  
  }
  private String clicktext="0";
  }
  public String ButtonText="0";
  private class ButtonActionB implements ActionListener//Ӽ˳
  {
  public ButtonActionB(String text)  
  {
  clicktext = text;
  }
  public void actionPerformed(ActionEvent event)
  {
  
  
  ButtonText=clicktext;
  repaint();
  b[j]=clicktext;
  j++;
  num1=0;
  
  
  }
  private String clicktext="0";
  }
  private class ButtonActionC implements ActionListener//
  {
  public ButtonActionC(String text)  
  {
  clicktext = text;
  }
  public void actionPerformed(ActionEvent event)
  {
  
  if(b[j-1]=="+")
  {
  result=a[i-1]+a[i-2];
  }
  if(b[j-1]=="-")
  {
  result=a[i-1]-a[i-2];
  }
  if(b[j-1]=="*")
  {
  result=a[i-1]*a[i-2];
  }
  if(b[j-1]=="/")
  {
  result=a[i-1]/a[i-2];
  
  }
  num1=0;
  a[i]=result;
  i++;
  ButtonText=new String(result+"");
  repaint();
  
  }
  //ButtonText=new String("0"+"");
  private String clicktext="0";
    
  }
  private class ButtonActionD implements ActionListener//
  {
  public ButtonActionD(String text)  
  {
  clicktext = text;
  }
  public void actionPerformed(ActionEvent event)
  {
  for (int m=0;m<100;m++)
  {
  a[m]=0;
  }
  for(int n=0;n<50;n++)
  {
  b[n]="+";
  }
  result=0;
  ButtonText="0"; 
    
  repaint();
    
  }
  }
  private String clicktext="0";
}

解决方案 »

  1.   


    // 上次也有一个一样的。
    imrort java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class calculator {
        public static void main(String[] args) {
            ButtonFrame frame = new ButtonFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // frame.show();
            // 过时了
            frame.setVisible(true);
        }
    }class ButtonFrame extends JFrame {    private static final long serialVersionUID = 1L;    public ButtonFrame() {
            setTitle("calculator");
            setSize(WIDTH, HEIGHT);
            ButtonPanel panel = new ButtonPanel();
            Container contentPane = getContentPane();
            contentPane.add(panel);
            setResizable(false);
        }    public static final int WIDTH = 300;
        public static final int HEIGHT = 300;
    }class ButtonPanel extends JPanel {    private static final long serialVersionUID = 1L;
        int result = 0;
        int num1 = 0;
        static int j = 0;
        static int i = 1;
        JTextArea area = null;
        int a[] = new int[100];
        String b[] = new String[50];    public ButtonPanel() {        for (int s = 0; s < 100; s++) {
                a[s] = 0;
            }        for (int t = 0; t < 50; t++) {
                b[t] = "+";
            }        area = new JTextArea(1, 25);
            area.setEditable(false);
            JScrollPane pane = new JScrollPane(area);
            add(pane);
            System.out.println();
            JButton Button0 = new JButton("0");
            JButton Button1 = new JButton("1");
            JButton Button2 = new JButton("2");
            JButton Button3 = new JButton("3");
            JButton Button4 = new JButton("4");
            JButton Button5 = new JButton("5");
            JButton Button6 = new JButton("6");
            JButton Button7 = new JButton("7");
            JButton Button8 = new JButton("8");
            JButton Button9 = new JButton("9");
            JButton Button10 = new JButton("vc");
            JButton Button11 = new JButton("+");
            JButton Button12 = new JButton("-");
            JButton Button13 = new JButton("*");
            JButton Button14 = new JButton("/");
            JButton Button15 = new JButton("=");
            add(Button0);
            add(Button1);
            add(Button2);
            add(Button3);
            add(Button4);
            add(Button5);
            add(Button6);
            add(Button7);
            add(Button8);
            add(Button9);
            add(Button10);
            add(Button11);
            add(Button12);
            add(Button13);
            add(Button14);
            add(Button15);        ButtonActionA ButtonAction0 = new ButtonActionA("0");
            ButtonActionA ButtonAction1 = new ButtonActionA("1");
            ButtonActionA ButtonAction2 = new ButtonActionA("2");
            ButtonActionA ButtonAction3 = new ButtonActionA("3");
            ButtonActionA ButtonAction4 = new ButtonActionA("4");
            ButtonActionA ButtonAction5 = new ButtonActionA("5");
            ButtonActionA ButtonAction6 = new ButtonActionA("6");
            ButtonActionA ButtonAction7 = new ButtonActionA("7");
            ButtonActionA ButtonAction8 = new ButtonActionA("8");
            ButtonActionA ButtonAction9 = new ButtonActionA("9");
            ButtonActionD ButtonAction10 = new ButtonActionD("0");
            ButtonActionB ButtonAction11 = new ButtonActionB("+");
            ButtonActionB ButtonAction12 = new ButtonActionB("-");
            ButtonActionB ButtonAction13 = new ButtonActionB("*");
            ButtonActionB ButtonAction14 = new ButtonActionB("/");
            ButtonActionC ButtonAction15 = new ButtonActionC("=");
            Button0.addActionListener(ButtonAction0);
            Button1.addActionListener(ButtonAction1);
            Button2.addActionListener(ButtonAction2);
            Button3.addActionListener(ButtonAction3);
            Button4.addActionListener(ButtonAction4);
            Button5.addActionListener(ButtonAction5);
            Button6.addActionListener(ButtonAction6);
            Button7.addActionListener(ButtonAction7);
            Button8.addActionListener(ButtonAction8);
            Button9.addActionListener(ButtonAction9);
            Button10.addActionListener(ButtonAction10);
            Button11.addActionListener(ButtonAction11);
            Button12.addActionListener(ButtonAction12);
            Button13.addActionListener(ButtonAction13);
            Button14.addActionListener(ButtonAction14);
            Button15.addActionListener(ButtonAction15);    }    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            area.setText(ButtonText);
            System.out.println();
        }    private class ButtonActionA implements ActionListener //
        {
            public ButtonActionA(String text) {
                clicktext = text;
            }        public void actionPerformed(ActionEvent event) {
                // ButtonText=clicktext;
                // repaint();
                /*
                 * int count=0; if (count==0) { ButtonText=clicktext; rapain();
                 * count++; } if()
                 */            // //
                num1 = (num1 * 10) + Integer.parseInt(clicktext);            ButtonText = new String(num1 + "");
                a[i] = num1;
                i++;
                repaint();
                String string = ButtonText;
                if (string.startsWith("0")) {
                    ButtonText = clicktext;
                }
                if (!ButtonText.matches("^\\d")) { // 这样也可以判断
                    ButtonText = ButtonText.replaceAll("^\\D*", "");
                }        }        private String clicktext = "0";
        }    public String ButtonText = "0";    private class ButtonActionB implements ActionListener// Ӽ˳
        {
            public ButtonActionB(String text) {
                clicktext = text;
            }        public void actionPerformed(ActionEvent event) {            ButtonText = clicktext;
                repaint();
                b[j] = clicktext;
                j++;
                num1 = 0;        }        private String clicktext = "0";
        }    private class ButtonActionC implements ActionListener//
        {
            public ButtonActionC(String text) {
                clicktext = text;
            }        public void actionPerformed(ActionEvent event) {            if (b[j - 1] == "+") {
                    result = a[i - 1] + a[i - 2];
                }
                if (b[j - 1] == "-") {
                    result = a[i - 1] - a[i - 2];
                }
                if (b[j - 1] == "*") {
                    result = a[i - 1] * a[i - 2];
                }
                if (b[j - 1] == "/") {
                    result = a[i - 1] / a[i - 2];            }
                num1 = 0;
                a[i] = result;
                i++;
                ButtonText = new String(result + "");
                repaint();        }        // ButtonText=new String("0"+"");
            @SuppressWarnings("unused")
            private String clicktext = "0";    }    private class ButtonActionD implements ActionListener//
        {
            public ButtonActionD(String text) {
                clicktext = text;
            }        public void actionPerformed(ActionEvent event) {
                for (int m = 0; m < 100; m++) {
                    a[m] = 0;
                }
                for (int n = 0; n < 50; n++) {
                    b[n] = "+";
                }
                result = 0;
                ButtonText = "0";            repaint();        }
        }    @SuppressWarnings("unused")
        private String clicktext = "0";
    }