【发布时间】:2016-11-23 16:07:39
【问题描述】:
我有 16 个 Jpanel,当我将鼠标悬停在它们上方时,我想突出显示它们。我匿名创建了 JPanel,然后将它们添加到父级,并为它们中的每一个添加了一个 MouseListener。然后我向父级添加了一个 MouseListener。问题是,现在它只是突出了父级。我该如何解决这个问题?
注意: 有时 JFrame 不显示任何内容 - 您只需要继续运行它直到它显示(通常需要 2-3 次尝试)。如果在 >5 次尝试后仍然无法正常工作,请发表评论。
HighlightJPanels(创建 JFrame、容器和子级,并添加 MouseListener)
public class HighlightJPanels extends JFrame{
private static final long serialVersionUID = 7163215339973706671L;
private static final Dimension containerSize = new Dimension(640, 477);
private JLayeredPane layeredPane;
static JPanel container;
public HighlightJPanels() {
super("Highlight Test");
setSize(640, 477);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(containerSize);
getContentPane().add(layeredPane);
createContainer();
layeredPane.add(container, JLayeredPane.DEFAULT_LAYER);
createChildren(4, 4);
container.addMouseMotionListener(new HighlightJPanelsContainerMouseListener());
}
private void createChildren(int columns, int rows){
for (int i = 0; i < columns; i++){
for (int j = 0; j < rows; j++){
JPanel child = new JPanel(new BorderLayout());
child.setBackground(Color.LIGHT_GRAY);
child.addMouseListener(new HighlightJPanelsMouseListeners());
container.add(child);
}
}
}
private JPanel createContainer(){
container = new JPanel();
container.setLayout(createLayout(4, 4, 1, 1));
container.setPreferredSize(containerSize);
container.setBounds(0, 0, containerSize.width, containerSize.height);
return container;
}
private GridLayout createLayout(int rows, int columns, int hGap, int vGap){
GridLayout layout = new GridLayout(rows, columns);
layout.setHgap(hGap);
layout.setVgap(vGap);
return layout;
}
public static void main(String[] args) {
new HighlightJPanels();
}
}
HighlightJPanelsChildMouseListeners(创建将添加到子级的 MouseListeners)
public class HighlightJPanelsChildMouseListeners implements MouseListener{
private Border grayBorder = BorderFactory.createLineBorder(Color.DARK_GRAY);
public HighlightJPanelsChildMouseListeners() {
}
public void mouseEntered(MouseEvent e) {
Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
JPanel parent = (JPanel) comp;
parent.setBorder(grayBorder);
parent.revalidate();
}
public void mouseExited(MouseEvent e) {
Component comp = HighlightJPanels.container.findComponentAt(HighlightJPanelsContainerMouseListener.eX, HighlightJPanelsContainerMouseListener.eY);
JPanel parent = (JPanel) comp;
parent.setBorder(null);
parent.revalidate();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e) {}
}
HighlightJPanelsContainerMouseListener(创建将添加到容器中的 MouseListener)
public class HighlightJPanelsContainerMouseListener implements MouseMotionListener{
static int eX;
static int eY;
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
eX = e.getX();
eY = e.getY();
}
}
【问题讨论】:
-
我建议使用 MouseListener 而不是 MouseMotionListener 并实现方法
MouseEntered和MouseExited -
@ControlAltDel 我确实在 HighlightJPanelsChildMouseListeners 中这样做了,但如果我要在 HighlightJPanelsContainerMouseListener 中这样做,那么它只会突出显示父级。跨度>
标签: java mouseevent mouseover