0

I am trying to place the components in the panel like this:

sample of desired layout

but in my code, it looks like this:

actual layout

I have tried without FlowLayout but the problem is the same. When I max the window size then it displays in a row, but when I try to resize the window size, components are out from border.

Will you please help me to resolve this issue? Here is my code:

    CNameLabel=new JLabel("Customer Name");

    CNameTextField = new JTextField (20); // create the Customer Name text field
    CNameTextField.setEditable(true);     // set editable text box

    CIDLabel=new JLabel("Customer ID");

    C_IDTextField = new JTextField (10);
    C_IDTextField.setEditable(true);      // set editable text box

    // Creating and populating the Top Panel

The code below is for creating the panel, setting the border of the panel, and setting the flowlayout for the components:

     topPanel=new JPanel();
     topPanel.setLayout(new FlowLayout());
     topPanel.setBorder(new TitledBorder(new EtchedBorder(), "Customer Data"));
     topPanel.add(CNameLabel); topPanel.add(CNameTextField); topPanel.add(CIDLabel);
     topPanel.add(C_IDTextField);

     roomTypeLabel=new JLabel ("Room Type ");

        //Create and populate Room type combo box
        roomTypeCombo = new JComboBox();
        roomTypeCombo.addItem( "Budget($50)" );
        roomTypeCombo.addItem( "Standard($75)" );
        roomTypeCombo.addItem( "Executive($200)" );
        roomTypeCombo.addItem( "Luxury($400)" );

        mealLabel=new JLabel ("Meal ");

        //Create and populate Meal type combo box
        mealCombo = new JComboBox();
        mealCombo.addItem( "None" );
        mealCombo.addItem( "Breakfast Only($10)" );
        mealCombo.addItem( "Any Two($30)" );
        mealCombo.addItem( "All Three($50)" );

        daysLabel=new JLabel ("Days");

        //Create and populate Days combo box

        daysCombo = new JComboBox();

        for(int i=0;i<31 ; i++)
                {

         daysCombo.addItem(i); // populate combobox with days

            }

       //Adding components to top panel

        topPanel.add(roomTypeLabel);
        topPanel.add(roomTypeCombo);
        topPanel.add(mealLabel);
        topPanel.add(mealCombo);
        topPanel.add(daysLabel);
        topPanel.add(daysCombo);

I have just skip the packages and declaration of the components. the program is running in fine condition

Thanks in advance.

4

1 回答 1

4

最后你可能需要调用 pack() ......我想如果你想在换行符中输入房间类型而不是你需要使用 gridlayout 但最后忘记调用 pack() 。

于 2012-04-10T03:46:00.047 回答