Я изучаю GridBagLayout в разгаре, используя учебные пособия из Oracle.
Как мы все знаем, мы можем снова установить ограничение после использования 1 компонентом. Здесь я заметил, что автор снова устанавливает ограничение перед вызовом метода add().
public class GridBagLayoutDemo {
//Here I have no idea why these 3 lines for
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
//1)why not just ignore the above declaration and just type
//pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); ?
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//same go to here
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//2)the author set the constraint in above line,then author set it again in below line
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
//here it is
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
Кто-нибудь, пожалуйста, ответьте на мои вопросы?