Topic: 自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!!

  Print this page

1.自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!! Copy to clipboard
Posted by: king431
Posted on: 2005-11-08 20:30

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Count {
  public static void main(String args[])
  {
    JFrame Myframe=new JFrame();
    Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Myframe.show();
  }
}
class Myframe extends JFrame
{
  public static final int HEIGHT=200;
  public static final int WIDHT=300;
  public Myframe()
  {
    setTitle("计算器");
    setSize(WIDHT,HEIGHT);
    Container con=getContentPane();
    CountPanel countpanel=new CountPanel();
    con.add(countpanel,BorderLayout.CENTER);
  }
}
class CountPanel extends JPanel
{
  private JPanel panel=new JPanel();
  private String lastcommand="=";
  private double result=0;
  private boolean start=true;
      
  setLayout(new GridBagLayout());
  private JTextField Numshow=new JTextField("0",15);
  private JButton CE=new JButton("CE");
  private JButton Backspace=new JButton("Backspace");
  private JButton Num0=new JButton("0");
  private JButton Num1=new JButton("1");
  private JButton Num2=new JButton("2");
  private JButton Num3=new JButton("3");
  private JButton Num4=new JButton("4");
  private JButton Num5=new JButton("5");
  private JButton Num6=new JButton("6");
  private JButton Num7=new JButton("7");
  private JButton Num8=new JButton("8");
  private JButton Num9=new JButton("9");
  private JButton Add=new JButton("+");
  private JButton Sub=new JButton("-");
  private JButton Mul=new JButton("*");
  private JButton Div=new JButton("/");
  private JButton Equ=new JButton("=");
  
  ActionListener numAction=new NumAction();
  ActionListener Command=new CommandAction();
  
  GridBagConstraints constraints=new GridBagConstraints();
  constraints.fill=GridBagConstraints.HORIZONTAL;
  constraints.weightx = 6;
  constraints.weighty = 4;
  
  addText(Numshow,constraints,0,0,1,4);
  
  addButton(CE,constraints,1,0,1,2,Command);
  addButton(Backspace,constraints,1,2,1,2,Command);
  
  addButton(Num7,constraints,2,0,1,1,numAction);
  addButton(Num8,constraints,2,1,1,1,numAction);
  addButton(Num9,constraints,2,2,1,1,numAction);
  addButton(Div,constraints,2,3,1,1,Command);
  
  addButton(Num4,constraints,3,0,1,1,numAction);
  addButton(Num5,constraints,3,1,1,1,numAction);
  addButton(Num5,constraints,3,2,1,1,numAction);
  addButton(Mul,constraints,3,3,1,1,Command);
  
  addButton(Num1,constraints,4,0,1,1,numAction);
  addButton(Num2,constraints,4,1,1,1,numAction);
  addButton(Num3,constraints,4,2,1,1,numAction);
  addButton(Sub,constraints,4,3,1,1,Command);
  
  addButton(Num0,constraints,5,0,1,1,numAction);
  addButton(Equ,constraints,5,1,1,2,Command);
  addButton(Add,constraints,5,3,1,1,Command);
  }
  
  public void addText(JTextField c,GridBagConstraints constraints,int x,int y,int w,int h)
  {
    constraints.gridx=x;
    constraints.gridy=y;
    constraints.gridwidth=w;
    constraints.gridheight=h;
    add(c,constraints);
  }
  public void addButton(Button button,GridBagConstraints constraints,int x,int y,int w,int h,ActionListener listener)
  {
    constraints.gridx=x;
    constraints.gridy=y;
    constraints.gridwidth=w;
    constraints.gridheight=h;
    button.addActionListener(listener);
    add(button,constraints);
  }
  private class NumAction implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      String input=event.getActionCommand();
      if(start)
      {
        Numshow.setText("");
        start=false;
      }
      Numshow.setText(Numshow.getText() +input);
    }
  }
  private class CommandAction implements ActionListener
  {
    public void actionPerformed(ActionEvent evt)
    {
      String command=evt.getActionCommand();
      if(start)
      {
        if(command.equals("-"))
        {
          Numshow.setText(command);
          start=false;
        }
        else
          lastcommand=command;
      }
      else
      {
        calculate(Integer.parseInt(Numshow.getText()));
        lastcommand=command;
        start=true;
      }
    }
  }
  public void calculate (int 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;
    else if(lastcommand.equals("CE")) result=0;
    else if(lastcommand.equals("Backspace")) result=result/10;
    Numshow.setText(""+result);
  }
}

