【问题标题】:GridLayout to GridBagLayout QuestionsGridLayout 到 GridBagLayout 问题
【发布时间】:2014-03-19 22:29:06
【问题描述】:

所以我曾经是一名出色的 GUI 程序员,但我似乎失去了相当多的 GridBagLayout 知识。

我想做的是制作一个定制的盒子,里面有 JPanel。我第一次使用 GridLayout,但是根据我的研究,确实没有办法为每个框自定义“% 宽度/高度”,它只是将它们全部均匀化。

所以我的第一次尝试导致了这个。

(网格布局)

我继续环顾四周,我认为我需要使用 GridBagLayout。所以我花了一个小时看例子,我觉得我只是因为没有更好的词而“不明白”。我已经尝试为 GridBagLaout 实现各种测试代码,但我永远无法完全满足我的需求。

我的问题是,有人可以给我一个我正在寻找的框架设置吗?我不想要整个代码,只想要基本的骨架,这样我就可以自己搞清楚(这样我实际上“明白了”)。

所以说骨架,我的意思是只为我制作一个盒子,然后请简化每一步的作用,以便我可以尝试对其余的盒子遵循相同的过程。

我希望最终得到的是......

(我想要的 GridBagLayout)

就实际百分比而言,我正在考虑这样做:

宽度:绿色/红色侧为 80%,蓝色侧为 20%。

高度:红色为 80%,绿色/白色为 20%。

我今天正在完成一个项目,但我希望我可以在今晚的某个时候开始工作。

非常感谢您以后的所有帮助! -奥斯汀

【问题讨论】:

    标签: java eclipse swing gridbaglayout


    【解决方案1】:

    试试这个:

    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagDemo implements Runnable
    {
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new GridBagDemo());
      }
    
      public void run()
      {
        JComponent redComp = new JPanel();
        redComp.setBackground(Color.RED);
    
        JComponent greenComp = new JPanel();
        greenComp.setBackground(Color.GREEN);
    
        JComponent blueComp = new JPanel();
        blueComp.setBackground(Color.BLUE);
    
        JComponent whiteComp = new JPanel();
        whiteComp.setBackground(Color.WHITE);
    
        GridBagConstraints gbc = new GridBagConstraints();
        // we'll use this anchor/fill for all components
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.BOTH;
    
        JPanel panel = new JPanel(new GridBagLayout());
    
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.gridheight = 1;
        gbc.weightx = 0.8;  // use 80% of the overall width
        gbc.weighty = 0.8;  // use 80% of the overall height
        panel.add(redComp, gbc);
    
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.weightx = 0.2;  // use 20% of the overall width
        gbc.weighty = 1.0;  // use 100% of the overall height
        panel.add(blueComp, gbc);
    
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.8;  // use 80% of the width used by green/white comps
        gbc.weighty = 0.2;  // use 20% of the overall height
        panel.add(greenComp, gbc);
    
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 0.2;  // use 20% of the width used by green/white comps
        gbc.weighty = 0.2;  // use 20% of the overall height
        panel.add(whiteComp, gbc);
    
        JFrame frame = new JFrame("GrigBag Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    

    【讨论】:

    • 嗯,好吧,从我所见(如果我错了,请纠正我)。您通过 JComponent 制作一个 JPane,并为其设置颜色。然后你调用 gbc 一个定义 GridBagLayout 的变量,我假设它被更新/覆盖,因为它使每个窗格(即在每个 panel.add(...) 之后。所以我遵循这个但我仍然对两件事感到困惑。我做不明白 gridwidth/gridwidth 和 gridx/gridy 如何与每个包的位置相关。我也不明白你的 gbc.anchor 和 gbc.fill 做了什么。我对“填充”的作用有一些猜测,但不知道在锚约束 TY 上!
    • GridBagConstraints 的所有设置(gridx/y、anchor、fill 等)在教程中都有讲解,How To Use GridBagLayout
    • 我想我现在已经弄清楚了。非常感谢楼主!
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2014-02-19
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多