I have these components shown on the center vertically, I want to show them on the top of the panel, like it shown in the picture (the yellow arrow).
NB: I tried to set the anchor on "NORTH" and fill to "BOTH" but nothing happened.
我将这些组件垂直显示在中间,我想将它们显示在面板的顶部,如图(黄色箭头)所示。注:我试着把锚设在“北”上,然后填上“两个”,但什么也没发生。
enter image description here
在此处输入图像描述
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Border border = BorderFactory.createTitledBorder(null, "Saisie", TitledBorder.CENTER, TitledBorder.TOP, new Font("times new roman",Font.BOLD,14),Color.GRAY);
setBorder(border);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(3, 3, 3, 3);
add(JLabel_ID, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(3, 3, 3, 3);
add(JTextField_ID, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(3, 3, 3, 3);
add(JLabel_Nom, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.;
gbc.weighty = 0.;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(3, 3, 3, 3);
add(JTextField_Nom, gbc);
}```
I tried to set the anchor on "NORTH" and fill to "BOTH" but nothing happened.
更多回答
Try adding weighty = 1
to the last component (the button)
尝试将权重=1添加到最后一个组件(按钮)
That solve the problem for all the components but the last one stays at the center vertically, I usually add an invisible useless component at the end and i apply your solution and that solve the problem but I feel like it's not professionnal, thanx for the answer any way
这解决了所有组件的问题,但最后一个组件垂直居中,我通常在末尾添加一个看不见的无用组件,然后我应用您的解决方案,这解决了问题,但我觉得它不是专业的,谢谢你的答案
The set it’s anchor to be NORTH or use a “filler” component
将其锚定为北或使用“填充物”组件
我是一名优秀的程序员,十分优秀!