具有可折叠 JPanel 的容器的首选布局管理器是什么

发布于 2024-11-27 23:14:42 字数 513 浏览 1 评论 0原文

我的代码有一个 JPanel,其中包含三个可折叠的 JPanel。外部 JPanel 使用 BoxLayout 垂直堆叠三个 JPanel。但是,当我折叠 JPanel 时,顶部的 JPanel 将始终扩展以填充该区域(即使我 setMaximumSize() 等),而我希望下部的 JPanel 向上扩展。一般都是有故障的。我正在查看 GridBagLayout,它是否更适合这种工作?

谢谢。

这是我最疯狂的梦想中的 VB 图像(标题为“垂直面板”的图像):

http://www.codeproject.com/KB/cpp/CollapsiblePanelVB.aspx

My code has a JPanel that contains three collapsible JPanels. The outer JPanel uses the BoxLayout to stack the three JPanels vertically. However, when I collapse a JPanel, the top JPanel will always expands to fill the region (even if I setMaximumSize() or such), whereas I want the lower JPanels to expand upward. It is generally glitchy. I was looking at the GridBagLayout, would that be more suitable for this sort of endeavor?

Thanks.

This is a VB image of what I dream about in my wildest dreams (images with title "Vertical Panels"):

http://www.codeproject.com/KB/cpp/CollapsiblePanelVB.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨死的猪 2024-12-04 23:14:42

我不知道什么是可折叠面板。它是否会一直塌陷到 0,或者是否有最小高度?

如果您将最大尺寸管理为始终等于首选尺寸,那么您应该能够使用 BoxLayout。只需确保您还使用:

panel.add( Box.createVerticalGlue() );

在面板底部,以允许胶水使用额外的空间。

我要花很长时间才能从我面前的 500 行垃圾中剪出一个可编译的片段。

这就是创建 SSCCE 并忘记垃圾代码的原因。您所需要的只是一个带有 3 个可折叠面板的面板。然后添加一个按钮来折叠面板,看看会发生什么。最好从演示代码开始,然后编写 500 行代码,然后发现它不起作用。

I don't know what a collapsable panel is. Does it collapse all the way to 0, or does is have a minimum height?

If you manage the maximum size to always equal the preferred size then you should be able to use a BoxLayout. Just make sure you also use:

panel.add( Box.createVerticalGlue() );

at the bottom of your panel to allow the extra space to be used by the glue.

It would take me forever to cut out a compilable snippet from the 500 lines of garbage sprawled out before me.

And that is the reason for creating a SSCCE and forgetting about your garbage code. All you need is a panel with 3 collapsable panels. Then you add a button to collapse the panel and see what happens. Its better to start with demo code then write 500 lines of code and find out it doesn't work.

烟燃烟灭 2024-12-04 23:14:42

我强烈建议 MigLayout。它非常强大并且非常容易使用。它也被广泛使用。

I strongly suggest MigLayout. It's very powerful and very easy to use. It's also widely used.

兰花执着 2024-12-04 23:14:42

或基于 GridBagLayout

在此处输入图像描述 在此处输入图像描述

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ExpansiblePanel {

    public static void main(String[] args) {
        CollapsablePanel cp = new CollapsablePanel("test", buildPanel());
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(cp));
        f.setPreferredSize(new Dimension(360, 200));
        f.setLocation(150, 150);
        f.pack();
        f.setVisible(true);
    }

    public static JPanel buildPanel() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 1, 2, 1);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        JPanel p1 = new JPanel(new GridBagLayout());
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        p1.add(new JButton("button 1"), gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        p1.add(new JButton("button 2"), gbc);
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        p1.add(new JButton("button 3"), gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        p1.add(new JButton("button 4"), gbc);
        p1.setBackground(Color.blue);
        return p1;
    }

    private ExpansiblePanel() {
    }
}

class CollapsablePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private boolean selected;
    private JPanel contentPanel_;
    private HeaderPanel headerPanel_;

    private class HeaderPanel extends JPanel implements MouseListener {

        private static final long serialVersionUID = 1L;
        private String text_;
        private Font font;
        private BufferedImage open, closed;
        final int OFFSET = 30, PAD = 5;

        HeaderPanel(String text) {
            addMouseListener(this);
            text_ = text;
            font = new Font("sans-serif", Font.PLAIN + Font.BOLD, 12);
            // setRequestFocusEnabled(true);
            setPreferredSize(new Dimension(200, 25));
            setBackground(Color.black);
            setForeground(Color.red);
            int w = getWidth();
            int h = getHeight();
            /*try {
            open = ImageIO.read(new File("images/arrow_down_mini.png"));
            closed = ImageIO.read(new File("images/arrow_right_mini.png"));
            } catch (IOException e) {
            e.printStackTrace();
            }*/
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int h = getHeight();
            /*if (selected)
            g2.drawImage(open, PAD, 0, h, h, this);
            else
            g2.drawImage(closed, PAD, 0, h, h, this);
             */ // Uncomment once you have your own images
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics(text_, frc);
            float height = lm.getAscent() + lm.getDescent();
            float x = OFFSET;
            float y = (h + height) / 2 - lm.getDescent();
            g2.drawString(text_, x, y);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            toggleSelection();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }
    }

    CollapsablePanel(String text, JPanel panel) {
        super(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(1, 3, 0, 3);
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        selected = false;
        headerPanel_ = new HeaderPanel(text);
        setBackground(Color.orange);
        contentPanel_ = panel;
        add(headerPanel_, gbc);
        add(contentPanel_, gbc);
        contentPanel_.setVisible(false);
        JLabel padding = new JLabel();
        gbc.weighty = 1.0;
        add(padding, gbc);
    }

    public void toggleSelection() {
        selected = !selected;
        if (contentPanel_.isShowing()) {
            contentPanel_.setVisible(false);
        } else {
            contentPanel_.setVisible(true);
        }
        revalidate();
        headerPanel_.repaint();
    }
}

or old classic based on GridBagLayout

enter image description here enter image description here

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class ExpansiblePanel {

    public static void main(String[] args) {
        CollapsablePanel cp = new CollapsablePanel("test", buildPanel());
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(cp));
        f.setPreferredSize(new Dimension(360, 200));
        f.setLocation(150, 150);
        f.pack();
        f.setVisible(true);
    }

    public static JPanel buildPanel() {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 1, 2, 1);
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        JPanel p1 = new JPanel(new GridBagLayout());
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        p1.add(new JButton("button 1"), gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        p1.add(new JButton("button 2"), gbc);
        gbc.gridwidth = GridBagConstraints.RELATIVE;
        p1.add(new JButton("button 3"), gbc);
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        p1.add(new JButton("button 4"), gbc);
        p1.setBackground(Color.blue);
        return p1;
    }

    private ExpansiblePanel() {
    }
}

class CollapsablePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private boolean selected;
    private JPanel contentPanel_;
    private HeaderPanel headerPanel_;

    private class HeaderPanel extends JPanel implements MouseListener {

        private static final long serialVersionUID = 1L;
        private String text_;
        private Font font;
        private BufferedImage open, closed;
        final int OFFSET = 30, PAD = 5;

        HeaderPanel(String text) {
            addMouseListener(this);
            text_ = text;
            font = new Font("sans-serif", Font.PLAIN + Font.BOLD, 12);
            // setRequestFocusEnabled(true);
            setPreferredSize(new Dimension(200, 25));
            setBackground(Color.black);
            setForeground(Color.red);
            int w = getWidth();
            int h = getHeight();
            /*try {
            open = ImageIO.read(new File("images/arrow_down_mini.png"));
            closed = ImageIO.read(new File("images/arrow_right_mini.png"));
            } catch (IOException e) {
            e.printStackTrace();
            }*/
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            int h = getHeight();
            /*if (selected)
            g2.drawImage(open, PAD, 0, h, h, this);
            else
            g2.drawImage(closed, PAD, 0, h, h, this);
             */ // Uncomment once you have your own images
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            LineMetrics lm = font.getLineMetrics(text_, frc);
            float height = lm.getAscent() + lm.getDescent();
            float x = OFFSET;
            float y = (h + height) / 2 - lm.getDescent();
            g2.drawString(text_, x, y);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            toggleSelection();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }
    }

    CollapsablePanel(String text, JPanel panel) {
        super(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(1, 3, 0, 3);
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        selected = false;
        headerPanel_ = new HeaderPanel(text);
        setBackground(Color.orange);
        contentPanel_ = panel;
        add(headerPanel_, gbc);
        add(contentPanel_, gbc);
        contentPanel_.setVisible(false);
        JLabel padding = new JLabel();
        gbc.weighty = 1.0;
        add(padding, gbc);
    }

    public void toggleSelection() {
        selected = !selected;
        if (contentPanel_.isShowing()) {
            contentPanel_.setVisible(false);
        } else {
            contentPanel_.setVisible(true);
        }
        revalidate();
        headerPanel_.repaint();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文