baidu_26454623 2015-06-09 14:40 采纳率: 100%
浏览 1630
已采纳

啊!脑抽了。。。怎么错了?求大神指出

图片说明import java.awt.event.*;
import javax.swing.*;
/******************************************************/
//TestProg 类为公共类,包含main函数
//在其中实现创建两个重要对象,Toy类的对象toy以及ToyalesFrame类的mine对象。

/*****************************************************/
public class TestProg{

    public static void main(String args[]){
    Toy toy = new Toy();
    ToyalesFrame mine = new ToyalesFrame();
    mine.setVisible(true);

}

}

/***********************************************/
/**************************************************/
//此类为实现程序的主界面,包括显示,布局,容器以及事件监听,并为其他类的事件处理提供参数
//本身并未实现任何实质功能

/**************************************************/
class ToyalesFrame extends JFrame{

 JPanel up;                           //声明组件,并且某些组件还包含其他组件
 JPanel down;
 static JTextArea middle;                  
 JButton buttons[];
 JTextField fields[];

 String names1[]={
        "Show Toy","Show Chart","Save and Exit"
    };
 private final String names2[]={
        "Process Sale","Clear","Staff Name:","Toy Code:","Quantity:"
    };


 public   ToyalesFrame(){                        //设置GUI和注册事件处理
    super("Toys Sales");
    this.setBounds(0,0,800,600);
    Container c= this.getContentPane();
    c.setLayout(new BorderLayout(8,8));
    up   = new JPanel();
    down = new JPanel();
    up.setOpaque(false);              
    up.setBackground(Color.gray);
    up.setLayout(new GridLayout(1,3,60,0));
    Buttonhandler handler1= new Buttonhandler();     
    buttons = new JButton [5];
    for(int count=0;count<names1.length;count++){        //添加局部组件,并且增加事件监听
        buttons[count] = new JButton(names1[count]);
        up.add(buttons[count]);
        buttons[count].addActionListener(handler1);
    }
    down.setBackground(Color.gray);     
    down.setLayout(new FlowLayout(1,50,0));     
    fields = new JTextField[3];

    Box vertical1 = Box.createVerticalBox();
    Box vertical2 = Box.createVerticalBox();
    Box vertical3 = Box.createVerticalBox();

    for(int count=3;count<5;count++)
    { buttons[count]= new JButton(""+names2[count-3]);
      buttons[count].addActionListener(handler1);
      vertical1.add(buttons[count]);
    }

    for(int count=2;count<5;count++)
         vertical2.add(new JLabel(""+names2[count]));
    for(int count=0;count<3;count++){
        fields[count]= new JTextField(15);
        vertical3 .add(fields[count]);          
    }

     down.add(vertical1);
     down.add(vertical2);
     down.add(vertical3);    
    middle = new JTextArea();
    middle.setText("序列号          玩具编号          售货员          单价          售出数量          总销售额(元)");
    middle.append("\n");
    middle.setEditable(false);

    c.add(up,BorderLayout.NORTH);
    c.add(middle,BorderLayout.CENTER);
    c.add(down,BorderLayout.SOUTH);
 }

 private class Buttonhandler implements ActionListener{        //提供事件处理的入口
    public void actionPerformed(ActionEvent event){
             if(event.getSource()==buttons[0]){
                BookFrame stock = new BookFrame();
             }
             if(event.getSource()==buttons[1]){
                ChartFrame chart = new ChartFrame();
             }
            if(event.getSource()==buttons[2]){
             // Salelist list =new Salelist();
             //Toylist list = new Toylist();

             }
             if(event.getSource()==buttons[3]) {
                 Sale dealsale = new Sale(fields[0].getText(),fields[1].getText(),fields[2].getText());
                 }
             if(event.getSource()==buttons[4]){
                 fields[0].setText("");
                 fields[1].setText("");
                 fields[2].setText("");
             }

 }
 }

}
/******************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*************************************************************/
//Sale 类为处理每一次的销售活动,包括处理,显示,记录,以及清除

