繁体   English   中英

垂直 JScrollPane 内的水平 JScrollPane

[英]Horizontal JScrollPane inside vertical JScrollPane

下午好。 有必要从事件列表(水平)中制作一个列表(垂直):至少有2个问题:

  1. 事件列表区域(水平滚动)扩展到面板边界之外。 显然,问题出在布局上,但我找不到正确的组合;
  2. 水平滚动不起作用(可能是由于上述问题),并且每组事件的滚动条必须不同。
import ivank.components.EventAdd;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class WindowAddCameras extends JFrame {
    public static final List<JPanel> labels = new ArrayList<JPanel>();
 
    public WindowAddCameras() {
        super("Добавить камеру");
 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panelButton = new JPanel();
 
        JButton addButton = new JButton("+");
        addButton.setFocusable(false);
        panelButton.add(addButton);
 
        JButton remButton = new JButton("-");
        remButton.setFocusable(false);
        panelButton.add(remButton);
 
        JPanel externalPanel = new JPanel();
        externalPanel.setLayout(new BorderLayout(0, 0));
        JScrollPane scrollPaneGroupEvent = new JScrollPane(
                externalPanel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
        );
 
        JPanel internalPanel = new JPanel();
        internalPanel.setLayout(new GridLayout(0, 1, 0, 0));
 
        JScrollPane scrollPaneEvent = new JScrollPane(internalPanel);
        scrollPaneEvent.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPaneEvent.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 
        externalPanel.add(scrollPaneEvent, BorderLayout.NORTH);
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int number = labels.size() + 1;
                EventAdd eventAdd = new EventAdd();
                Dimension labelSize = new Dimension(80, 80);
 
                //add event to group event
                Random rand = new Random();
                for(int a = 0; a < 20; a++) {
                    //random color border event for TEST
                    Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
                    eventAdd.createEventLabel("Камера " + number, labelSize, randomColor);
                }
 
                labels.add(eventAdd);
                internalPanel.add(eventAdd, BorderLayout.NORTH);
                scrollPaneGroupEvent.revalidate();
            }
        });
 
        remButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(labels.size() > 0) {
                    int index = labels.size() - 1;
                    JPanel panel = labels.remove(index);
                    internalPanel.remove(panel);
                    internalPanel.repaint();
                    scrollPaneGroupEvent.revalidate();
                }
            }
        });
 
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(panelButton, BorderLayout.NORTH);
        this.getContentPane().add(scrollPaneGroupEvent, BorderLayout.CENTER);
 
        this.setPreferredSize(new Dimension(600, 400));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}
package ivank.components;
 
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
 
public class EventAdd extends JPanel {
    public EventAdd() {
        super(new FlowLayout(FlowLayout.LEFT));
    }
 
    public JComponent createEventLabel(String name, Dimension labelSize, Color randomColor) {
        this.setBorder(BorderFactory.createTitledBorder(name));
        JLabel label = new JLabel();
        label.setPreferredSize(labelSize);
        label.setHorizontalAlignment(JLabel.CENTER);
 
        label.setBorder(BorderFactory.createLineBorder(randomColor, 5));
        this.add(label);
 
        return label;
    }
}

我所拥有的:在此处输入图像描述我想要获得的:在此处输入图像描述

JScrollPane 必须使用 jscrollpane 方法setViewportView分配一个 JViewport。 您还应该给它一个滚动条策略 setVerticalScrollBarPolicy setHorizontalScrollBarPolicy 该策略int是前两种方法的 class 的一部分,称为“常量字段值”,请参阅 JScrollPane 的 API 文档。 要将 JScrollPane 展开,请将其放入具有 BorderLayout 的 JPanel中,并且在具有 JScrollPane 的特定面板中没有其他元素。 在 JPanel 上使用方法 setMinimumSize 和 setPreferredSize 和 setSize 或主窗口中的 BoxLayout(虽然我认为主 windows 有一个特殊的布局,所以如果是这样,则不需要 boxlayout)或堆栈中的更高级别面板,或者只是 setPreferredSize对于 JScrollPane。 A jscrollpane has a view set by its " setView " method, its view is a java.awt.Component note: a JComponent is a java.awt.Component, any javax swing J panel or J viewport is a java.awt.Component by等级制度。 注意一个 jviewport 是一个 java.awt.Component 所以得到一个 jscrollpane.setViewportView 添加 jviewport 引用 jviewport.setView 作为 JPanel 与 BorderLayout,添加另一个面板开始添加其他组件,但记住给所有 Jcomponents a.preferredSize 方法使用. If you are after javax.swing.JList<E> and javax.swing.DefaultListModel<E> The JList must be placed in a JPanel with a BorderLayout, or much more difficult system a java.awt.GridBagLayout then in a jscrollpane.

