线程AWT EVENTQUEUE 1中的异常

时间:2014-02-14 13:25:58

标签: java arrays exception awt

一切正常,直到我按下任何按钮时弹出错误。我正在按照教程进行操作,按照指定进行操作, 我不明白什么是错的。

  

“线程中的异常”AWT-EventQueue-0“   java.lang.ArrayIndexOutOfBoundsException:1“

这就是代码。

public class Med implements Comparable<Med>{
    private String nume, comp, ind, cind, mod, pret;

    public Med(String nume, String comp, String ind, String cind, String mod, String pret) {
        this.nume = nume;
        this.comp = comp;
        this.ind = ind;
        this.cind = cind;
        this.mod = mod;
        this.pret = pret;
 }
 public String getNume(){  return nume; }
 public String toString(){return nume+"_"+nume+"_"+comp+"_"+ind+"_"+pret; }
 public int compareTo(Med o){  return (nume).compareTo(o.getNume()); }
}

import java.util.*;import java.io.*; 
import javax.swing.*;
public class Farmacie{
 private TreeSet<Med> ts;
 private BufferedReader br;
 private PrintWriter pw;
 private String l;
 private Med med;
 private static Farmacie instanta;
 private Farmacie(){
  File f=new File("X:\\med.txt");
  ts=new TreeSet<Med>();
  String[] s, s1;
  String ae; 
  if (f.exists()){
   try{
    br=new BufferedReader(new FileReader(f));
    while ((l=br.readLine())!=null){
     s=l.split("_");
     s1=s[0].split(" ");
     med=new Med(s1[0], s1[1], s[1], s[2], s[3], s[4]);
     ts.add(med);
    }
   }catch(IOException ioe){ioe.printStackTrace();}  
  } else System.out.println("Fisierul nu exista");
 }  

 public String getMed(){
  String rez="";
  Iterator<Med> it=ts.iterator();
  while (it.hasNext()) rez+=it.next()+"\n";
  return rez;
 }

 public void adaugaMed(String nume, String comp, String ind, String cind, String mod, String pret){
  med=new Med(nume, comp, ind, cind, mod, pret);
  if (ts.contains(med)) {
   JOptionPane.showMessageDialog(null, "Fisa electronica exista deja in agenda telefonica", "Alert", 
JOptionPane.ERROR_MESSAGE);
   return;
  }
  ts.add(med);
  JOptionPane.showMessageDialog(null, "A fost adaugata o noua fisa electronica", "Information", 
JOptionPane.INFORMATION_MESSAGE);
 }

 public String cautaMed(String nume){
  for(Med f: ts)    if(nume.equals(f.getNume())) return f.toString();
  return "nu exista nicio fisa cu numele "+ nume;
 }

 public void stergeMed(String nume){
  Iterator<Med> it=ts.iterator();
  while (it.hasNext())  {
   if(nume.equalsIgnoreCase(it.next().getNume())) {
    it.remove();
    JOptionPane.showMessageDialog(null, "Fisa a fost stearsa din agenda   telefonica", "Information", JOptionPane.INFORMATION_MESSAGE);
    return;
   }
  } 
  JOptionPane.showMessageDialog(null, "Fisa nu se gaseste in agenda telefonica",   "Alert", JOptionPane.ERROR_MESSAGE);
 }
 public void salveaza(){
  try{
   pw=new PrintWriter(new FileWriter("fise.txt"));
   for(Med f: ts) pw.println(f);
   pw.close();  
  }catch(IOException e){e.printStackTrace();}
 }
 public static Farmacie getInstanta(){  if (instanta==null) instanta=new Farmacie();
  return instanta;
 }
 }

import java.awt.*;import java.awt.event.*;
public class MedNouFrame extends Frame{
 private GestorEvenimenteFNF ec;
 private TextField lt, at, tt, aet, mt, pt;
 private Button ok, cancel;
 public MedNouFrame(){
  super ("Adauga un med nou");
  Panel p=new Panel();
  p.setLayout(new GridLayout(6,2,10,10));
  p.setBackground(Color.lightGray); 

  p.add(new Label("Nume"));
  lt=new TextField(10);
  p.add(lt);

  p.add(new Label("Comp"));
  at=new TextField(10);
  p.add(at);

  p.add(new Label("ind"));
  tt=new TextField(10);
  p.add(tt);

  p.add(new Label("cind"));
    aet=new TextField(10);
    p.add(aet);

    p.add(new Label("mod"));
    mt=new TextField(10);
    p.add(mt);

    p.add(new Label("pret"));
    pt=new TextField(10);
    p.add(pt);

  add(p);
  ec=new GestorEvenimenteFNF();
  p=new Panel();
  ok=new Button("OK");
  ok.addActionListener(ec);
  p.add(ok);
  cancel=new Button("Cancel");
  cancel.addActionListener(ec);
  p.add(cancel);
  add(p, BorderLayout.SOUTH);       
  setLocation(300,300);
 }
  private class GestorEvenimenteFNF implements ActionListener{
  private  Farmacie tnb;
  GestorEvenimenteFNF(){tnb=Farmacie.getInstanta();}
  public void actionPerformed(ActionEvent e){
   if(e.getSource()==ok) {
      tnb.adaugaMed(lt.getText(),at.getText(),tt.getText(), aet.getText(), mt.getText(), pt.getText());
      MedNouFrame.this.dispose();
   }
   else MedNouFrame.this.dispose();
  } }}

