gpt4 book ai didi

Java : Components position

转载 作者:行者123 更新时间:2023-12-01 19:06:02 25 4
gpt4 key购买 nike

我试图将组件放置在面板中,如下所示:

sample of desired layout

但在我的代码中,它看起来像这样:

actual layout

我尝试过不使用FlowLayout,但问题是一样的。当我最大化窗口大小时,它会显示在一行中,但是当我尝试调整窗口大小时,组件会超出边框。

您能帮我解决这个问题吗?这是我的代码:

    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

下面的代码用于创建面板、设置面板边框以及设置组件的流程布局:

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

我刚刚跳过了组件的包和声明。程序运行状况良好

提前致谢。

最佳答案

最后你可能需要调用 pack()......我想如果你想要换行的房间类型比你需要使用 gridlayout 但不要忘记最后调用 pack() 。

关于Java : Components position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com