1、awc/swing实现超链接的自定义控件
public class HFLinkLabel extends JLabel { private static final long serialVersionUID = -4533897048399372238L; public String text, url; private boolean isSupported; public HFLinkLabel() { } public HFLinkLabel(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(HFLinkLabel.this.url)); }catch (Exception ex) { } } }); } public void setText(boolean b) { if (!b) { setText("<html><font color=blue><u>" + text); }else { setText("<html><font color=red><u>" + text); } } public void setLinkText(String text,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 HFLinkLabel("访问eRedLab", "http://hi.baidu.com/eRedLab")); jf.setContentPane(jp); jf.pack(); jf.setVisible(true); } }
标签:控件,自定义,isSupported,text,setText,void,超链接,new,public From: https://www.cnblogs.com/wwssgg/p/17025446.html