【问题标题】:Adding vertical distance to components in gridBag Layout向 gridBagLayout 中的组件添加垂直距离
【发布时间】:2012-12-05 20:44:28
【问题描述】:

我在 gridbagLayout 中添加了四个组件。我想在它们之间添加垂直距离如何添加垂直距离? 组件是

datfeild (Jlabel) 比尔诺 (JTextFEILD) 日期(JTextField) 搜索 JBUTTOn) 删除(JBUTTOn)

我尝试让 widthy=1.0;但没有得到。请帮帮我..谢谢..

enter code here


     b()
{
    GridBagLayout gridBag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    this.setLayout(gridBag);
    panel.setLayout(gridBag);

    JButton search=new JButton();
    JButton delete=new JButton();

    JLabel dateFieldLabel = new JLabel("Date Field");
    JTextField thingNameField = new JTextField("Billno");
    JTextField thingdateField = new JTextField("Date");


gbc.gridx = 0;
gbc.gridy = 0;
this.add(new JScrollPane(table),gbc);


gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.BOTH; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0;

panel.add(dateFieldLabel,  gbc);




gbc.gridx = 0;
gbc.gridy = 2;  
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.BOTH; 
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL; 
gbc.weightx = 0.0; 
gbc.weighty = 0.0;
panel.add(thingNameField,  gbc);        





gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.BOTH; 
gbc.weightx = 0.0; 
gbc.weighty = 1.0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL; 
panel.add(thingdateField,  gbc);

gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH; 
gbc.fill = GridBagConstraints.NONE; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0;

gbc.insets = new Insets(2,2,2,2);
panel.add(search,  gbc);

gbc.gridx = 1;
gbc.gridy = 4;  
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH; 
gbc.fill = GridBagConstraints.NONE; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0;
gbc.insets = new Insets(2,2,2,2);
panel.add(delete,  gbc);
gbc.anchor = GridBagConstraints.NORTHEAST;

this.add(panel);


     }

【问题讨论】:

    标签: java swing layout gridbaglayout insets


    【解决方案1】:

    有类GridBagConstraints 有字段insets。这允许设置组件插图:

    Component c = ...
    GridBagLayout gbl = ...
    Container container = new JPanel(gbl);
    GridBagConstraints gbc = gbl.getConstraints(c);
    gbc.insets = new Insets(....);
    
    container.add(c, gbc);
    

    【讨论】:

      【解决方案2】:

      答案就在您的眼皮底下:使用Insets

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

      可以变成

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-02
        • 1970-01-01
        • 2014-09-18
        • 2011-07-10
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        相关资源
        最近更新 更多