Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/03/2016, 12:48
oscaar90
 
Fecha de Ingreso: febrero-2015
Mensajes: 55
Antigüedad: 10 años, 1 mes
Puntos: 7
Acceder a URL URI

Buenas tardes,

Estoy iniciandome en programación Java, y me surge un problema con mi primer codigo.

Quiero realizar una ventana donde muestre un diagalo para insertar una direccion URL y al pulsar el boton "Acceder" que entre en la pagina web con el navegador.


Código Java:
Ver original
  1. import java.awt.Desktop;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.IOException;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JTextField;
  11. import javax.swing.WindowConstants;
  12.  
  13. public class nav {
  14.    
  15.     private static JFrame ven;
  16.     private static JButton but;
  17.     private static JTextField text1;
  18.  
  19.      public static void main(String[] args) throws Exception {
  20.          
  21.          
  22.             ven = new JFrame ("Acceder URL");          
  23.             ven.getContentPane().setLayout(new FlowLayout());  
  24.             but = new JButton("Acceder");
  25.             ven.getContentPane().add(but);
  26.             text1 = new JTextField(20);
  27.             ven.getContentPane().add(text1);
  28.             text1.setText("http://");
  29.            
  30.             String direccion = text1.getText();
  31.            
  32.             but.addActionListener(new ActionListener(){
  33.                
  34.                 public void actionPerformed(ActionEvent e) {
  35.  
  36.  
  37.                     if (Desktop.isDesktopSupported()) {
  38.                         // Windows
  39.                         try {
  40.                             Desktop.getDesktop().browse(new URI(direccion));
  41.                         } catch (IOException e1) {
  42.                             // TODO Auto-generated catch block
  43.                             e1.printStackTrace();
  44.                         } catch (URISyntaxException e1) {
  45.                             // TODO Auto-generated catch block
  46.                             e1.printStackTrace();
  47.                         }
  48.                     } else {
  49.                         // Ubuntu
  50.                         Runtime runtime = Runtime.getRuntime();
  51.                         try {
  52.                             runtime.exec("/usr/bin/firefox -new-window " + direccion);
  53.                         } catch (IOException e1) {
  54.                             // TODO Auto-generated catch block
  55.                             e1.printStackTrace();
  56.                         }
  57.                     }
  58.                    
  59.                 }
  60.             }
  61.            
  62.             );
  63.        
  64.         ven.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  65.        
  66.         ven.pack();
  67.        
  68.         ven.setVisible(true);
  69.     }
  70.   }

El problema creo que lo tengo en la linea
Código Java:
Ver original
  1. text1.setText("http://");

Ya que si indico directamente la url en text1, si que accede a la pagina web.
Es decir, no está retornando el valor de text1 que es donde indico la pagina web.

Espero me puedan ayudar.

Muchas gracias!