Сложное расположение нескольких компонентов - PullRequest
0 голосов
/ 01 января 2019

Я разрабатываю графический интерфейс, где у меня есть компоненты, показанные ниже.Но во втором ряду я хотел бы управлять пробелами между компонентами.Например, во втором ряду я хотел бы уместить как JLabel «Переместить во время», так и JTextField без такого большого расстояния.Как я вижу, сейчас MigLayout помещает JTextField в линию со вторым компонентом в первом ряду.Затем JButton «Перемещение» во 2-м ряду следует выровнять по 2-му компоненту в первом ряду.Как мне этого добиться?Ниже мой код.Я просмотрел множество шпаргалок и быстрых руководств по MigLayout, но пока не мог справиться.Может ли кто-нибудь порекомендовать изменения в моем фрагменте кода?Спасибо.

`

    JPanel helper = new JPanel( new MigLayout( "" ) );
    helper.add( new JButton( "Add puncta coordinates from CSV" ), "width 250:20" );
    helper.add( new JButton( "Add track coordinates from CSV" ), "wrap" );
    helper.add( new JLabel( "Move to time:" ) );
    JTextField tMoveTime = new JTextField();
    helper.add( tMoveTime, " gap 2px, width 75:20" );
    JButton bMoveTime = initMoveButton();
    helper.add( bMoveTime, "width 75:20" );

`

enter image description here

Собственно, я намерен сделать следующий графический интерфейсв полном объеме со всеми кнопками и текстовыми полями, подходит ли какой-либо другой макет?Если да, можно ли указать пример кода для рекомендуемого макета?Спасибо.

enter image description here

1 Ответ

0 голосов
/ 02 января 2019

Вы можете рассмотреть два основных подхода: Один из них - использовать очень гибкий менеджер раскладки, например MigLayout.Я не знаком с этим, но я могу рекомендовать GridbagLayout для работы:

    JPanel helper = new JPanel(  );
    GridBagLayout gbl_helper = new GridBagLayout();
    gbl_helper.columnWidths = new int[]{200, 7, 200, 60, 18, 0};
    gbl_helper.rowHeights = new int[]{20, 20};
    gbl_helper.columnWeights = new double[]{1.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
    gbl_helper.rowWeights = new double[]{0.0, 0};
    helper.setLayout(gbl_helper);

    GridBagConstraints gbc1 = new GridBagConstraints();
    gbc1.fill = GridBagConstraints.HORIZONTAL;
    gbc1.gridwidth = 2;
    gbc1.anchor = GridBagConstraints.NORTHWEST;
    gbc1.insets = new Insets(5,5,5,5);
    gbc1.gridx = 0;
    gbc1.gridy = 0;
    JButton button1 = new JButton( "Add puncta coordinates from CSV" );
    helper.add( button1, gbc1 );

    GridBagConstraints gbc2 = new GridBagConstraints();
    gbc2.fill = GridBagConstraints.HORIZONTAL;
    gbc2.gridwidth = 2;
    gbc2.anchor = GridBagConstraints.NORTHWEST;
    gbc2.insets = new Insets(5,5,5,5);
    gbc2.gridx = 2;
    gbc2.gridy = 0;
    JButton button2 = new JButton( "Add track coordinates from CSV" );

    helper.add( button2, gbc2 );

    GridBagConstraints gbc3 = new GridBagConstraints();
    gbc3.insets = new Insets(0, 0, 0, 5);
    gbc3.gridx = 0;
    gbc3.gridy = 1;
    JLabel label = new JLabel( "Move to time:" );
    helper.add( label, gbc3 );

    JTextField tMoveTime = new JTextField();
    tMoveTime.setColumns(15);
    GridBagConstraints gbc4 = new GridBagConstraints();
    gbc4.anchor = GridBagConstraints.WEST;
    gbc4.insets = new Insets(0, 0, 0, 5);
    gbc4.gridx = 1;
    gbc4.gridy = 1;
    helper.add( tMoveTime, gbc4);

    JButton bMoveTime = new JButton("Move");
    GridBagConstraints gbc5 = new GridBagConstraints();
    gbc5.insets = new Insets(0, 0, 0, 5);
    gbc5.anchor = GridBagConstraints.NORTHEAST;
    gbc5.gridx = 3;
    gbc5.gridy = 1;
    helper.add( bMoveTime, gbc5 );

enter image description here

Другой подходразделить сложный макет на более простые макеты с помощью подпанелей, каждая со своим собственным менеджером макетов:

enter image description here

    JPanel helper = new JPanel(  );
    helper.setLayout(new BoxLayout(helper, BoxLayout.Y_AXIS));
    getContentPane().add(helper);

    JPanel topPanel = new JPanel();
    helper.add(topPanel);
    JButton button1 = new JButton( "Add puncta coordinates from CSV" );
    topPanel.add(button1);
    JButton button2 = new JButton( "Add track coordinates from CSV" );
    topPanel.add(button2);

    JPanel bottomPanel = new JPanel();
    helper.add(bottomPanel);
    bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));

    JPanel bottomRIght = new JPanel();
    bottomPanel.add(bottomRIght);
    JLabel label = new JLabel( "Move to time:" );
    bottomRIght.add(label);
    JTextField tMoveTime = new JTextField();
    bottomRIght.add(tMoveTime);
    tMoveTime.setColumns(15);

    JPanel bottomleftPanel = new JPanel();
    bottomPanel.add(bottomleftPanel);
    bottomleftPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
    JButton bMoveTime = new JButton("Move");
    bottomleftPanel.add(bMoveTime);

enter image description here

...