Разметка сетки с коробкой внутри - PullRequest
0 голосов
/ 15 сентября 2010

У меня есть сетка, состоящая из 4 столбцов.Всего один ряд.Внутри каждого блока макета сетки у меня есть макет блока, чтобы я мог складывать вещи друг на друга.

Проблема в том, что у меня есть 5 вещей в первом столбце, 4 в столбце 2, 3 и 4 не имеют отношения к настоящему моменту.Проблема в том, что я хочу, чтобы элементы в столбцах 1 и 2 совпадали, но поскольку во 2-ом элементе есть 4 элемента, он растягивается, чтобы соответствовать размеру столбца 1, в котором есть 5 элементов

Хорошо, это сложноЧтобы объяснить, вот код, над которым я работаю

import javax.swing.*;
import java.awt.*;

public class myGui extends JFrame
{
      /**
       * Constructor for my GUI class
       */
      public myGui()
      {
      }

      /**
       * @param args
       */
      public static void main(String[] args)
      {
            createFrame();
      }

      /**
       * Build the gui
       */
    public static void createFrame ()
    {
        JFrame frame = new JFrame("This is my gui");
        JPanel content = new JPanel(new GridLayout(1, 4));
        frame.getContentPane().add(content);
        content.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Requestor"));
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

       //Create a box grid layout inside a panel and apply this to the grid view colums
        JPanel col1 = new JPanel();
        BoxLayout boxLayout1 = new BoxLayout(col1, BoxLayout.Y_AXIS);
        col1.setLayout(boxLayout1);

        JPanel col2 = new JPanel();
        BoxLayout boxLayout2 = new BoxLayout(col2, BoxLayout.Y_AXIS);
        col2.setLayout(boxLayout2);

        JPanel col3 = new JPanel();
        BoxLayout boxLayout3 = new BoxLayout(col3, BoxLayout.Y_AXIS);
        col3.setLayout(boxLayout3);

        JPanel col4 = new JPanel();
        BoxLayout boxLayout4 = new BoxLayout(col4, BoxLayout.Y_AXIS);
        col4.setLayout(boxLayout4);

        //Add the boxLayouts to the appropriate columns
        content.add(col1);
        content.add(col2);
        content.add(col3);
        content.add(col4);   

        //create JLabels
        JLabel requestorId = new JLabel("RequestorID");
        JLabel org = new JLabel("Organisation");
        JLabel orgAddress = new JLabel("Organisation Address");
        JLabel otherAdd = new JLabel("Other Address");
        JLabel title = new JLabel("Title");
        JLabel phoneNo = new JLabel("Phone Number");
        JLabel firstName = new JLabel("First Name");
        JLabel surname = new JLabel("Surname");
        JLabel emailAdd = new JLabel("E Mail Address");
        JLabel dept = new JLabel("Department");
        JLabel fax = new JLabel("Fax Number");

        //create text areas
        JTextArea reqIDTxt = new JTextArea();
        reqIDTxt.setEditable(false);     

        JTextArea orgTxt = new JTextArea();
        JTextArea orgAddTxt = new JTextArea();
        JTextArea otherAddTxt = new JTextArea();
        JTextArea titleTxt = new JTextArea();
        JTextArea phoneNoTxt = new JTextArea();
        JTextArea firstNameTxt = new JTextArea();
        JTextArea faxNoTxt = new JTextArea();
        JTextArea surnameTxt = new JTextArea();
        JTextArea emailTxt = new JTextArea();
        JTextArea deptTxt = new JTextArea();

        //Add text areas and labels to their respective grid locations
        col1.add(requestorId);
        col1.add(reqIDTxt);
        col1.add(title);
        col1.add(titleTxt);
        col1.add(firstName);
        col1.add(firstNameTxt);
        col1.add(surname);
        col1.add(surnameTxt);
        col1.add(dept);
        col1.add(deptTxt);

        col2.add(org);
        col2.add(orgTxt);
        col2.add(phoneNo);
        col2.add(phoneNoTxt);
        col2.add(fax);
        col2.add(faxNoTxt);
        col2.add(emailAdd);
        col2.add(emailTxt);

        col3.add(orgAddress);
        col3.add(orgAddTxt);

        col4.add(otherAdd);
        col4.add(otherAddTxt);
        frame.pack();
        frame.show();      

    }
}

любые идеи, как сделать выравнивание столбцов: /

Спасибо

Ответы [ 2 ]

0 голосов
/ 16 сентября 2010

Вам нужно дать менеджеру раскладки больше информации для работы.В этом случае BoxLayout учитывает максимальный размер компонента, поэтому вам нужно написать что-то вроде:

JTextArea reqIDTxt = new JTextArea(2, 10);
JTextArea orgTxt = new JTextArea(2, 20);
JTextArea orgAddTxt = new JTextArea(2, 20);
JTextArea otherAddTxt = new JTextArea(2, 20);
JTextArea titleTxt = new JTextArea(2, 20);
JTextArea phoneNoTxt = new JTextArea(2, 20);
JTextArea firstNameTxt = new JTextArea(2, 20);
JTextArea faxNoTxt = new JTextArea(2, 20);
JTextArea surnameTxt = new JTextArea(2, 20);
JTextArea emailTxt = new JTextArea(2, 20);
JTextArea deptTxt = new JTextArea(2, 20);

orgTxt.setMaximumSize( orgTxt.getPreferredSize() );
deptTxt.setMaximumSize( deptTxt.getPreferredSize() );
reqIDTxt.setMaximumSize( reqIDTxt.getPreferredSize() );
titleTxt.setMaximumSize( titleTxt.getPreferredSize() );
firstNameTxt.setMaximumSize( firstNameTxt.getPreferredSize() );
surnameTxt.setMaximumSize( surnameTxt.getPreferredSize() );
orgTxt.setMaximumSize( orgTxt.getPreferredSize() );
phoneNoTxt.setMaximumSize( phoneNoTxt.getPreferredSize() );
faxNoTxt.setMaximumSize( faxNoTxt.getPreferredSize() );
emailTxt.setMaximumSize( emailTxt.getPreferredSize() );
0 голосов
/ 15 сентября 2010

В этом случае вы должны использовать только GridLayout, а не макеты блоков.

В сетке все будет выровнено.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...