2.Re:自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!! [Re: king431] Copy to clipboard
Posted by: francis36
Posted on: 2005-11-09 00:45

老大,这么多错误怎么运行呢~

3.Re:自己的作业,运行不了,能力有限查不出错误,希望各大虾帮帮忙,急!!!! [Re: king431] Copy to clipboard
Posted by: annieKim
Posted on: 2005-11-09 08:58

//注册过了12小时才能发言。真是!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Myframe extends JFrame
{
public static final int HEIGHT=200;
public static final int WIDHT=300;
public Myframe()
{
setTitle("计算器");
setSize(WIDHT,HEIGHT);
Container con=getContentPane();
CountPanel countpanel=new CountPanel();
con.add(countpanel,BorderLayout.CENTER);
}
}

class CountPanel extends JPanel
{
private JPanel panel=new JPanel();
private String lastcommand="=";
private double result=0;
private boolean start=true;

private JTextField Numshow=new JTextField("0",15);
private Button CE=new Button("CE");
private Button Backspace=new Button("Backspace");
private Button Num0=new Button("0");
private Button Num1=new Button("1");
private Button Num2=new Button("2");
private Button Num3=new Button("3");
private Button Num4=new Button("4");
private Button Num5=new Button("5");
private Button Num6=new Button("6");
private Button Num7=new Button("7");
private Button Num8=new Button("8");
private Button Num9=new Button("9");
private Button Add=new Button("+");
private Button Sub=new Button("-");
private Button Mul=new Button("*");
private Button Div=new Button("/");
private Button Equ=new Button("=");

public CountPanel()
{
setLayout(new GridBagLayout());
ActionListener numAction=new NumAction();
ActionListener Command=new CommandAction();

GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.weightx = 6;
constraints.weighty = 4;

addText(Numshow,constraints,0,0,1,4);

addButton(CE,constraints,1,0,1,2,Command);
addButton(Backspace,constraints,1,2,1,2,Command);

addButton(Num7,constraints,2,0,1,1,numAction);
addButton(Num8,constraints,2,1,1,1,numAction);
addButton(Num9,constraints,2,2,1,1,numAction);
addButton(Div,constraints,2,3,1,1,Command);

addButton(Num4,constraints,3,0,1,1,numAction);
addButton(Num5,constraints,3,1,1,1,numAction);
addButton(Num5,constraints,3,2,1,1,numAction);
addButton(Mul,constraints,3,3,1,1,Command);

addButton(Num1,constraints,4,0,1,1,numAction);
addButton(Num2,constraints,4,1,1,1,numAction);
addButton(Num3,constraints,4,2,1,1,numAction);
addButton(Sub,constraints,4,3,1,1,Command);

addButton(Num0,constraints,5,0,1,1,numAction);
addButton(Equ,constraints,5,1,1,2,Command);
addButton(Add,constraints,5,3,1,1,Command);
}
public void addText(JTextField c,GridBagConstraints constraints,int
x,int y,int w,int h)
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=w;
constraints.gridheight=h;
add(c,constraints);
}
public void addButton(Button button,GridBagConstraints
constraints,int x,int y,int w,int h,ActionListener listener)
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=w;
constraints.gridheight=h;
button.addActionListener(listener);
add(button,constraints);
}
private class NumAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input=event.getActionCommand();
if(start)
{
Numshow.setText("");
start=false;
}
Numshow.setText(Numshow.getText() +input);
}
}

private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String command=evt.getActionCommand();
if(start)
{
if(command.equals("-"))
{
Numshow.setText(command);
start=false;
}
else
lastcommand=command;
}
else
{
calculate(Integer.parseInt(Numshow.getText()));
lastcommand=command;
start=true;
}
}
public void calculate (int 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;
else if(lastcommand.equals("CE")) result=0;
else if(lastcommand.equals("Backspace")) result=result/10;
Numshow.setText(""+result);
}
}
}

public class Count {
public static void main(String args[])
{
Myframe myframe=new Myframe();
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.show();
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923