如何可视化缺失的内面板?

发布于 2024-12-11 01:11:19 字数 1070 浏览 0 评论 0原文

我正在尝试形成以下框架:

主框架使用 BorderLayout。

在此框架中,我添加了一个使用 BoxLayout 的面板 - 我将其称为 P1。

由于某种原因,当我运行程序时看不到我添加到 P1 中的面板。

更令人困惑的是,如果 P1 使用 GridLayout 而不是 BoxLayout,则会显示我添加到 P1 中的所有面板。

代码中使用的 EventPanel 类扩展了 Panel 并使用 SpringLayout。

有什么想法可以让 P1 发挥作用吗?

这是相关代码:

    public static void main(String[] args) 
{
        //Setting Frame
        JFrame m_CalendarFrame = new JFrame();
        m_CalendarFrame.getContentPane().setLayout(new BorderLayout());

        //Setting inner Panel
        JPanel P1 = new JPanel();
        P1.setLayout(new BoxLayout(P1, BoxLayout.Y_AXIS));


        EventPanel Ev = new EventPanel("8:00", "16:00", "Bday", "Go party!");
        EventPanel Ev2 = new EventPanel("8:00", "16:00", "Java", "Handing Java Project");

        //Adding Two pannels into previous inner Panel
        P1.add(Ev);
        P1.add(Ev2);

        m_CalendarFrame.getContentPane().add(P1, BorderLayout.NORTH);

        m_CalendarFrame.setVisible(true);

        m_CalendarFrame.setSize(800,800);


}

I am trying to form the following Frame:

The main Frame uses a BorderLayout.

Into at this Frame and I added a Panel which uses BoxLayout - I'll call it P1.

For some reason the Panels I add into P1 are not seen when I run the program.

What's even more confusing is that if P1 uses a GridLayout instead of a BoxLayout, all the Panels I added into P1 are shown.

The EventPanel Class used in the code extends Panel and uses a SpringLayout.

Any ideas how to make P1 work?

Here's the relevant code:

    public static void main(String[] args) 
{
        //Setting Frame
        JFrame m_CalendarFrame = new JFrame();
        m_CalendarFrame.getContentPane().setLayout(new BorderLayout());

        //Setting inner Panel
        JPanel P1 = new JPanel();
        P1.setLayout(new BoxLayout(P1, BoxLayout.Y_AXIS));


        EventPanel Ev = new EventPanel("8:00", "16:00", "Bday", "Go party!");
        EventPanel Ev2 = new EventPanel("8:00", "16:00", "Java", "Handing Java Project");

        //Adding Two pannels into previous inner Panel
        P1.add(Ev);
        P1.add(Ev2);

        m_CalendarFrame.getContentPane().add(P1, BorderLayout.NORTH);

        m_CalendarFrame.setVisible(true);

        m_CalendarFrame.setSize(800,800);


}

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

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

发布评论

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

评论(2

神也荒唐 2024-12-18 01:11:19

我猜问题出在您使用 SpringLayout 的面板中。首先阅读有关 如何使用 SpringLayout 的 Swing 教程示例和解释。

如果您仍然需要帮助,请发布您的 SSCCE 来说明您的问题。并从简单开始。尝试将 SpringLayout 与一个组件一起使用,然后是两个,然后三个,直到您感觉更舒服为止。

I'm guess the problem is in your panel that uses the SpringLayout. Start by reading the Swing tutorial on How to Use SpringLayout for working examples and explanations.

If you still need help then post your SSCCE that demonstrates your problem. And start simple. Try using the SpringLayout with one comopnent, then two, then three until you feel more comforatable with it.

滴情不沾 2024-12-18 01:11:19

我相信这与可用空间管理有关,但我自己没有测试过。 BoxLayout 使用其绘制的组件的首选大小,而 GridLayout 在绘制的对象之间均匀分配可用空间。

如果您为 EventPanel 指定首选大小,会发生什么?根据您的代码示例,您可以通过强制 EventPanel 实例的首选大小 (800,400) 来快速测试这一点,并查看使用 BoxLayout 时它是否确实发生了变化。

I believe it has to do with free space management, but I haven't tested it myself. The BoxLayout uses the preferred size for the components it draws, and the GridLayout distributes the available space evenly among the objects drawn.

What happens if you specify a preferred size for your EventPanel? Based on your code example, you can quickly test this by forcing a preferred size of (800,400) for your EventPanel instances, and see if it does change something when using the BoxLayout.

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