Swing 实现超链接 打开网页

本文介绍如何在Java中实现超链接跳转和通过命令行执行操作的示例代码,包括使用JLabel作为超链接和Runtime.exec方法在cmd中执行命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建一个超链接的JLabel,// 主要是使用下面方法,仅适用于JdK1.6及以上版本
  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().isSupported(Desktop.Action.BROWSE);
        } catch (Exception e) {
            this.isSupported = false;
        }
        setText(false);
        addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                setText(isSupported);
                if (isSupported)
                    setCursor(new Cursor(Cursor.HAND_CURSOR));
            }

            public void mouseExited(MouseEvent e) {
                setText(false);
            }

            public void mouseClicked(MouseEvent e) {
                try {
                    Desktop.getDesktop().browse(
                            new java.net.URI(LinkLabel.this.url));
                } catch (Exception ex) {
                }
            }
        });
    }

    private void setText(boolean b) {
        if (!b)
            setText("<html><font color=blue><u>" + text);
        else
            setText("<html><font color=red><u>" + text);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame("一个超链接实现的例子");
        JPanel jp = new JPanel();
        jp.add(new LinkLabel("访问eRedLab", "http://hi.baidu.com/eRedLab"));
        jf.setContentPane(jp);
        jf.pack();
        jf.setVisible(true);
    }
}

2、 使用 Runtime.getRuntime().exec 在cmd中执行 start http://www.baidu.com 

 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("百度搜索");
  mainFrame.getContentPane().add(simpleButton);
  simpleButton.addActionListener(new ActionListener()
  {// 添加动作侦听器,当按钮被按下时执行这里的代码以打开网页
     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();
 }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值