主题:请大家帮我看看这个"计算器"错在哪啊,谢谢啦
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
public class JPanelDemo extends JFrame implements ActionListener {
private JPanel DisplayPanel;
private JPanel InputPanel;
private Container container;
int x,y;
int z;
StringBuffer str;
private JTextField tfResult;
private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
private static final String Lables="789*456/123-0C=+";
public JPanelDemo(){
super("计算器");
setSize(200,230);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tfResult=new JTextField("27");
tfResult.setHorizontalAlignment(JTextField.RIGHT);
tfResult.setEnabled(false);
tfResult.setText("0");
b0=new JButton("7");
b0.addActionListener(this);
b1=new JButton("8");
b1.addActionListener(this);
b2=new JButton("9");
b2.addActionListener(this);
b3=new JButton("*");
b3.addActionListener(this);
b4=new JButton("4");
b4.addActionListener(this);
b5=new JButton("5");
b5.addActionListener(this);
b6=new JButton("6");
b6.addActionListener(this);
b7=new JButton("/");
b7.addActionListener(this);
b8=new JButton("1");
b8.addActionListener(this);
b9=new JButton("2");
b9.addActionListener(this);
b10=new JButton("3");
b10.addActionListener(this);
b11=new JButton("-");
b11.addActionListener(this);
b12=new JButton("0");
b12.addActionListener(this);
b13=new JButton("c");
b13.addActionListener(this);
b14=new JButton("=");
b14.addActionListener(this);
b15=new JButton("+");
b15.addActionListener(this);
str=new StringBuffer();
}
public void setLayout(){
container=getContentPane();
container.setLayout(new BorderLayout());
DisplayPanel=new JPanel();
DisplayPanel.setLayout(new BorderLayout());
tfResult=new JTextField();
DisplayPanel.add(tfResult,BorderLayout.CENTER);
container.add(DisplayPanel,BorderLayout.NORTH);
InputPanel=new JPanel();
InputPanel.setLayout(new GridLayout(4,4));
for(int i=0;i<Lables.length();i++){
JButton btn=new JButton(Lables.substring(i,i+1));
InputPanel.add(btn);
}
container.add(InputPanel,BorderLayout.CENTER);
}
public static void main(String []args){
JPanelDemo TestPanel=new JPanelDemo();
TestPanel.setLayout();
TestPanel.show();
}
public void actionPerformed(ActionEvent e2){
try{
if(e2.getSource()==b13)
{
tfResult.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b15)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=0;
}
else if(e2.getSource()==b11)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=1;
}
else if(e2.getSource()==b3)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=2;
}
else if(e2.getSource()==b7)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=3;
}
else if(e2.getSource()==b14)
{
str.setLength(0);
switch(z)
{
case 0 : tfResult.setText(""+(x+y));break;
case 1 : tfResult.setText(""+(x-y));break;
case 2 : tfResult.setText(""+(x*y));break;
case 3 : tfResult.setText(""+(x/y));break;
}
}
else
{
tfResult.setText(str.append(e2.getActionCommand()).toString());
y=Integer.parseInt(tfResult.getText().trim());
}
}
catch(NumberFormatException e){
tfResult.setText("数字格式异常");
}
}
}
}
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
public class JPanelDemo extends JFrame implements ActionListener {
private JPanel DisplayPanel;
private JPanel InputPanel;
private Container container;
int x,y;
int z;
StringBuffer str;
private JTextField tfResult;
private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
private static final String Lables="789*456/123-0C=+";
public JPanelDemo(){
super("计算器");
setSize(200,230);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tfResult=new JTextField("27");
tfResult.setHorizontalAlignment(JTextField.RIGHT);
tfResult.setEnabled(false);
tfResult.setText("0");
b0=new JButton("7");
b0.addActionListener(this);
b1=new JButton("8");
b1.addActionListener(this);
b2=new JButton("9");
b2.addActionListener(this);
b3=new JButton("*");
b3.addActionListener(this);
b4=new JButton("4");
b4.addActionListener(this);
b5=new JButton("5");
b5.addActionListener(this);
b6=new JButton("6");
b6.addActionListener(this);
b7=new JButton("/");
b7.addActionListener(this);
b8=new JButton("1");
b8.addActionListener(this);
b9=new JButton("2");
b9.addActionListener(this);
b10=new JButton("3");
b10.addActionListener(this);
b11=new JButton("-");
b11.addActionListener(this);
b12=new JButton("0");
b12.addActionListener(this);
b13=new JButton("c");
b13.addActionListener(this);
b14=new JButton("=");
b14.addActionListener(this);
b15=new JButton("+");
b15.addActionListener(this);
str=new StringBuffer();
}
public void setLayout(){
container=getContentPane();
container.setLayout(new BorderLayout());
DisplayPanel=new JPanel();
DisplayPanel.setLayout(new BorderLayout());
tfResult=new JTextField();
DisplayPanel.add(tfResult,BorderLayout.CENTER);
container.add(DisplayPanel,BorderLayout.NORTH);
InputPanel=new JPanel();
InputPanel.setLayout(new GridLayout(4,4));
for(int i=0;i<Lables.length();i++){
JButton btn=new JButton(Lables.substring(i,i+1));
InputPanel.add(btn);
}
container.add(InputPanel,BorderLayout.CENTER);
}
public static void main(String []args){
JPanelDemo TestPanel=new JPanelDemo();
TestPanel.setLayout();
TestPanel.show();
}
public void actionPerformed(ActionEvent e2){
try{
if(e2.getSource()==b13)
{
tfResult.setText("0");
str.setLength(0);
}
else if(e2.getSource()==b15)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=0;
}
else if(e2.getSource()==b11)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=1;
}
else if(e2.getSource()==b3)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=2;
}
else if(e2.getSource()==b7)
{
x=Integer.parseInt(tfResult.getText().trim());
str.setLength(0);
y=0;
z=3;
}
else if(e2.getSource()==b14)
{
str.setLength(0);
switch(z)
{
case 0 : tfResult.setText(""+(x+y));break;
case 1 : tfResult.setText(""+(x-y));break;
case 2 : tfResult.setText(""+(x*y));break;
case 3 : tfResult.setText(""+(x/y));break;
}
}
else
{
tfResult.setText(str.append(e2.getActionCommand()).toString());
y=Integer.parseInt(tfResult.getText().trim());
}
}
catch(NumberFormatException e){
tfResult.setText("数字格式异常");
}
}
}
}