【问题标题】:Repeat WindowBuilder JLabel using Grid Layout使用网格布局重复 WindowBuilder JLabel
【发布时间】:2014-03-14 19:49:06
【问题描述】:

我想重复一个 JLabel "n" no。用户给出“n”的次数。 这个实现,使用 WindowBuilder 的 Grid 布局,不起作用,根本不打印。

我做错了什么?

JLabel lblNewLabel_1[]=new JLabel[20];
GridBagConstraints gbc_lblNewLabel_1[] = new GridBagConstraints[20];
for(int i=0;i<n;i++)
{
        lblNewLabel_1[i] = new JLabel("Table #");
        gbc_lblNewLabel_1[i].insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_1[i].gridx = 0;
        gbc_lblNewLabel_1[i].gridy = 4+i;
        frame.getContentPane().add(lblNewLabel_1[i], gbc_lblNewLabel_1[i]);
}

完整方法,我的内容窗格是网格布局。

private void initialize() {

        frame = new JFrame();
        frame.setBounds(100, 100, 507, 432);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{57, 377, 0};
        gridBagLayout.rowHeights = new int[]{60, 37, 35, 20, 0, 0, 0, 0};
        gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        frame.getContentPane().setLayout(gridBagLayout);

        textField = new JTextField();
        textField.setToolTipText("Max: 20");
        textField.setText("0");

        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(arg0.getSource()==textField)
                    n = Integer.parseInt(arg0.getActionCommand());

            }
        });

        JLabel lblNewLabel = new JLabel("Enter The Number of Tables");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 30));
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel.gridx = 1;
        gbc_lblNewLabel.gridy = 1;
        frame.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 3;
        frame.getContentPane().add(textField, gbc_textField);
        textField.setColumns(10);

        GridBagConstraints gbc_lblNewLabel_1[] = new GridBagConstraints[20];
        for(int i=0;i<n;i++){
            lblNewLabel_1[i] = new JLabel("Table #");
            gbc_lblNewLabel_1[i].insets = new Insets(0, 0, 5, 5);
            gbc_lblNewLabel_1[i].gridx = 0;
            gbc_lblNewLabel_1[i].gridy = 4+i;
            frame.getContentPane().add(lblNewLabel_1[i], gbc_lblNewLabel_1[i]);
        }
    }

【问题讨论】:

  • 网格布局没有 Insets 和 GridBagConstraints

标签: java swing windowbuilder


【解决方案1】:

JFrame 的默认布局是BorderLayout

如果要使用GridBagLayout,则需要将内容窗格的布局设置为使用 GridBagLayout。

我建议您阅读 How to Use GridBagLayout 上的 Swing 教程中的部分以获得更多解释和工作示例。

如果您想使用GridLayout,那么您还需要将内容窗格的布局管理器设置为 GridLayout,并且您不使用任何约束。再次阅读示例教程。

【讨论】:

  • 感谢您的回复。我的 ContentPane 是 gridBagLayout。更新了问题以反映完整的方法。
  • @IshaanGarg,我从未将 GridBagLayout 与 columnWidths、rowHeight... 属性一起使用。每次向面板添加组件时,我也不会创建新的约束。尝试查看 Swing 教程以获取工作示例。您发布的代码没有帮助,因为它不是正确的SSCCE
  • @IshaanGarg,我不知道你为什么切换到 GroupLayout。那是为 IDE 设计的。 GridBagLayout 更容易使用。您需要做的就是阅读教程并测试示例以了解其工作原理。
猜你喜欢
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
相关资源
最近更新 更多