自制的Java超链接按钮

package lib;

import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * 超链接按钮。
 * 
 * @author Elvis
 */
public class LinkButton extends JLabel {
 private static final long serialVersionUID = 1L;
 private String text;
 private Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);

 public LinkButton() {
  addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
    Runtime rt = Runtime.getRuntime();
    try {
     String cmd = "rundll32   url.dll,FileProtocolHandler   http://www.csdn.net";
     rt.exec(cmd);
    } catch (IOException e1) {
     e1.printStackTrace();
    }
    setClickedText();
   }

   public void mouseEntered(MouseEvent e) {
    setHandCursor();
   }
   
   public void mouseExited(MouseEvent e) {
    setDefaultCursor();
   }
  });
 }

 public void setText(String text) {
  String content = "<html><font color=blue><u>" + text
    + "</u></font></html>";
  this.text = text;
  super.setText(content);
 }

 private void setClickedText() {
  String content = "<html><font color=red><u>" + text
    + "</u></font></html>";
  super.setText(content);
 }

 private void setHandCursor() {
  this.setCursor(handCursor);
 }

 private void setDefaultCursor() {
  this.setCursor(null);
 }
 public static void main(String[] args) {
  JFrame f = new JFrame();
  f.setSize(400, 400);
  f.setLayout(new FlowLayout());
  LinkButton btn = new LinkButton();
  btn.setText("Heeloo");
  f.add(btn);
  f.setVisible(true);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}



你可能感兴趣的:(java,.net,swing,F#)