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