/************************************************************/
class Sale {

String name = new String();
String code = new String();
String temcode = new String();
String quantity = new String();
int codenum,quantitynum,temp;
static int serialnum=1;
int exception=0;

  public Sale( String name, String code,String quantity){

         temcode= code;

     if(name.isEmpty())
          JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input the Staff name!");
     else{  
              codenum=Integer.parseInt(temcode);

             JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input a product code!");


         quantitynum = Integer.parseInt(quantity);

             JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input a product quantity!");


            if(exception==0){

              if(codenum<=0||codenum>Toy.kindsum)         
                        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"this product is not available!");
               else 
                  {
                        if (Toy.Toysum[codenum-1]<quantitynum)
                             JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"please input a quantity not above"+Toy.Toysum[codenum-1]); 
                        else {

                             Toy.Toysum[codenum-1]=Toy.Toysum[codenum-1]-quantitynum;

                             ToyalesFrame.middle.append("   "+serialnum);

                             ToyalesFrame.middle.append("                 "+Toy.Toycod[codenum-1]);
                             ToyalesFrame.middle.append("                   "+name);


                                ToyalesFrame.middle.append("               "+Toy.Toyprice[codenum-1]);
                                ToyalesFrame.middle.append("                 "+quantitynum);
                                java.text.DecimalFormat myformat=new java.text.DecimalFormat("#0.00");
                                ToyalesFrame.middle.append("                 "+Float.parseFloat(myformat.format(Toy.Toyprice[codenum-1]*quantitynum)));
                                ToyalesFrame.middle.append("\n");                     
                                serialnum++;
                            }

                   }
        }
        }




  }

}
/***********************************/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/****************************************************/
//Toy 类为从初始化文件toy.txt中读取玩具初始信息,并作相应的调整,例如存储,分类,
//以及将字符串元素转换成可以进行数学运算的int,float等类型
/*****************************************************/
class Toy{

String line; 
static String[] Toycode = new String[5];
static String[] Toyname = new String[5];
String[]  Toysum1 = new String[5];
static String[]  Toyprice1= new String[5];
static int[]    Toysum = new int[5];           
static Float[]  Toyprice= new Float[5];
static int kindsum=0;


public Toy(){

try{        
    FileInputStream infile= new FileInputStream("toy.txt");
    InputStreamReader  fis = new InputStreamReader(infile);
    BufferedReader bReader= new BufferedReader(fis);
    while((line = bReader.readLine()) != null){
        int j=0;
        String[] lines = line.split("\\ ");
        while(lines[j].length()==0) j++;
         Toycode[kindsum] = lines[j];
         System.out.println("read:"+Toycode[kindsum]);
         j++;
         while(lines[j].length()==0) j++;
         Toyname[kindsum] = lines[j];
         System.out.println("read:"+Toyname[kindsum]);
         j++;
         while(lines[j].length()==0) 
         j++;            
         Toyprice1[kindsum]  =  lines[j];
         Toyprice[kindsum] =  Float.parseFloat(Toyprice1[kindsum]);
         System.out.println("read:"+Toyprice[kindsum]);
         j++;
         while(lines[j].length()==0) j++;
         Toysum1[kindsum]  =  lines[j];
         Toysum[kindsum]   = Integer.parseInt(Toysum1[kindsum]);
        System.out.println("read:"+Toysum[kindsum]);
        kindsum++;
    }
    fis.close();

   }


      catch(FileNotFoundException e){
        System.out.println("FileStreamTest1:"+e);
      }
      catch(IOException e){
        System.out.println("FileStreamTest1:"+e);
      }

}

  • 写回答

1条回答 默认 最新

  • frank_20080215 2015-06-09 14:52
    关注

    String[] lines = line.split("\ ");
    改成String[] lines = line.split("\t");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