【问题标题】:Java GridBagLayout with components insideJava GridBagLayout,里面有组件
【发布时间】:2012-02-10 14:52:32
【问题描述】:

我试图让带有两个面板的 GridBagLayout 分别占框架的 40% 和 60%,同时能够在其中包含组件,这很麻烦。

当我不将按钮放在面板内时,它会按照我想要的方式工作。

不太确定我做错了什么,我尝试将按钮的创建移动到创建 GridBagLayout 面板的位置,但它仍然不起作用。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test{

public void display(){
    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900,650);
    frame.setVisible(true);

    JPanel test = new JPanel();
    test.setLayout(new GridBagLayout());
    GridBagConstraints c= new GridBagConstraints();

    JPanel left= new JPanel();
    JPanel right= new JPanel();

    c.fill = GridBagConstraints.VERTICAL - GridBagConstraints.HORIZONTAL;
    c.weightx = 0.4;
    c.gridx = 1;
    c.weighty = 1;
    test.add(left,c);
    c.weightx = .6;
    c.gridx = 2;
    test.add(right,c);

    JButton button= new JButton("A button");
    left.add(button,c);//If I do not add this, then it shows how I want it to be

    frame.add(test);
   }
}

【问题讨论】:

  • createVector 是什么?
  • 对不起,重命名为按钮,来自别的东西。
  • 当我运行它时,它看起来像两个面板,有 40/60% 的分割,有或没有按钮。究竟是什么问题?
  • 另外,除了GridBagConstraints.VERTICAL - GridBagConstraints.HORIZONTAL,你确定你不只是想要GridBagConstraints.BOTH吗?
  • docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 大约在页面下方的 2/3 处应该可以帮助您。此外,正如文章所述,如果您难以手动操作,则始终可以使用 NetBeans,然后只需查看源代码。

标签: java swing grid jcomponent gridbaglayout


【解决方案1】:

权重的作用在于它们描述了如何处理额外的空间。组件具有布局管理器在计算布局时使用的首选、最小和最大尺寸。然后 GridBagLayout 使用这些权重分割额外的空间。在您的情况下,我认为拆分的空间等于 900-button.getPreferredSize().width。您可能将 800 像素分成 320 和 480。

【讨论】:

    【解决方案2】:

    这里是一个使用 GridBagLayout 创建的面板示例:(不用担心 swing factory,只需创建一个组件即可)

    private void buildSourcePanel() {
    
      JPanel pnlSource = new JPanel();
    
      GridBagLayout gbl_pnlSource = new GridBagLayout();
      gbl_pnlSource.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
      gbl_pnlSource.columnWidths = new int[]{0, 0, 100, 100, 25};
      pnlSource.setLayout(gbl_pnlSource);
    
      final JLabel lblFolderMask = swingFactory.createLabel(" SOURCE DIRECTORY ", null, null, SwingConstants.LEFT, SwingConstants.CENTER, true);
      pnlSource.add(lblFolderMask, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 0), 0, 0));
    
      txtSource = swingFactory.createTextField(null, "txtSource", null, SystemColor.textHighlight, SwingConstants.LEFT, false, true, "Source Directory");
      pnlSource.add(txtSource, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));
    
      final JButton btnBrowse = new JButton("Browse...");
      btnBrowse.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5)));
      btnBrowse.setFont(new Font("Verdana", Font.BOLD, 14));
      pnlSource.add(btnBrowse, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));
    
      final JButton btnClear = new JButton("Clear...");
      btnClear.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5)));
      btnClear.setFont(new Font("Verdana", Font.BOLD, 14));
      pnlSource.add(btnClear, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));
    
      lblStatus = swingFactory.createLabel(null, null, null, SwingConstants.CENTER, SwingConstants.CENTER, false);
      pnlSource.add(lblStatus, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 5), 0, 0));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2012-10-05
      • 2018-07-30
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 2020-08-29
      相关资源
      最近更新 更多