简体   繁体   English

带有框的网格布局

[英]Grid layout with box inside

I have a grid layout, consisting of 4 columns. 我有一个网格布局,由4列组成。 Just one row. 只需一行。 Inside each grid layout box i have a box layout so i can stack things on top of each other. 在每个网格布局框内,我都有一个框布局,因此我可以将东西堆叠在一起。

The problem is, i have 5 things in column one, 4 in column 2, 3 and 4 are irellevant for now. 问题是,我在第一列中有5件事,在第二列,第三和第四列中有4件事目前是不利的。 The issue is i want the items in columns 1 and 2 to match, but because the 2nd has 4 items in it, its stretching those to fit the size of column 1 which has 5 things inside it 问题是我希望第1列和第2列中的项目匹配,但是由于第2列中有4个项目,因此它会拉伸它们以适合第1列的大小,其中第1列中有5件事

Ok its complicated to explain, here is the code i am working on 好的,它的解释很复杂,这是我正在处理的代码

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();      

    }
}

any ideas how to make the colums line up :/ 任何想法如何使列对齐:/

Thanks 谢谢

In this case you should only use the GridLayout and not the box layouts. 在这种情况下,您应该仅使用GridLayout而不是Box布局。

In a grid everything will be aligned. 在网格中,所有内容都将对齐。

You need to give the Layout Manager more information to work with. 您需要为布局管理器提供更多信息以供使用。 In this case the BoxLayout respects the maximum size of a component so you need code something like: 在这种情况下,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() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM