DOMDocument.addEventListener("mousemove", new EventListener() {
public void handleEvent(Event evt) {
System.out.println(evt.getType().toString());
final DOMElement targetElement = (DOMElement) browserDocument.getElementfromPixelPosition(mpl.screenX, mpl.screenY);
targetElement.setAttribute("style", "border:1px solid red;");
targetElement.addEventListener("mouseout", new EventListener() {
public void handleEvent(Event evt) {
System.out.println(evt.getType().toString());
targetElement.setAttribute("style", "border:none;");
}
}, false);
}
}, false);
我想要一个红色边框来突出显示鼠标悬停时的元素。在mouseout上,我希望它消失。
如何解决?我写的代码效果不好。
答案 0 :(得分:0)
这是一个说明你问的内容的例子
此示例将边框应用于JPanel
,您可以对其进行修改以使用dom element
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class HighlightBorder extends MouseAdapter {
JPanel p;
LineBorder border;
public HighlightBorder() {
JFrame j = new JFrame();
p = new JPanel();
border = new LineBorder(Color.red, 2);
j.getContentPane().add(p);
j.setSize(400, 300);
j.setVisible(true);
p.addMouseListener(this);
}
public void mouseEntered(MouseEvent e) {
p.setBorder(border);
}
public void mouseExited(MouseEvent e) {
p.setBorder(null);
}
public static void main(String[] args) {
new HighlightBorder();
}
}