在一个panel上添加label,给label添加链接。
鼠标移到上面时显示为蓝色,否则就显示为黑色。
现在基本通过。点击也可以打开网页。
但有个小bug,发现鼠标上下through字符时,颜色变化很好。
左右through时,发现就不行了。
谁能帮我看看,我实在不知道哪个地方导致的问题。
谢谢各位!!
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import java.text.*;
public class MouseMove{
public static void main(String[] args){
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/*
* JFrame
* @author yung
*/
class MouseFrame extends JFrame{
public MouseFrame(){
setTitle("MouseTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
MousePanel panel = new MousePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
/*
* JPanel
* @author yung
*/
class MousePanel extends JPanel{
public MousePanel(){
label = new HLabel(message,url);
label.setFont(new Font("Simsun",20,20));
add(label,BorderLayout.CENTER);
}
private HLabel label;
private String message="Hello Bupt";
private String url="http://www.bupt.edu.cn";
}
/*
* HLabel
* @author yung
*/
class HLabel extends JLabel{
private String url; //链接网址
private Rectangle bounds; //字符区域
boolean flag = true; //判断鼠标在字符区域内的标志
/*
* constructor
*/
public HLabel(String text,String uurl){
super(text);
url=uurl;
setHorizontalAlignment(SwingConstants.CENTER);
bounds=new Rectangle();
}
//当点击链接时打开网页
public void openIt() {
try {
Runtime.getRuntime().exec("mozilla "+url);
}catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
//当鼠标移到链接上字体变为蓝色,然后重绘,鼠标变为手形
public void blue(){
flag = false;
repaint();
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
//鼠标在其他地方,鼠标为默认。
public void black(){
flag = true;
repaint();
setCursor(Cursor.getDefaultCursor());
}
//paint
public void paint(Graphics g) {
super.paintBorder(g);//括号里是g,不知怎么显示不出来
Graphics2D g2=(Graphics2D)g;
//获取label上字符属性
Font f=getFont();
AttributedString ats=new AttributedString(this.getText());
ats.addAttribute(TextAttribute.FONT, f);
ats.addAttribute(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE_ON);
AttributedCharacterIterator iter=ats.getIterator();
FontRenderContext frc=g2.getFontRenderContext();
TextLayout tl=new TextLayout(iter, frc);
//重绘时根据flag来判断是变为蓝色还是黑色
if(flag==true){
g2.setColor(Color.black);
}else{
g2.setColor(Color.BLUE);
}
//获取字符区域
Rectangle rect=tl.getBounds().getBounds();
Rectangle total=this.getBounds();
bounds.x=(total.width-rect.width)/2;
bounds.y=(total.height-rect.height)/2;
bounds.width=rect.width;
bounds.height=rect.height;
tl.draw(g2,bounds.x,bounds.y+rect.height);
//添加监听器
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
//查找鼠标所在点是否在字符的区域内
public Rectangle2D find(Point2D p){
if (bounds.contains(p)) return bounds; //括号里是p,也没显示出来
return null;
}
//鼠标点击的事件处理
private class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent event){
if(SwingUtilities.isLeftMouseButton(event)) {
if(event.getClickCount()==1) {
if(find(event.getPoint())!=null) {
openIt();
}
}
}
}
}
//鼠标移动的事件处理
private class MouseMotionHandler implements MouseMotionListener{
public void mouseMoved(MouseEvent event){
if (find(event.getPoint()) == null){
black(); //调用默认鼠标方法
}
else{
blue(); //调用显示链接为蓝色方法
}
}
public void mouseDragged(MouseEvent event){
}
}
}