Java JLabel/JPanel定位问题。(网格布局)
在myJava JLabel/JPanel定位问题。(网格布局),java,swing,jlabel,layout-manager,gridbaglayout,Java,Swing,Jlabel,Layout Manager,Gridbaglayout,在myJLabel组件的定位上存在小问题 代码: 公共类删除{ 公共静态void main(字符串[]args){ GridBagConstraints gbc=新的GridBagConstraints(); //添加JPanels.Panel以获取说明 JPanel面板=新的JPanel(); panel.setLayout(新的GridBagLayout()); //JLabel以获取说明。 JLabel标签=新的JLabel标签(“说明:键入您收到的成绩,以及它们在确定总体平均值时的权重。
JLabel
组件的定位上存在小问题
代码:
公共类删除{
公共静态void main(字符串[]args){
GridBagConstraints gbc=新的GridBagConstraints();
//添加JPanels.Panel以获取说明
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
//JLabel以获取说明。
JLabel标签=新的JLabel标签(“说明:键入您收到的成绩,以及它们在确定总体平均值时的权重。
按“计算”后,结果将显示您迄今为止的平均值。
您输入的每个成绩必须是非负数,输入的每个百分比/权重必须是正数:”;
gbc.gridwidth=2;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.anchor=gridbagsconstraints.PAGE_START;
gbc.weighty=1;
面板。添加(标签,gbc);
//JLabel1用于分配/等级/重量(百分比)
JLabel label1=新的JLabel(“Assingment\t\t\t\t\tWeight\t\t\t\t百分比”);
gbc.gridx=0;
gbc.gridy=0;
面板。添加(标签1,gbc);
//新帧集
JFrame=新JFrame(“坡度计算器”);
frame.setVisible(true);
框架。设置尺寸(750500);
frame.setDefaultCloseOperation(关闭时退出);
框架。添加(面板);
}
}
现在,这将JLabel
(label
)定位在我想要的框架顶部。问题是JLabel
(label1
)我想要label1
稍微低于标签
如果我设置了gbc.gridy=0
,label1
显示在标签顶部,使两个单独的标签碰撞在一起
当我设置gbc.gridy=1
时,label1
一直到框架的底部
现在,我希望标签1略低于标签,但是我不能将浮动与
gridy
一起使用。我如何解决这个问题?gbc.weighty=1;
使组件在所有组件布局后都有剩余的空间。而不是提供label
培训您应仅将其交给标签1
当您不向组件提供gridx
/gridy
时,GridBagLayout
将组件放在最后一个组件旁边,因为没有添加其他组件,标签
放在顶部,但您也向label1
提供0
的gridy
,w将其放置在同一位置
相反,将label
放在0x0
和label1
放在0x1
//JLabel for the Instructions.
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>");
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(label1, gbc);
JLabel标签=新的JLabel(“说明:输入您收到的分数,以及它们在确定总体平均值时的权重。
按“计算”后,结果将显示您到目前为止的平均值。
您输入的每个分数必须是非负数,并且您输入的每个百分比/权重必须是正数:);
gbc.gridwidth=2;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridy=0;
面板。添加(标签,gbc);
//JLabel1用于分配/等级/重量(百分比)
JLabel label1=新的JLabel(“Assingment\t\t\t\t\tWeight\t\t\t\t百分比”);
gbc.gridx=0;
gbc.gridy=1;
gbc.anchor=gridbag.NORTH;
gbc.weighty=1;
面板。添加(标签1,gbc);
您还可以通过使用锚定
属性来影响组件在其单元内对齐的位置
请查看更多详细信息
在不知道“确切”您在做什么的情况下,我可能建议您也查看一下//JLabel以了解说明。
JLabel标签=新的JLabel(“说明:输入您收到的分数,以及它们在确定总体平均值时的权重。
按“计算”后,结果将显示您到目前为止的平均值。
您输入的每个分数必须是非负数,并且您输入的每个百分比/权重必须是正数:);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weighty=1;
gbc.gridx=0;
gbc.gridy=0;
面板。添加(标签,gbc);
//JLabel1用于分配/等级/重量(百分比)
JLabel label1=新的JLabel(“Assingment\t\t\t\t\tWeight\t\t\t\t百分比”);
gbc.gridx=0;
gbc.gridy=1;
面板。添加(标签1,gbc);
非常感谢。在这里提问之前,我总是使用Java教程,但在这里有人解释之前,我无法完全理解。再次感谢。您好,我还有一个问题。您能帮我吗?@bob9123我通常会提供其他教程的链接,docs o支持文档,因为你不应该接受我的话;如果你有更多的问题,也许考虑再问一个问题,只需提供一个链接作为一个参考点,对于那些还没有读过这个问题的人来说,)链接到新的问题。再次感谢你的帮助:
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 0;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
panel.add(label1, gbc);
//JLabel for the Instructions.
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(label, gbc);
//JLabel1 for Assingment/Grade/Weight(Percent)
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>");
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(label1, gbc);