Swing Implementation Hyperlink Open Web page

Source: Internet
Author: User

1, create a hyperlink jlabel,//mainly use the following methods, only applicable to JdK1.6 and above version
Desktop.getdesktop (). Browse (
New URL ("http://www.baidu.com"). Touri ());

Import Java.awt.Cursor;
Import Java.awt.Desktop;
Import Java.awt.event.MouseAdapter;

Import java.awt.event.MouseEvent;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;

Import Javax.swing.JPanel;
    public class LinkLabel extends JLabel {private String text, url;

    Private Boolean issupported;
        Public LinkLabel (string text, string url) {this.text = text;
        This.url = URL; try {this.issupported = desktop.isdesktopsupported () && desktop.getdesktop (). issu
        Pported (Desktop.Action.BROWSE);
        catch (Exception e) {this.issupported = false;
        } setText (FALSE); Addmouselistener (New Mouseadapter () {public void mouseentered (MouseEvent e) {SetText (Issuppo
                rted);
            if (issupported) setcursor (new Cursor (cursor.hand_cursor)); public void mouseexited (MouseEvent e) {SetText (false); public void mouseclicked (MouseEvent e) {try {desktop.getdesktop (). Bro
                WSE (new Java.net.URI (LinkLabel.this.url));
    The catch (Exception ex) {}}}); } private void SetText (Boolean b) {if (!b) setText (" 

2, the use of Runtime.getruntime (). exec executes start http://www.baidu.com in cmd

Import java.awt.*;
Import javax.swing.*;
Import java.awt.event.*;
Import java.io.*;
Class Jbuttondemo
{
 JFrame mainFrame;
 JButton SimpleButton;
 Public Jbuttondemo ()
 {
  mainFrame = new JFrame ("Jbuttondemo");
  SimpleButton = new JButton ("Baidu Search");
  Mainframe.getcontentpane (). Add (SimpleButton);
  Simplebutton.addactionlistener (New ActionListener ()
  {///Add action Listener, execute code here when button is pressed to open web page public
     void actionperformed (ActionEvent e)
     {
      try
      {
       runtime.getruntime ()
         . EXEC ("cmd/c start http:// Www.baidu.com ");
      } catch (IOException ee)
      {
       ee.printstacktrace ();}}}
    );
  Simplebutton.setcursor (New Cursor (Cursor.hand_cursor));
  Mainframe.setdefaultcloseoperation (jframe.exit_on_close);
  Mainframe.pack ();
  Mainframe.setlocationrelativeto (null);
  Mainframe.setvisible (true);
 public static void Main (string[] args)
 {
  new Jbuttondemo ();
 }
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.