放置位置渲染

这是一个更高级的主题,大多数人无需担心。但是,如果您有一个自定义组件,则需要处理自己呈现的放置位置。

dropLocation属性更改时,您可以注册以接收通知。您将侦听此更改,并使用getDropLocation方法在组件的自定义渲染器或paintComponent方法中对放置位置进行渲染。

这是一个监听dropLocation属性的示例:

class Repainter extends PropertyChangeListener {
    public void propertyChange(PropertyChangeEvent pce) {
        repaintDropLocation(pce.getOldValue());
        repaintDropLocation(pce.getNewValue());
    }
}

comp.addPropertyChangeListener("dropLocation", newRepainter());

这是paintComponent方法的示例:

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    DropLocation loc= getDropLocation();
    if (loc == null) {
        return;
    }

    renderPrettyIndicatorAt(loc);
}