Установка ограничения дважды перед вызовом add () - PullRequest
0 голосов
/ 05 июля 2019

Я изучаю 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);

Кто-нибудь, пожалуйста, ответьте на мои вопросы?

1 Ответ

0 голосов
/ 06 июля 2019

Я думаю, что вторая ориентация макета: c.fill = GridBagConstraints.HORIZONTAL; является ошибкой, первая является условной и зависит от предыдущего установленного значения shouldFill.Поэтому я не согласен с тем, что эта переменная ничего не делает.

...