【问题标题】:Swing doesn't respect my GridLayout Rows x Columns definition. How to fix?Swing 不尊重我的 GridLayout Rows x Columns 定义。怎么修?
【发布时间】:2015-04-17 22:02:58
【问题描述】:

我一直在尝试解决这个问题,但没有成功。 如何使JPanel p 尊重定义的GridLayout? 我得到的只是第一行包含 3 个面板,但不是我告诉 Java 执行的 4 个。

什么是必要的巫术(或我的无知的知识),才能使它起作用?

package temp2;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Temp2 {

    public static void main(String[] args) {

        JFrame f = new JFrame();
        JPanel p = new JPanel();  
    //  GridLayout flow = new GridLayout(1, 5); --> With this I get a row with 5 jpanels
        GridLayout flow = new GridLayout(2, 4); // --> Why it doenst work? I want 4 
                                                // jpanels in the first row and 
                                                // subsequent one on the next row
                                                // --> Why it doesnt respect my code?
        p.setLayout(flow);
      // p.setSize(800,800); // --> This doesnt make any difference on final result either
      //  p.setPreferredSize(new Dimension(800,800));  // --> This doesnt make any
                                                      // difference on final result either
        p.setMinimumSize(new Dimension(800,800));

        JPanel x1 = new JPanel();     
        x1.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x1.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
        x1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x2 = new JPanel();       
        x2.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x2.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
        x2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x3 = new JPanel();      
        x3.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x3.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect this maximum
        x3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x4 = new JPanel();      
        x4.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x4.setMaximumSize(new Dimension(50,30));  // --> It doesnt respect this maximum      
        x4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        JPanel x5 = new JPanel();       
        x4.setMaximumSize(new Dimension(50,30)); // --> It doesnt respect these preferences
        x5.setPreferredSize(new Dimension(50,30)); // --> It doesnt respect this maximum 
        x5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

         p.add(x1);
         p.add(x2);
         p.add(x3);
         p.add(x4);
         p.add(x5);

         f.getContentPane().add(p, "Center");

         f.setSize(800, 800);
         f.setVisible(true);
         p.updateUI();

        }   
    }

因为这是我的第一个 StackOverflow 问题,所以我试图严格遵守规则:

具体:我希望GridLayout 尊重行 x 列的定义

我的最终目的是什么:在第一行显示 4 个面板,在第二行显示后续一个。

使其与他人相关:这就是为什么此代码具有指导意义,重复所有小组声明,以便新手(和我)能够理解所有代码并专注于 GridLayout 问题。

【问题讨论】:

  • 参见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是的。)顺便说一句-GridLayout 会将布局中的每个组件拉伸到最宽的宽度和最高的高度。也许看看GridBagLayout,它将为其列和行中的每个组件保留足够的空间,但是(取决于GridBagConstraints不拉伸(而是使用首选) 组件的大小。
  • 在设置容器的最小尺寸时要非常非常小心
  • 感谢您的意见。我将看一下 GridBagLayout ,看看我是否能得到我真正想要的东西(第一行相同的 4 个 jpanels)。好吧,我删除了所有关于 Preferred|Maximum|Minimum Sizes 的代码,而 GridLayout 仍然不尊重我的网格定义。我的猜测是 GridLayout 定义不是最重要的,但 Java 认为在 jPanel 中分发这些组件的最有用的方法是什么?我有点失落......

标签: java swing awt layout-manager grid-layout


【解决方案1】:
    GridLayout flow = new GridLayout(2, 4); // --> Why it doenst work? I want 4 
                                            // jpanels in the first row and 
                                            // subsequent one on the next row
                                            // --> Why it doesnt respect my code?

像这样?

改用:

    GridLayout flow = new GridLayout(0,4);

引用constructor 的文档:

创建具有指定行数和列数的网格布局。布局中的所有组件都具有相同的大小。

行和列中的一个(但不能同时)可以为零,这意味着可以将任意数量的对象放在一行或一列中。

【讨论】:

  • 非常感谢安德鲁。你应该得到 +1,但我不能这样做,因为我需要更多的声誉。事实上,我的错在文档中就有一个明确的答案。有趣的是,零定义覆盖了 Java 在这一方面的“智能”。至少他们没有打开那个选项!
猜你喜欢
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多