gpt4 book ai didi

java - 垂直 View 中的可拖动组件 (Java Swing)

转载 作者:行者123 更新时间:2023-12-02 09:12:26 24 4
gpt4 key购买 nike

这是我在这个平台上的第一个问题,所以请耐心等待:

我正在使用 Java Swing 制作一个 LayerPanel,就像在 Photoshop 中一样,其中 LayerPanel 的每个子级也是代表一个图层的面板。就像在 Photoshop 中一样,我使用鼠标通过 DraggableComponent 类移动 LayerPanel 的这些子级,其灵感来自于:在此处输入图像描述regable-component>。

问题在于重新排列 UI 中的图层。我无法在 UI 中交换面板。

这是我迄今为止的代码:

可拖动组件:

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);
}
}

最佳答案

The problem lies in re-arranging the layers.

无需自己管理。

这由面板中组件的 ZOrder 控制。

在您的 mousePressed 事件中,您可以将组件的 ZOrder 设置为 0,以确保该组件最后绘制,这意味着它现在将绘制面板上的所有其他组件。

类似于:

Container container = e.getComponent().getParent();
container.setComponentZOrder(e.getComponent(), 0);

关于java - 垂直 View 中的可拖动组件 (Java Swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59302300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com