使用滚动条动态显示面板的布局

发布于 2024-12-13 03:04:41 字数 281 浏览 2 评论 0原文

在java中,我一直在尝试创建一个可以接受带有滚动条的其他面板的面板。

我尝试使用网格布局,这工作得很好,除了如果我只添加几个面板,它会增长这些面板以适应父面板的大小。

我尝试使用 flowlayout,但这使得面板水平流动,因为有滚动条。

我该如何制作才能将面板添加到从顶部开始的父面板,并使它们始终具有相同的尺寸(或其首选尺寸)。

此外,当我在事件发生后将面板添加到父面板时,直到我移动或调整表单大小后,它们才会出现。我该如何让它重新喷漆?对其调用 repaint() 不起作用。

In java, I have been trying to create a panel that can accept other panels with a scroll bar.

I tried using gridlayout, and this works fine, except for the fact that if I only add a few panels, it grows those panels to fit the size of the parent panel.

I tried using flowlayout, but this makes the panels flow horizontally as there is a scroll bar.

How do I make it so I can add panels to the parent panel starting at the top and make them always the same size(or their preferred size).

Also, when I add panels to the parent panel after an event, they do not appear until after I move or resize the form. How do I make it repaint? calling repaint() on it did not work.

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

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

发布评论

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

评论(2

时常饿 2024-12-20 03:04:41

约束网格

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

/** This lays out components in a column that is constrained to the
top of an area, like the entries in a list or table.  It uses a GridLayout
for the main components, thus ensuring they are each of the same size.
For variable height components, a BoxLayout would be better. */
class ConstrainedGrid {

    ConstrainedGrid() {
        final JPanel gui = new JPanel(new BorderLayout(5,5));
        gui.setBorder(new EmptyBorder(3,3,3,3));
        gui.setBackground(Color.red);

        JPanel scrollPanel = new JPanel(new BorderLayout(2,2));
        scrollPanel.setBackground(Color.green);
        scrollPanel.add(new JLabel("Center"), BorderLayout.CENTER);
        gui.add(new JScrollPane(scrollPanel), BorderLayout.CENTER);

        final JPanel componentPanel = new JPanel(new GridLayout(0,1,3,3));
        componentPanel.setBackground(Color.orange);
        scrollPanel.add(componentPanel, BorderLayout.NORTH);

        JButton add = new JButton("Add");
        gui.add(add, BorderLayout.NORTH);
        add.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                componentPanel.add(new JTextField());
                gui.validate();
            }
        });

        Dimension d = gui.getPreferredSize();
        d = new Dimension(d.width, d.height+100);
        gui.setPreferredSize(d);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ConstrainedGrid cg = new ConstrainedGrid();
            }
        });
    }
}

Constrained Grid

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

/** This lays out components in a column that is constrained to the
top of an area, like the entries in a list or table.  It uses a GridLayout
for the main components, thus ensuring they are each of the same size.
For variable height components, a BoxLayout would be better. */
class ConstrainedGrid {

    ConstrainedGrid() {
        final JPanel gui = new JPanel(new BorderLayout(5,5));
        gui.setBorder(new EmptyBorder(3,3,3,3));
        gui.setBackground(Color.red);

        JPanel scrollPanel = new JPanel(new BorderLayout(2,2));
        scrollPanel.setBackground(Color.green);
        scrollPanel.add(new JLabel("Center"), BorderLayout.CENTER);
        gui.add(new JScrollPane(scrollPanel), BorderLayout.CENTER);

        final JPanel componentPanel = new JPanel(new GridLayout(0,1,3,3));
        componentPanel.setBackground(Color.orange);
        scrollPanel.add(componentPanel, BorderLayout.NORTH);

        JButton add = new JButton("Add");
        gui.add(add, BorderLayout.NORTH);
        add.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                componentPanel.add(new JTextField());
                gui.validate();
            }
        });

        Dimension d = gui.getPreferredSize();
        d = new Dimension(d.width, d.height+100);
        gui.setPreferredSize(d);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ConstrainedGrid cg = new ConstrainedGrid();
            }
        });
    }
}
蓦然回首 2024-12-20 03:04:41

假设 JScrollPane ,请参阅调整滚动大小窗格。为了方便起见,可滚动客户端,例如 JTable 优惠 setPreferredScrollableViewportSize( ),但您始终可以显式设置视口的大小。

Assuming JScrollPane, see Sizing a Scroll Pane. For convenience, Scrollable clients such as JTable offer setPreferredScrollableViewportSize(), but you can always set the viewport's size explicitly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文