2011-12-29 8 views
9

मुझे जावा स्विंग के साथ एक लेबल बनाने की आवश्यकता है जो क्लिक करने योग्य है और डेस्कटॉप पर डिफ़ॉल्ट ब्राउज़र खोलने और इसे एक विशिष्ट यूआरएल पर रीडायरेक्ट करने में सक्षम है। मेरा कोड ब्राउज़र खोलने में सक्षम है लेकिन इसे सही यूआरएल पर रीडायरेक्ट नहीं कर रहा है (डिफ़ॉल्ट होम पेज लोड हो गया है)। मेरे परीक्षण कोड:सही यूआरएल पर ब्राउज़र खोलने के लिए जेएलएबल हाइपरलिंक

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(); 
} 
} 

कैसे मैं एक डिफ़ॉल्ट ब्राउज़र खुला है और जावा स्विंग के साथ सही URL पर रीडायरेक्ट कर सकते हैं?

+1

क्या ओएस और ब्राउज़र? कोई अपवाद? –

+0

यह डिफ़ॉल्ट ब्राउज़र के रूप में देर से मॉडल 1.6 जेआरई और एफएफ का उपयोग कर विन 7 पर काम करता है। –

+0

क्षमा करें मैं ओएस/ब्राउज़र का उल्लेख करना भूल गया: उबंटू 11.10 + क्रोम 16 + जावा 6 – Randomize

उत्तर

0

समस्या मिली: उबंटू 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

विंडोज उन्मुख? – Randomize

1

Here आपको क्या चाहिए के लिए एक कोड नमूना है।

+0

विंडोज उन्मुख दिखता है? – Randomize

1

आपका कोड मेरे डिफ़ॉल्ट ब्राउज़र के रूप में सफारी के साथ मैक पर चलने के लिए ठीक काम करता है।

आप किस ब्राउज़र का उपयोग कर रहे हैं और आप किस ओएस पर चल रहे हैं?

+0

उबंटू 11.10 + क्रोम 16 + जावा 6 – Randomize

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

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 
        } 
      } 
     }); 
    }