1
试图了解用于Java的GridBagLayout
是如何工作的。以前从未使用过,所以它可能是我犯的一个愚蠢的错误。GridBagLayout没有得到预期的结果
我的目标是将JLabel
放在页面的顶部中心。我一直在使用Oracle上的java教程,但没有运气。看起来标签仍然在页面的中心。 (中心在x和y图的死点)。
从我的理解,如果我设置了gridx
和gridy
约束0
,编译器会顶一下,该计划的第一行和他们的文本。然后,我使用PAGE START
锚点将文本放置在页面的中心。我不完全确定weightx
和weighty
函数在我的防守中有什么作用。
import javax.swing.*;
import java.awt.*;
class test
{
public static void main (String Args [])
{
//frame and jpanel stuff
JFrame processDetail = new JFrame("Enter information for processes");
JPanel panelDetail = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//label to add on top centre
JLabel label = new JLabel("LOOK AT ME");
//set size of frame and operation
processDetail.setSize(500,500);
processDetail.setDefaultCloseOperation(processDetail.EXIT_ON_CLOSE);
//add the label to panel
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.PAGE_START;
c.weightx = 0; //not sure what this does entirely
c.gridx = 0; //first column
c.gridy = 0; //first row
panelDetail.add(label, c);
processDetail.add(panelDetail);
processDetail.setVisible(true);
}
}
显示您想要实现的目标以及目前正在获取的内容的图像。 – 2015-03-13 16:26:05