gpt4 book ai didi

java - 为gridBagLayout中的组件添加垂直距离

转载 作者:行者123 更新时间:2023-12-01 18:56:44 27 4
gpt4 key购买 nike

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

datfeild(Jlabel)billno (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);


}

最佳答案

有类GridBagConstraints,它具有字段insets。这允许设置组件插入:

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

container.add(c, gbc);

关于java - 为gridBagLayout中的组件添加垂直距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13732086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com