布局管理器和滚动窗格的一些问题,不显示对象

发布于 2024-12-10 04:37:04 字数 850 浏览 1 评论 0原文

我的应用程序遇到问题。我希望动态创建几个“选项卡式”滚动窗格,其中包含半大量称为“ImageLabels”的对象,这些对象只是带有(您猜对了)图像的标签。

我的大部分 gui 都是在 Netbeans 中制作的,因为它是半复杂的,而且我自己也不太懂。

我的问题是这样的:当添加滚动窗格对象(包含带有 GridLayout 的 JPanel)时,以及在尝试将图像标签添加到带有布局的 JPanel 后(我的构造函数设置为“new GridLayout(0, 5, 5, 5);" 无限行、5 列、5 pxls 空间。)没有显示任何内容,滚动面板也不会进入“滚动”模式。

我完全不知所措,一整天都在尝试不同的布局。这是我的一些代码...

ImageLabel 对象的构造函数..

addMouseListener(this);
setVisible(true);
setPreferredSize(new Dimension(32, 32));

我如何设置我的tileHolderPanel JPanel(包含ImageLabels的面板)

myLayout = new GridLayout(0, 5, 5, 5);
tileHolderPanel.setLayout(myLayout);

,并在构造完我希望添加到每个面板的所有ImageLabels之后,我只需做

tileHolderPanel.add(label);

如果重要的话,我的 TileHolderPanel 位于 JPanel 内的滚动窗格内,并且它们具有 Netbeans 生成的默认 GroupLayouts。

谢谢你, -卢克

I'm having an issue in my application. I wish to dynamically create several "tabbed" scrollpanes that hold a semi-large amount of objects called "ImageLabels", which are just labels with (you guessed it) images on them.

I made most of my gui in Netbeans, as it is semi-complicated and I'm too nooby to do it myself.

My problem is this: When adding my scrollpane objects (that contain a JPanel with a GridLayout), and after I attempt to add my imagelabels to the JPanel with the layout (my constructor is set to "new GridLayout(0, 5, 5, 5);" which is unlimited rows, 5 columns, with 5 pxls of space.) nothing shows up, nor does the scrollpanel go into "scroll" mode.

I am completely at a loss and have been trying all day with different layouts. Here's some code I have...

Constructor of the ImageLabel object..

addMouseListener(this);
setVisible(true);
setPreferredSize(new Dimension(32, 32));

How I set up my tileHolderPanel JPanel (the panel holding the ImageLabels)

myLayout = new GridLayout(0, 5, 5, 5);
tileHolderPanel.setLayout(myLayout);

and after constructing all of the ImageLabels I wish to add to each panel, I simply do

tileHolderPanel.add(label);

If it matters, my TileHolderPanel is inside of a scrollpane which is inside a JPanel,and they have the default GroupLayouts which Netbeans generates.

Thank you,
-Luke

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

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

发布评论

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

评论(2

初相遇 2024-12-17 04:37:04

这是一个可用于比较您的代码的工作示例。

在此处输入图像描述

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/** @see http://stackoverflow.com/questions/7801870 */
public class ScrollGroup extends JPanel {

    private static final int N = 8;
    private static final int NN = N * N;
    private static final int GAP = 5;
    private static final int SIZE = 32;

    public ScrollGroup() {
        this.setLayout(new GridLayout(N, N, GAP, GAP));
        for (int i = 0; i < NN; i++) {
            JLabel label = new JLabel();
            label.setOpaque(true);
            label.setBackground(Color.getHSBColor((float) i / NN, 1, 1));
            label.setPreferredSize(new Dimension(SIZE, SIZE));
            this.add(label);
        }
    }

    private void display() {
        JFrame f = new JFrame("ScrollGroup");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane sp = new JScrollPane(this);
        GroupLayout layout = new GroupLayout(f.getContentPane());
        f.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup().addComponent(sp)));
        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup().addComponent(sp)));
        f.pack();
        f.setSize(N * SIZE, N * SIZE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ScrollGroup().display();
            }
        });
    }
}

Here's a working example with which to compare your code.

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/** @see http://stackoverflow.com/questions/7801870 */
public class ScrollGroup extends JPanel {

    private static final int N = 8;
    private static final int NN = N * N;
    private static final int GAP = 5;
    private static final int SIZE = 32;

    public ScrollGroup() {
        this.setLayout(new GridLayout(N, N, GAP, GAP));
        for (int i = 0; i < NN; i++) {
            JLabel label = new JLabel();
            label.setOpaque(true);
            label.setBackground(Color.getHSBColor((float) i / NN, 1, 1));
            label.setPreferredSize(new Dimension(SIZE, SIZE));
            this.add(label);
        }
    }

    private void display() {
        JFrame f = new JFrame("ScrollGroup");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane sp = new JScrollPane(this);
        GroupLayout layout = new GroupLayout(f.getContentPane());
        f.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup().addComponent(sp)));
        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup().addComponent(sp)));
        f.pack();
        f.setSize(N * SIZE, N * SIZE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ScrollGroup().display();
            }
        });
    }
}
¢蛋碎的人ぎ生 2024-12-17 04:37:04

我希望动态创建...

当您将组件添加到可见 GUI 时,基本代码应该是:

panel.add(...);
panel.revalidate();

如果您需要更多帮助,那么您需要发布 SSCCE

I wish to dynamically create ...

When you add components to a visible GUI the basic code should be:

panel.add(...);
panel.revalidate();

If you need more help then you need to post an SSCCE.

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