在 JScrollPane 和自定义 LayoutManager 中带有 JComponents 的 JPanel

JPanel with JComponents inside a JScrollPane and custom LayoutManager

我写了一个 JComponent 的子class,它在 paintComponent 方法中绘制自己。在构造函数中,我使用 setBounds 设置边界。这很好用。我将此 subclass 添加到 JPanel。此 JPanel 位于 JScrollPane 内。此 JPanel 是一个 class subclassing JPanel 并实现了 MouseInputListener 和 KeyListener。因此,每当用户在 JPanel 中单击时,都会根据鼠标点检查每个 JComponent 的边界。如果匹配,焦点将设置到该 JComponent。如果焦点位于 JComponent 并且用户拖动鼠标,则 JComponent 将移动到 JPanel 内。我想要实现的是,每当 JComponent 在 JPanel 中的位置发生变化并离开 JScrollPane 的视口时,滚动条就会变得可见并且 JPanel 会调整大小,以便用户可以滚动到可视区域之外。为了实现这一点,我编写了一个自定义 LayoutManager,它不以任何方式布置 JComponents,而是以绝对位置布置。但是每当我在 JPanel 内部和可视区域外部移动 JComponent 时,滚动条都不会更新。我怎样才能做到这一点?

这里是 class subclassig JComponent:

public abstract class Gate extends JComponent {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public Gate() {
    super();
    setBounds(new Rectangle(0, 0, 100, 100));
    setBorder(BorderFactory.createLineBorder(Color.black));
    setFocusable(true);
    setOpaque(false);
    setBackground(Color.white);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(hasFocus()) {
        setBorder(BorderFactory.createLineBorder(Color.green));
    } else {
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    Graphics g2 = g.create();
    g2.setColor(getBackground());
    g2.fillRect(0, 0, getWidth(), getHeight());
    g2.dispose();
}
}

这是 class subclassing JPanel:

public class GatesPanel extends JPanel implements MouseInputListener, KeyListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public GatesPanel() {
    super();
    setLayout(new GateLayout());
    addMouseListener(this);
    addMouseMotionListener(this);
    addKeyListener(this);
    setFocusable(true);
}

@Override
public void mouseClicked(MouseEvent e) {
    if(e.getButton() == MouseEvent.BUTTON1) {
        for(int i = 0; i < getComponentCount(); i++) {
            Component c = getComponent(i);
            if(c instanceof JComponent) {
                JComponent j = (JComponent)c;
                if(j.getBounds().contains(e.getPoint())) {
                    if(j.hasFocus()) {
                        grabFocus();
                    } else {
                        j.grabFocus();
                    }
                }
            }
        }
    }
}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseDragged(MouseEvent e) {

}

@Override
public void mouseMoved(MouseEvent e) {
    for(int i = 0; i < getComponentCount(); i++) {
        Component c = getComponent(i);
        if(c.hasFocus()) {
            c.setLocation(e.getPoint());
        }
    }
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {

}
}

这里是 class 实施布局管理器:

public class GateLayout implements LayoutManager {

private int minWidth;
private int minHeight;
private int preferedWidth;
private int preferedHeight;

public GateLayout() {
    minWidth = 0;
    minHeight = 0;
    preferedWidth = 0;
    preferedHeight = 0;
}

@Override
public void addLayoutComponent(String name, Component component) {

}

@Override
public void layoutContainer(Container parent) { 
    setSize(parent);
}

private void setSize(Container parent) {
    minWidth = 0;
    minHeight = 0;
    preferedWidth = 0;
    preferedHeight = 0;
    for(int i = 0; i < parent.getComponentCount(); i++) {
        Component c = parent.getComponent(i);
        Dimension d = c.getPreferredSize();
        minWidth = Math.max(d.width, minWidth);
        minHeight = Math.max(d.height, minHeight);
        preferedWidth += d.width;
        preferedHeight += d.height;
    }
}

@Override
public Dimension minimumLayoutSize(Container parent) {
    setSize(parent);
    Insets insets = parent.getInsets();
    return new Dimension(minWidth + insets.left + insets.right,
                        minHeight + insets.top + insets.bottom);
}

@Override
public Dimension preferredLayoutSize(Container parent) {
    setSize(parent);
    Insets insets = parent.getInsets();
    return new Dimension(preferedWidth + insets.left + insets.right,
                         preferedHeight + insets.top + insets.bottom);
}

@Override
public void removeLayoutComponent(Component component) {

}

}

在名为 GatesFrame 的 JFrame 子class 中,我调用了以下代码:

gatesPanel = new GatesPanel();
JScrollPane scrollPane = new JScrollPane(gatesPanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setAutoscrolls(true);
setContentPane(scrollPane);

在此之后,我将一个名为 GateAND 的 Gate subclass 添加到 GatesPanel:

gatesPanel.add(new GateAND());

那么当用户在被滚动窗格包围的面板内移动组件时滚动条没有更新,我做错了什么?欢迎任何帮助。

加油,

maxpa1n87

To achive this I have written a custom LayoutManager which does not layout the JComponents in any way but the absolute position. But whenever I move a JComponent inside the JPanel and outside of the viewable area the scrollbars do not get updated.

可能您的布局管理器没有正确设置首选大小。当首选尺寸大于滚动窗格的尺寸时,滚动条将自动出现。

您还需要确保在完成拖动面板上调用的组件后 revalidate() 以便调用布局管理器。

查看为此目的设计的Drag Layout

这是一个简单的例子:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class DragLayoutTest
{
    public static void main( String[] args )
    {
        DragListener drag = new DragListener();

        DragLayout dl = new DragLayout();
        dl.setUsePreferredSize(false);

        JPanel panel = new JPanel( dl );
        panel.setBorder( new MatteBorder(10, 10, 10, 10, Color.YELLOW) );

        createLabel(drag, panel, "North", 150, 0);
        createLabel(drag, panel, "West", 0, 100);
        createLabel(drag, panel, "East", 300, 100);
        createLabel(drag, panel, "South", 150, 200);
        createLabel(drag, panel, "Center", 150, 100);

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame( "Drag Layout" );
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new JScrollPane(panel) );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void createLabel(MouseInputAdapter drag, JPanel panel, String text, int x, int y)
    {
        JLabel label = new JLabel( text );
        label.setOpaque(true);
        label.setBackground( Color.ORANGE );
        label.setLocation(x, y);
        panel.add( label );
        label.addMouseListener( drag );
        label.addMouseMotionListener( drag );
    }

    static class DragListener extends MouseInputAdapter
    {
        Point location;
        MouseEvent pressed;

        public void mousePressed(MouseEvent me)
        {
            pressed = me;
        }

        public void mouseDragged(MouseEvent me)
        {
            Component component = me.getComponent();
            location = component.getLocation(location);
            int x = location.x - pressed.getX() + me.getX();
            int y = location.y - pressed.getY() + me.getY();
            component.setLocation(x, y);
            ((JComonent)component.getParent()).revalidate();
         }
    }
}