Foros del Web » Programación para mayores de 30 ;) » Java »

usar variable en otra clase - java

Estas en el tema de usar variable en otra clase - java en el foro de Java en Foros del Web. hola stoy aprendiendo java, y tengo esta complicacion. tengo una varieble con un dato que quiero mostrar en otraclase que es de extencion frame, pero ...
  #1 (permalink)  
Antiguo 17/06/2010, 16:22
 
Fecha de Ingreso: junio-2010
Mensajes: 1
Antigüedad: 13 años, 10 meses
Puntos: 0
usar variable en otra clase - java

hola stoy aprendiendo java, y tengo esta complicacion. tengo una varieble con un dato que quiero mostrar en otraclase que es de extencion frame, pero no se como usarla o llamarla, este me entrega la uf del dia

Código:
public class Uf {

    public static void Ufdeldia(String[] args) {

      int i=1;
      int n=1;

      String uf=null;
      String palabra;

	try {

		      // Indicamos la URL donde nos conectamos
		      URL url = new URL("http://valoruf.cl/");

		      // Buffer con los datos recibidos
		      BufferedReader in = null;

		      try {
		        // Volcamos lo recibido al buffer
		        in = new BufferedReader(new InputStreamReader(url.openStream()));
		      } catch(Throwable t){}

		      // Transformamos el contenido del buffer a texto
		      String inputLine;
		      String inputText="";

		      // Mientras haya cosas en el buffer las volcamos a las
		      // cadenas de texto

		      while ((inputLine = in.readLine()) != null)
		      {

                        StringTokenizer stk =new  StringTokenizer (inputLine);
		        inputText = inputText +"\n"+ inputLine;

                        while (stk.hasMoreTokens())

                            {
                            palabra = stk.nextToken();
                            
//                            System.out.print(i+".");
//                            System.out.print(n);

if(i==58 & n==6)                       {
    uf=palabra;

                            System.out.println(palabra);
                                }
n=n+1;
                            }

i=i+1;
n=1;
		      }
		      // Mostramos el contenido y cerramos la entrada
		     // System.out.println("El contenido de la URL es: " + inputText);
		   //   in.close();

		    } catch (MalformedURLException me) {
		      System.out.println("URL erronea");
		    } catch (IOException ioe) {
		      System.out.println("Error IO");
		   }

       char arregloChar[] = new char[9];

      uf.getChars( 0, 9, arregloChar, 0 );
	     

//             for ( int cuenta = 0; cuenta < arregloChar.length; cuenta++ )

//             salida += arregloChar[ cuenta ];

             System.out.print(arregloChar);

	}

}

y esta es la frame que al precionar un botos quiero que me muestre el arreglo "arregloChar" en un texfile

Código:
public class Ventana extends javax.swing.JFrame {

    String palabra;
    /** Creates new form Ventana */
    public Ventana () {
        
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Uf del dia");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Noticia del dia");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(96, 96, 96)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

System.out.println();



        System.out.println("eeee");
//        jTextArea1.setText(palabra);
 jTextArea1.setText("eee" + palabra);

 

    }

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//
//    java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new Ventana().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration

}
me imagino que debe ser simple pero me he complicado un monton, por eso les agradesco su ayuda, saludos
  #2 (permalink)  
Antiguo 21/06/2010, 13:07
 
Fecha de Ingreso: enero-2005
Mensajes: 15
Antigüedad: 19 años, 3 meses
Puntos: 0
Respuesta: usar variable en otra clase - java

en la clase Uf la variable char arregloChar[] la haces global oculpta para mantener el encapsulamiento. entonces pones en debajo de public class Uf {

private char arregloChar[];

mas abajo en el codigo donde tenes : char arregloChar[] = new char[9];

dejas: arregloChar[] = new char[9];

agregas un nuevo metodo:

public char [] getArregloChar()
{

return arregloChar;
}

en la otra Clase en el metodo que necesitas el arreglo creas una variable del tipo Uf - dentro del Listener del boton (asi podes acceder al metodo este que acabamos de crear.) Y ahi llamas directamente al metodo de la otra clase, por ejmeplo creas una variable:
Uf uf=null;

char miNuevoArregloChar[] = uf.getArregloChar();

Etiquetas: clase, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:57.