您可以只用一个 JScrollPane 进行水平和垂直滚动。 但是您需要的是呈现您的内容的底层 JComponent。 然而,这可以是一个JPanel,它可以根据您的需要延伸。

看看如何使用 ScrollPanes

另一个滚动区域内的滚动区域是用户界面反模式(反设计?)。 应该避免。

我将创建一个基于垂直 BoxLayout 的可滚动面板:

public class WindowAddCameras extends JFrame {
    private static final long serialVersionUID = 1;

    public static final List<JPanel> labels = new ArrayList<JPanel>();

    private static class CameraListPanel
    extends JPanel
    implements Scrollable {
        private static final long serialVersionUID = 1;

        CameraListPanel() {
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return getPreferredSize();
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect,
                                              int orientation,
                                              int direction) {
            return getScrollableIncrement(30,
                visibleRect, orientation, direction);
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect,
                                               int orientation,
                                               int direction) {
            return getScrollableIncrement(
                orientation == SwingConstants.HORIZONTAL ?
                    getWidth() : getHeight(),
                visibleRect, orientation, direction);
        }

        private int getScrollableIncrement(int amount,
                                           Rectangle visibleRect,
                                           int orientation,
                                           int direction) {
            if (orientation == SwingConstants.HORIZONTAL) {
                return Math.min(amount, direction < 0 ? visibleRect.x :
                    getWidth() - (visibleRect.x + visibleRect.width));
            } else {
                return Math.min(amount, direction < 0 ? visibleRect.y :
                    getHeight() - (visibleRect.y + visibleRect.height));
            }
        }
    }
 
    public WindowAddCameras() {
        super("Добавить камеру");
 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        JPanel panelButton = new JPanel();
 
        JButton addButton = new JButton("+");
        addButton.setFocusable(false);
        panelButton.add(addButton);
 
        JButton remButton = new JButton("-");
        remButton.setFocusable(false);
        panelButton.add(remButton);

        JPanel camerasPanel = new CameraListPanel();
        JScrollPane scrollPaneGroupEvent = new JScrollPane(camerasPanel);
 
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int number = labels.size() + 1;
                EventAdd eventAdd = new EventAdd();
                Dimension labelSize = new Dimension(80, 80);
 
                //add event to group event
                Random rand = new Random();
                for(int a = 0; a < 20; a++) {
                    //random color border event for TEST
                    Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
                    eventAdd.createEventLabel("Камера " + number, labelSize, randomColor);
                }
 
                labels.add(eventAdd);
                camerasPanel.add(eventAdd);
                scrollPaneGroupEvent.revalidate();
            }
        });
 
        remButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(labels.size() > 0) {
                    int index = labels.size() - 1;
                    JPanel panel = labels.remove(index);
                    camerasPanel.remove(panel);
                    camerasPanel.repaint();
                    scrollPaneGroupEvent.revalidate();
                }
            }
        });
 
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(panelButton, BorderLayout.NORTH);
        this.getContentPane().add(scrollPaneGroupEvent, BorderLayout.CENTER);
 
        this.setPreferredSize(new Dimension(600, 400));
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new WindowAddCameras());
    }
}

CameraListPanel class 主要是一个基本的可滚动面板; 重要的部分是getScrollableTracksViewportWidth()返回 true,这将导致面板的宽度与任何 JScrollPane 视口的宽度相匹配。 这消除了对水平滚动条的任何需要。

当然,您会希望以某种方式显示所有子组件。 为此,我会让 EventAdd class 计算可以容纳所有孩子的高度:

public class EventAdd extends JPanel {
    private static final long serialVersionUID = 1;

    private final FlowLayout layout;

    public EventAdd() {
        layout = new FlowLayout(FlowLayout.LEFT);
        setLayout(layout);
    }

    @Override
    public Dimension getPreferredSize() {
        Rectangle childSize = new Rectangle();
        Component[] children = getComponents();
        for (Component child : children) {
            childSize.add(new Rectangle(child.getPreferredSize()));
        }

        Insets insets = getInsets();

        int hgap = layout.getHgap();
        int vgap = layout.getVgap();
        int childWidth = childSize.width + hgap;

        Dimension size;
        if (getParent() == null) {
            size = new Dimension(
                children.length * (childWidth * hgap) + hgap,
                childSize.height + vgap * 2);
        } else {
            int width = getParent().getWidth();
            width -= insets.left + insets.right;
            int childrenPerRow =
                childWidth == 0 ? 0 : (width - hgap) / childWidth;

            int rows;
            if (childrenPerRow == 0) {
                rows = 0;
            } else {
                rows = children.length / childrenPerRow;
                if (children.length % childrenPerRow > 0) {
                    rows++;
                }
            }
            size = new Dimension(width,
                vgap + rows * (childSize.height + vgap));
        }

        size.width += insets.left + insets.right;
        size.height += insets.top + insets.bottom;

        return size;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM