这是我在这个平台上的第一个问题,因此请继续:
我正在使用Java Swing来制作LayerPanel,就像在Photoshop中一样,其中LayerPanel的每个子对象也是代表一层的Panel。就像在Photoshop中一样,我使用鼠标通过受以下因素启发的DraggableComponent类移动LayerPanel的这些子级:输入图像描述heregable-component>。
问题在于重新排列UI中的图层。我无法交换面板以在UI中工作。
到目前为止,这是我的代码:
DraggableComponent:
class DraggableComponent extends JPanel implements MouseListener, MouseMotionListener {
private LayerPanel panel;
private volatile int screenY = 0;
private volatile int myY = 0;
private String name;
public DraggableComponent(LayerPanel panel, String name) {
setPreferredSize(new Dimension(300, 80));
this.name = name;
this.panel = panel;
updateBorder(name);
setVisible(true);
setLayout(null);
addMouseListener(this);
addMouseMotionListener(this);
}
private void updateBorder(String name) {
setBorder(new TitledBorder(new DropShadowBorder(), name, TitledBorder.LEADING, TitledBorder.BELOW_TOP, null, null));
}
private void handleLocation(int y) {
if (y >= getParent().getHeight() - this.getHeight()) {
setLocation(getLocation().x, getParent().getHeight() - this.getHeight());
} else setLocation(getLocation().x, Math.max(y, 0));
}
@Override
public void mousePressed(MouseEvent e) {
screenY = e.getYOnScreen();
myY = getY();
}
@Override
public void mouseReleased(MouseEvent e) {
panel.handleReleased(this);
}
@Override
public void mouseDragged(MouseEvent e) {
int deltaY = e.getYOnScreen() - screenY;
handleLocation(myY + deltaY);
panel.handleLocation(this);
}
public void rename(String name) {
this.name = name;
updateBorder(name);
}
}
LayerPanel类:
public class LayerPanel extends JPanel {
private ArrayList<DraggableComponent> components = new ArrayList<>();
protected int HEIGHT_PER_COMPONENT;
public LayerPanel(int height, int amountOfComponents) {
setPreferredSize(new Dimension(200, height));
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
for (int i = 0; i < 10; i++) {
components.add(new DraggableComponent(this, Integer.toString(i)));
}
updateComponents();
}
public void addComponent(String name) {
components.add(new DraggableComponent(this, name));
updateComponents();
}
private void updateComponents() {
HEIGHT_PER_COMPONENT = getHeight() / (components.size() + 1);
removeAll();
for (DraggableComponent c : components) {
//c.rename(Integer.toString(components.indexOf(c)));
add(c);
c.setLocation(0, HEIGHT_PER_COMPONENT * components.indexOf(c));
}
}
public void handleReleased(DraggableComponent component) {
updateComponents();
}
public void handleLocation(DraggableComponent component) {
final int index = components.indexOf(component);
for (DraggableComponent nextComponent : components) {
final int indexNext = components.indexOf(nextComponent);
if (component.getY() >= nextComponent.getY() - HEIGHT_PER_COMPONENT / 2 && !component.equals(nextComponent)) {
Collections.swap(components, indexNext, indexNext - 1);
updateComponents();
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Layers test");
LayerPanel panel = new LayerPanel(800, 10);
f.add(panel);
f.setSize(300, 800);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
最佳答案
问题在于重新排列图层。
无需自己进行管理。
这由面板中组件的ZOrder
控制。
在您的mousePressed
事件中,您可以将组件的ZOrder
设置为0,以确保该组件最后被绘制,这意味着它现在将在面板上所有其他组件的顶部绘制。
就像是:
Container container = e.getComponent().getParent();
container.setComponentZOrder(e.getComponent(), 0);