Como passar Valores de Um Box para um Vetor

1 resposta
S

:oops: Estou com um problemão, não consigo passar os valores digitados em um Box (campo[0]) para um vetor(f1[i]), meu código está MUITO CONFUSO pois estou iniciando neste UNIVERSO JAVA e utilizo a técnica do Ctrl+C, Ctrl+V, dicas, …

:?: :!: :!: :?: O que devo fazer :?: :?: :!: :?:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Janela extends JFrame
{
   private GridBagLayout gbLayout;
   JTextField camp, tfResultado;
   
   public Janela()
   {
	  super( "Menor Caminho" );
	  
	  String estacao;
      int i_estacao;
       
      estacao = JOptionPane.showInputDialog("Entre com a QUANTIDADE DE ESTAÇÕES");
      i_estacao = Integer.parseInt(estacao);   	
   	      
      final int SIZE = i_estacao;
     
	  int result = 0;
	  int f1[] = new int[20];
	  JLabel lValor1 = new JLabel("Linha de Prod A");
   	  JLabel lValor2 = new JLabel("Linha de Prod B");
	  JLabel lValor3 = new JLabel("De A Para B");
   	  JLabel lValor4 = new JLabel("De B Para A");
   	  JButton mCam = new JButton("Menor Caminho");
   	  camp = new JTextField("0");
   	  tfResultado = new JTextField(8);
   	  tfResultado.setEditable(false);
   	  
  	  

      GridBagConstraints gbConstraints = new GridBagConstraints();
      Container c = getContentPane();
      gbLayout = new GridBagLayout();
      c.setLayout( gbLayout );
      //c.setLayout( new GridLayout(5,10));

	  gbConstraints.fill = GridBagConstraints.CENTER;
     
      Box boxes[] = new Box[9];
      Box campo[] = new Box[9];
                  
      Box c1 = Box.createVerticalBox();
      Box c2 = Box.createVerticalBox();
      Box c3 = Box.createVerticalBox();
      Box c4 = Box.createVerticalBox();
      Box c5 = Box.createVerticalBox();
      Box c6 = Box.createVerticalBox();
      Box c7 = Box.createVerticalBox();
      Box c8 = Box.createVerticalBox();
      Box c9 = Box.createHorizontalBox();
      

      boxes[0] = Box.createVerticalBox();
      boxes[1] = Box.createVerticalBox();
      boxes[2] = Box.createVerticalBox();
      boxes[3] = Box.createVerticalBox();
      campo[0] = Box.createVerticalBox();
      campo[1] = Box.createVerticalBox();
      campo[2] = Box.createVerticalBox();
      campo[3] = Box.createVerticalBox();
     
           
      for (int i=0; i<(SIZE + 2); i++)
      {
      	boxes[0].add(Box.createRigidArea (new Dimension(38,8)));
      	boxes[0].add(new JButton("Est.A, "+i));
      }	
      	
      for (int i=0; i<(SIZE + 2); i++)
      {
      	boxes[1].add(Box.createRigidArea (new Dimension(38,8)));
      	boxes[1].add(new JButton("Est.B, "+i));
      }
      
      for (int i=1; i<SIZE ; i++)
      {
      	boxes[2].add(Box.createRigidArea (new Dimension(38,8)));
      	boxes[2].add(new JButton("A"+i+ " p/ B"+(i+1)));
      }	
      	
      for (int i=1; i<SIZE; i++)
      {
      	boxes[3].add(Box.createRigidArea (new Dimension(38,8)));
      	boxes[3].add(new JButton("B"+i+ " p/ A"+(i+1)));
      }
      
      for (int i=0; i<(SIZE + 2); i++)
      {
      	campo[0].add(Box.createRigidArea (new Dimension(62,14)));
      	campo[0].add(new JTextField(""+i));
      	//f1[i] = Integer.parseInt(campo[0].getText());
      	result = result + f1[i];
      }	
      
      for (int i=0; i<(SIZE + 2); i++)
      {
      	campo[1].add(Box.createRigidArea (new Dimension(62,14)));
      	campo[1].add(new JTextField("0"));
      }	
      
      for (int i=1; i<SIZE; i++)
      {
      	campo[2].add(Box.createRigidArea (new Dimension(62,14)));
      	campo[2].add(new JTextField("0"));
      }	
      
      for (int i=1; i<SIZE; i++)
      {
      	campo[3].add(Box.createRigidArea (new Dimension(62,14)));
      	campo[3].add(new JTextField("0"));
      }	
     
      mCam.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                int x = Integer.parseInt(camp.getText());
                tfResultado.setText(String.valueOf(x));	
            }
        });
      
      
      c1.add(lValor1); c1.add(boxes[0]);
      c.add(c1);
      c2.add(campo[0]);
      c.add(c2);
      c3.add(lValor2); c3.add(boxes[1]);
      c.add(c3);
      c4.add(campo[1]);
      c.add(c4);
      c5.add(lValor3); c5.add(boxes[2]);
      c.add(c5);
      c6.add(campo[2]);
      c.add(c6);
      c7.add(lValor4); c7.add(boxes[3]);
      c.add(c7);
      c8.add(campo[3]);
      c.add(c8);
      c9.add(mCam); c9.add(tfResultado);
      c.add(c9);
      pack();
 
   }
}
   
public class MenorCaminho1
{

   	public static void main( String args[] )
   	{
     	Janela j = new Janela();
        j.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        } });
        j.show();
    }
}

1 Resposta

J

realmente está bem confuso… nem consegui entender o qeu vc está precisando? o que seria esse Box/?

bom, da uma explicadinha melhor ai…

abraço! cya!

Criado 2 de junho de 2004
Ultima resposta 10 de jul. de 2004
Respostas 1
Participantes 2