我有16個Jpanels,當我將鼠標懸停在他們上面時,我想突出顯示。我創建了匿名的JPanel,然後將它們添加到父項,併爲它們中的每一個添加了一個MouseListener。然後我添加了一個MouseListener給父級。事情是,現在它突出了父母。我怎樣才能解決這個問題?如何突出顯示鼠標懸停時的JPanel?
注意:有時JFrame不顯示任何東西 - 你只需要繼續運行它,直到它(通常需要2-3次嘗試)。評論> 5次嘗試後它是否還沒有工作。
HighlightJPanels(創建的JFrame,容器,和孩子,並且將所MouseListeners)
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();
}
}
我建議在MouseMotionListener上使用MouseListener並實現MouseEntered和MouseExited方法 – ControlAltDel
@ControlAltDel我在** HighlightJPanelsChildMouseListeners **中這樣做了,但是如果我在** HighlightJPanelsContainerMouseListener **中這麼做的話,那麼它會突出顯示父母。 –