import java.awt.*;import java.awt.event.*;
public class CautaMedFrame extends Frame{
 private GestorEvenimenteCFF ec;
 private TextField t;
 private TextArea ta;
 public CautaMedFrame(){
  super("Cauta med");
  Panel p=new Panel();
  p.add(new Label("Introduceti nume"));
  t=new TextField(10);
  ec=new GestorEvenimenteCFF();
  t.addActionListener(ec);
  p.add(t);
  add(p, BorderLayout.NORTH);       
  ta=new TextArea(20,50);
  add(ta);
  setSize(300,200);
  setLocation(300,300);
  addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent we){
   CautaMedFrame.this.dispose();
  }});
 }
  private class GestorEvenimenteCFF implements ActionListener{
  private  Farmacie at;
  GestorEvenimenteCFF(){at=Farmacie.getInstanta();}
  public void actionPerformed(ActionEvent e){
   if (e.getSource()==t){
    ta.setText(at.cautaMed(t.getText()));
    t.setText("");
   }    
  } }}

import java.awt.*;import java.awt.event.*;
public class StergeMedFrame extends Frame{
 private GestorEvenimenteFF ec;
 private TextField t;
 private Button ok, c;
 public StergeMedFrame(){
  super ("Sterge o fisa");
  Panel p=new Panel();
  p.setBackground(Color.lightGray); 
  p.add(new Label("Introduceti nume si prenume"));
  t=new TextField(10);
  p.add(t);
  add(p);       
  p=new Panel();
  ec=new GestorEvenimenteFF();
  ok=new Button("OK");
  ok.addActionListener(ec);
  p.add(ok);
  c=new Button("Cancel");
  c.addActionListener(ec);
  p.add(c);
  add(p, BorderLayout.SOUTH);
  setSize(300,100);
  setLocation(300,300); 
 }
 private class GestorEvenimenteFF implements ActionListener{
  private  Farmacie at;
  GestorEvenimenteFF(){ at=Farmacie.getInstanta();  }
  public void actionPerformed(ActionEvent e){
   if (e.getSource()==ok) {
    at.stergeMed(t.getText());
    t.setText("");
   }
   else StergeMedFrame.this.dispose();
  } }}

import java.awt.*;
import java.awt.event.*;
public class MedFrame extends Frame{
 private TextArea ta;
 private Farmacie at;
 public MedFrame(){
   super ("Agenda telefonica");
   add(new Label("Toate fisele"), BorderLayout.NORTH);
  ta=new TextArea(20,200);
  at=Farmacie.getInstanta();
  ta.setText(at.getMed());
  ta.setEnabled(false);
  add(ta);
  setSize(500,300);
  setLocation(300,300);
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
    MedFrame.this.dispose();
   }});
  } }

import java.awt.*;import java.awt.event.*;
public class FereastraPrincipala extends Frame{
 private GestorEvenimenteFP ec;
 private Button[] b;
 public FereastraPrincipala(){
  super ("Bine ati venit!");
  Panel p=new Panel();  
  p.setLayout(new GridLayout(3,2,10,10));
  p.setBackground(Color.lightGray); 
  String[] s=new String[]{"Creaza fisa noua", "Cauta fisa", "Sterge fisa", "Arata continut agenda telefonica", 
"Salveaza toate fisele"};
  ec=new GestorEvenimenteFP();  
  b=new Button[5];  
  for (int i=0; i<b.length; i++){
    b[i]=new Button(s[i]);
    b[i].addActionListener(ec);
    p.add(b[i]);
 }
 add(p);    
}
private class GestorEvenimenteFP implements ActionListener{
 private Frame f;
 public void actionPerformed(ActionEvent e){
  if(e.getSource()==b[0]){
   f=new MedNouFrame();
   f.pack();
   f.setVisible(true);
  }
  else if(e.getSource()==b[1]){   f=new CautaMedFrame();   f.setVisible(true);  }
  else if(e.getSource()==b[2]){   f=new StergeMedFrame();   f.setVisible(true);  }
  else if(e.getSource()==b[3]){    f=new MedFrame();   f.setVisible(true);  }   
  else {   Farmacie at=Farmacie.getInstanta();   at.salveaza();  }
 }  
}
public static void main(String[] args){
 Frame w=new FereastraPrincipala();
 w.setSize(375, 150);
 w.setLocation(300,300);    
 w.setVisible(true);    
 w.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){System.exit(0);}
 });    
 }  
}

1 个答案:

答案 0 :(得分:0)

我对Farmacie构造函数进行了一些更改。我对行中第一个元素的拆分进行了一些检查。如果没有空间那么它就会失败。否则你的代码就可以了。

private Farmacie() {
    File f = new File("G:\\med.txt");
    ts = new TreeSet<Med>();
    String[] s, s1;
    String ae;
    if (f.exists()) {
    try {
        br = new BufferedReader(new FileReader(f));
        while ((l = br.readLine()) != null) {
            s = l.split("_");

            s1 = s[0].split(" ");
            String first = s1[0];
            String second = null;
            if (s1.length > 1) 
                second = s1[1]; 

            med = new Med(first, second, s[1], s[2], s[3], s[4]);
            ts.add(med);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
} else
    System.out.println("Fisierul nu exista");

}