【问题标题】:JFrame center componentsJFrame 中心组件
【发布时间】:2015-09-20 22:08:32
【问题描述】:

我想知道如何使我的组件居中,我找到了 gridBagLayout 但我找不到让它比原来更大的方法:我的按钮太小了。

我的部分代码:

private void addMainButtons() {
        JPanel container = new JPanel() {
            private static final long serialVersionUID = -424395301619105440L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Dimension dim = this.getSize();
                DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
                Date date = new Date();
                g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
                g.setColor(Color.BLACK);
                String s = format.format(date);
                g.drawString(s, (int) ((dim.getWidth() - s.length() - 80) / 2), 20);
            }
        };
        container.setBackground(new Color(109, 69, 60));

        JButton productsButton = new JButton("Produits");
        productsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(frame, pages[1]);
            }
        });
        productsButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        productsButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        // Creating grid
        container.setPreferredSize(new Dimension(400, 600));
        container.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        container.add(productsButton, gbc);

        gbc.gridy = 1;
        gbc.gridwidth = 1;
        container.add(new JButton("Entrée"), gbc);

        gbc.gridx = 1;
        container.add(new JButton("Sortie"), gbc);

        mainPage.setLayout(new BorderLayout());
        mainPage.add(container, BorderLayout.CENTER);
        // End of main page
    }

GridBagLayout 真的很难用,还有其他方法可以让组件居中吗?我可以使用 NestedLayouts,但我不知道如何使用。

【问题讨论】:

  • 您可以使用ipadxipady 来影响GridBagLayout 中组件的大小。您可能还需要使用fill 使组件填充其单元格的可用空间
  • 考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应。您可能还想更好地解释您的期望,说您的按钮“太小”真的无济于事

标签: java swing jframe centering gridbaglayout


【解决方案1】:

尝试下面的代码来增加按钮的大小,同时将每个组件添加到 gridbagconstraints。根据需要更改 weightx/weighty 的值。

gbc.weightx = 1.0;
gbc.weighty = 1.0;

要在组件之间留出更多空间,请在将每个组件添加到 gridbagconstraints 时使用以下代码。

gbc.insets = new Insets(2, 2, 2, 2);

据我所知,您使用了正确的方法来居中组件。也可以通过其他布局来完成,但可能会有点困难。

【讨论】:

    【解决方案2】:

    您可以使用weightx 和/或weighty 来影响组件将占用的空间量,例如,如果我这样做了

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    
    gbc.weightx = 1;
    gbc.weighty = 1;
    
    container.add(productsButton, gbc);
    
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    container.add(new JButton("Entrée"), gbc);
    
    gbc.gridx = 1;
    container.add(new JButton("Sortie"), gbc);
    

    我可以生成这个...

    您可以使用两个容器,一个包含显示日期的 JLabel,另一个包含按钮,但我怀疑这就是您真正想要的。

    您可以使用 ipadxipady 将数量添加到组件的首选大小

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    
    gbc.ipadx = 40;
    gbc.ipady = 40;
    
    container.add(productsButton, gbc);
    
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    container.add(new JButton("Entrée"), gbc);
    
    gbc.gridx = 1;
    container.add(new JButton("Sortie"), gbc);
    

    当包含在您现有的约束中时,可以让您稍微“增加”按钮。

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 2014-10-26
      • 2023-03-13
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      相关资源
      最近更新 更多