网格布局与框内

时间:2010-09-15 18:20:28

标签: java user-interface

我有一个网格布局,由4列组成。只有一排。在每个网格布局框中,我有一个框布局,所以我可以把东西堆叠在一起。

问题是,我在第一列有5件事,第2列有4件,现在有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();      

    }
}

任何想法如何使colums排队:/

由于

2 个答案:

答案 0 :(得分:0)

在这种情况下,您应该只使用GridLayout而不是框布局。

在网格中,所有内容都会对齐。

答案 1 :(得分:0)

您需要为布局管理器提供更多信息。在这种情况下,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() );