Java - Ayuda con un programa

 
Vista:

Ayuda con un programa

Publicado por Agarosh (4 intervenciones) el 27/08/2009 11:28:53
Saludos, tengo un problema con la lectura o escritura de los ficheros y no se que estoy haciendo mal, abajo encontrareís el codigo, lo que deseo hacer es k cuando se cree un objeto del tipo cargar salga una lista de botones con los datos del fichero, el error es que solo lee un objeto del fichero.Hay mucho codigo de relleno pero no se si me equivoco en algo anterior o es solo el tema de los ficheros, por favor intentad resolverlo con el minimo cambio del codigo original, gracias de antemano.

public class SeleccionT extends MouseAdapter implements Datos {
JFrame marco;
JPanel panel;
JScrollPane panel2;
JSplitPane division;
JLabel [] imagen=new JLabel[5];
JLabel [] valor=new JLabel[5];
JLabel total;
JTextField [] campoTexto=new JTextField[5];
JTextArea areaTexto;
String lista[]= {"ESPAÑOLES", "INGLESES", "FRANCESES","JAPONESES","REINOS ARABES"};
int tropa[]=new int[5];
int aux[]=new int[5];
JButton []incluir=new JButton[5];
JButton borrar, aceptar;
String ejercito, nombreejercito;
int maximo;
int maximo2;
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("ejercito.dat"));
boolean continuar=false;
Ejercito objeto;
SeleccionT(String ejercito, int maximo, String nombreejercito)throws IOException{

this.nombreejercito=nombreejercito;
this.ejercito=ejercito;
this.maximo=maximo;
this.maximo2=maximo;
marco=new JFrame("WARGAME");
marco.setDefaultCloseOperation(JFrame.DISPOSE_ON_C LOSE);
marco.setLayout(new BorderLayout());
panel=new JPanel(new GridLayout(6,5));

//Inclusión en la variable tropa de las tropas que posee el ejercito
tropa[0]=0;
tropa[1]=1;
tropa[2]=2;
tropa[3]=3;
if(ejercito==lista[0]){
tropa[4]=4;
}
if(ejercito==lista[1]){
tropa[4]=5;
}
if(ejercito==lista[2]){
tropa[4]=6;
}
if(ejercito==lista[3]){
tropa[4]=7;
}
if(ejercito==lista[4]){
tropa[4]=8;
}

//Inclusión en la variable aux el coste de cada tropa
for(int i=0;i<5;i++) aux[i]=coste[tropa[i]];

for(int i=0;i<5;i++){
if(i==0) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("arqueros.jpg"))) ;
if(i==1) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("lanceros.jpg"))) ;
if(i==2) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("espadachines.jpg ")));
if(i==3) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("caballeria.jpg") ));
if(i==4) {
if(tropa[4]==4) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("alabarderos.jpg" )));
if(tropa[4]==5) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("arcabuceros.jpg" )));
if(tropa[4]==6) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("caballeriapesada .jpg")));
if(tropa[4]==7) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("samurais.jpg"))) ;
if(tropa[4]==8) imagen [i]=new JLabel(new ImageIcon(getClass().getResource("yihadistas.jpg") ));
}

valor[i]=new JLabel("Coste/und"+aux[i]);

campoTexto[i]=new JTextField();
campoTexto[i].setName(""+i);

incluir[i]=new JButton("INCLUIR");
incluir[i].setName(""+i);
incluir[i].addMouseListener(this);

panel.add(imagen[i]);
panel.add(valor[i]);
panel.add(campoTexto[i]);
panel.add(incluir[i]);
}

aceptar=new JButton("Aceptar");
aceptar.setName("Aceptar");
aceptar.addMouseListener(this);
borrar=new JButton("Borrar Todo");
borrar.setName("Borrar");
borrar.addMouseListener(this);
total=new JLabel("Oro restante= "+maximo);
panel.add(total);
panel.add(aceptar);
panel.add(borrar);
areaTexto=new JTextArea("");
panel2=new JScrollPane(areaTexto);
areaTexto.append("NOMBRE "+"M "+"HA "+"HP "+"F "+"R "+"H "+"I "+"A "+"L "+" ");

panel.setSize(1200,500);
panel2.setSize(1200,500);
division=new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, panel2);
division.setDividerLocation(1000);
marco.add("North", panel);
marco.add("Center", panel2);
marco.setLocation(30, 40);
marco.setSize(1200,600);
marco.setVisible(true);
}

Tropa[] objTropa=new Tropa[100];
int auxObjTropa=0;
int aux3;
public void mousePressed(MouseEvent e){
Component aux2=e.getComponent();

if(aux2.getName().equals("Aceptar")){
try{
//Busqueda hasta un null para
for(int i=0; objTropa[i]==null; i++) aux3++;
Tropa objTropaFinal[]=new Tropa[aux3];
for(int i=0; i<objTropaFinal.length;i++) objTropaFinal[i]=objTropa[i];

objeto=new Ejercito(nombreejercito, objTropaFinal);
continuar=true;

oos.writeObject(objeto);
oos.close();
marco.dispose();
}catch(Exception exc){System.out.println("ERROR EN LA LECTURA");}

}
if(aux2.getName().equals("Borrar")){
areaTexto.append("----------------TODO BORRADO---------------- ");
areaTexto.append("NOMBRE "+"M "+"HA "+"HP "+"F "+"R "+"H "+"I "+"A "+"L "+" ");
maximo=maximo2;
Tropa basura;
for(int i=0;i<objTropa.length;i++){
basura=objTropa[i];
}

}

}
public Ejercito getEjercito(){
return objeto;
}
public boolean continuar(){
return continuar;
}
protected void writeStreamHeader() throws IOException{
// No hacer nada.
}

}


public class Cargar {
JButton [] boton;
JPanel panel;
JFrame marco;
ObjectInputStream ois123;
File consulta;
JLabel etiqueta;
Cargar()throws IOException,ClassNotFoundException{
JFrame.setDefaultLookAndFeelDecorated(true);
marco=new JFrame("WARGAME");
marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
marco.setLayout(new BorderLayout());
panel=new JPanel(new GridLayout(5,5));
ois123 = new ObjectInputStream(new FileInputStream("ejercito.dat"));

int i=numerodeelementos();

if(i!=0){
ois123 = new ObjectInputStream(new FileInputStream("ejercito.dat"));

Ejercito[] objeto=new Ejercito[i];
boton=new JButton[i];
for (int j=0;j<boton.length; j++){
objeto[i-1]= (Ejercito)ois123.readObject();
boton[j]=new JButton(objeto[i-1].getnombreejercito());
panel.add(boton[j]);
}
ois123.close();
}
else{
etiqueta=new JLabel("NO EXISTEN EJERCITOS GUARDADOS");
panel.add(etiqueta);

}
marco.add(panel);
marco.pack();
marco.setVisible(true);

}

public int numerodeelementos(){
int i=0;
try{
ois123 = new ObjectInputStream(new FileInputStream("ejercito.dat"));
while(ois123.readObject()!=null){
i++;
}
ois123.close();
}catch(Exception excp){}
return i;
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder