2011-12-29 106 views
9

我需要創建一個帶有Java Swing的標籤,它是可點擊的,並且能夠打開桌面上的默認瀏覽器並將其重定向到特定的URL。我的代碼能夠打開瀏覽器,但不能將其重定向到正確的URL(默認主頁已加載)。我的測試代碼:JLabel超鏈接在正確的URL下打開瀏覽器

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.IOException; 
import java.net.*; 

public class LinkTest extends JFrame { 

public LinkTest() { 
JPanel p = new JPanel(); 

JLabel link = new JLabel("Click here"); 
link.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 
link.addMouseListener(new MouseAdapter() { 
    public void mouseClicked(MouseEvent e) { 
     if (e.getClickCount() > 0) { 
      if (Desktop.isDesktopSupported()) { 
       Desktop desktop = Desktop.getDesktop(); 
       try { 
        URI uri = new URI("http://www.bbc.co.uk"); 
        desktop.browse(uri); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } catch (URISyntaxException ex) { 
        ex.printStackTrace(); 
       } 
     } 
     } 
    } 
}); 
p.add(link); 
getContentPane().add(BorderLayout.NORTH, p); 
} 

public static void main(String[] args) { 
    LinkTest linkTest = new LinkTest(); 
    linkTest.setSize(640,100); 
    linkTest.show(); 
} 
} 

如何使用Java Swing打開默認瀏覽器並重定向到正確的URL?

+1

什麼操作系統和瀏覽器?任何例外? – 2011-12-29 15:05:03

+0

它在Win 7上運行,使用後期的1.6 JRE&FF作爲默認瀏覽器。 – 2011-12-29 15:20:46

+0

抱歉,我忘了提及操作系統/瀏覽器:Ubuntu 11.10 + Chrome 16 + Java 6 – Randomize 2011-12-29 16:01:43

回答

0

發現問題:在Ubuntu 12.10上我已經安裝了「libgnome2」,它現在工作正常。

1
public void mouseClicked(MouseEvent e) { 
     if (e.getClickCount() > 0) { 
      if (Desktop.isDesktopSupported()) { 
           try { 
        String osName = System.getProperty("os.name"); 
        String urlPath = "http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html"; 

        if (osName.startsWith("Windows")) 
         Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + urlPath); 
        else { 
         String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; 
         String browser = null; 
         for (int count = 0; count < browsers.length && browser == null; count++) 
          if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) 
           browser = browsers[count]; 
         Runtime.getRuntime().exec(new String[] { browser, urlPath }); 
        } 
       } 
       catch (Exception e) { 
        JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage()); 
       } 
     } 
     } 
    } 
}); 
+1

面向Windows? – Randomize 2011-12-29 16:04:59

1

Here是您需要的代碼示例。

+0

看起來面向Windows? – Randomize 2011-12-29 16:04:40

1

你的代碼對我來說在我的默認瀏覽器上用Safari瀏覽器運行時效果很好。

您使用的瀏覽器是什麼,以及您在運行什麼操作系統?

+0

Ubuntu 11.10 + Chrome 16 + Java 6 – Randomize 2011-12-29 16:02:05

1

這似乎是工作,Here是一個偉大的選擇值得嘗試麥克道爾,用一個JButton,

public static void main(String[] args) throws URISyntaxException { 
    final URI uri = new URI("http://java.sun.com"); 
    class OpenUrlAction implements ActionListener { 
     @Override public void actionPerformed(ActionEvent e) { 
     open(uri); 
     } 
    } 
    JFrame frame = new JFrame("Links"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(100, 400); 
    Container container = frame.getContentPane(); 
    container.setLayout(new GridBagLayout()); 
    JButton button = new JButton(); 
    button.setText("<HTML>Click the <FONT color=\"#000099\"><U>link</U></FONT>" 
     + " to go to the Java website.</HTML>"); 
    button.setHorizontalAlignment(SwingConstants.LEFT); 
    button.setBorderPainted(false); 
    button.setOpaque(false); 
    button.setBackground(Color.WHITE); 
    button.setToolTipText(uri.toString()); 
    button.addActionListener(new OpenUrlAction()); 
    container.add(button); 
    frame.setVisible(true); 
    } 

    private static void open(URI uri) { 
    if (Desktop.isDesktopSupported()) { 
     try { 
     Desktop.getDesktop().browse(uri); 
     } catch (IOException e) { /* TODO: error handling */ } 
    } else { /* TODO: error handling */ } 
    } 
+0

謝謝,但它不適合我。 – Randomize 2011-12-29 16:05:23

15

簡單,只是這種方法複製到你的代碼正確的參數。不要忘記添加所需的進口。

import java.awt.Cursor; 
import java.awt.Desktop; 
import java.awt.EventQueue; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.IOException; 
import java.net.URI; 
import java.net.URISyntaxException; 


    private void goWebsite(JLabel website, final String url, String text) { 
     website.setText("<html> Website : <a href=\"\">"+text+"</a></html>"); 
     website.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
     website.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
        try { 
          Desktop.getDesktop().browse(new URI(url)); 
        } catch (URISyntaxException | IOException ex) { 
          //It looks like there's a problem 
        } 
      } 
     }); 
    } 
相關問題