GridBagLayout 多个按钮 +边界

发布于 2024-11-29 18:16:12 字数 2240 浏览 1 评论 0原文

我正在尝试使用 BoxLayoutJPanel 内使用 10 (3x3 + 1) JButtonsGridBagLayout

但是我对glueboxes 或类似的GridBagLayout JPanel 所做的一切都会占用BoxLayout 中的所有额外空间。我可能错过了一些东西或者这是不可能做到的?

我使用的一种解决方案是使用 gridbaglayout 内的扩展元素向上推动按钮。这会将按钮放在正确的位置,但边框看起来很大。

下面是我的示例代码:

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridBagLayoutTest extends JFrame {

    public GridBagLayoutTest(){
        super();
        this.setTitle("JVectorView");
        this.setSize(300,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = this.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        content.add(new JLabel("Hello!"));
        content.add(new Controls());
        content.add(Box.createGlue());
        this.setVisible(true);
    }


    private class Controls extends JPanel{
        private static final int WIDTH = 3, HEIGHT = 3;

        public Controls(){
            GridBagConstraints constraints = new GridBagConstraints();

            //this.setBorder(BorderFactory.createLineBorder(Color.red));
            this.setBorder(BorderFactory.createTitledBorder("Some stuff"));
            constraints.fill = GridBagConstraints.NONE;
            this.setLayout(new GridBagLayout());
            for(int row = 0; row < HEIGHT; row++){
                for(int col = 0; col < WIDTH; col++){
                    constraints.gridx = col;
                    constraints.gridy = row;
                    this.add(new JButton("B"+(col+row*WIDTH)), constraints);

                }
            }
            constraints.gridx = 1;
            constraints.gridy = 3;
            this.add(new JButton("B"+(10)), constraints);
        }
    }

    public static void main(String[] args) {
        new GridBagLayoutTest();
    }

}

我希望按钮周围的边框紧密。是否有可能让 gridbaglayout 折叠在其内容上,或者它总是强制填充面板?

I'm trying to use GridBagLayout for 10 (3x3 + 1) JButtons inside a JPanel using BoxLayout.

But what ever I do with glueboxes or similarly the GridBagLayout JPanel takes up all the extra space in the BoxLayout. I'm probably missing something or is this not possible to do?

One solution I have used is to push the buttons up with a expanding element inside the gridbaglayout. This puts the buttons in the right place but the border box appear to big.

Here follows my example code:

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GridBagLayoutTest extends JFrame {

    public GridBagLayoutTest(){
        super();
        this.setTitle("JVectorView");
        this.setSize(300,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = this.getContentPane();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        content.add(new JLabel("Hello!"));
        content.add(new Controls());
        content.add(Box.createGlue());
        this.setVisible(true);
    }


    private class Controls extends JPanel{
        private static final int WIDTH = 3, HEIGHT = 3;

        public Controls(){
            GridBagConstraints constraints = new GridBagConstraints();

            //this.setBorder(BorderFactory.createLineBorder(Color.red));
            this.setBorder(BorderFactory.createTitledBorder("Some stuff"));
            constraints.fill = GridBagConstraints.NONE;
            this.setLayout(new GridBagLayout());
            for(int row = 0; row < HEIGHT; row++){
                for(int col = 0; col < WIDTH; col++){
                    constraints.gridx = col;
                    constraints.gridy = row;
                    this.add(new JButton("B"+(col+row*WIDTH)), constraints);

                }
            }
            constraints.gridx = 1;
            constraints.gridy = 3;
            this.add(new JButton("B"+(10)), constraints);
        }
    }

    public static void main(String[] args) {
        new GridBagLayoutTest();
    }

}

I would like the border to be tight around the buttons. Is it at all possible to get gridbaglayout to collapse in on it's content or does it always force fill the panels?

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

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

发布评论

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

评论(1

暖阳 2024-12-06 18:16:12
JPanel p=new JPanel(new FlowLayout());
p.add(new Controls());
content.add(p);
JPanel p=new JPanel(new FlowLayout());
p.add(new Controls());
content.add(p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文