![](/img/trans.png)
[英]How do I make generic layouts to include in other layouts but change the properties inside the generic layouts dynamically in android app development?
[英]How do I make the layouts in rows?
我正在嘗試使布局穩定和固定。 當我調整窗口大小時,它只給我一行而不是實際的行布局。 如何使布局整潔?
這是我的代碼:
class BookstoreFrame extends JFrame
{
JButton btnSubmit;
JTextField txtISBN, txtTitle, txtAuthor, txtPrice;
JLabel lblISBN, lblTitle, lblAuthor, lblPrice;
int count = 0;
public BookstoreFrame(String title)
{
FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 5, 20);
setLayout(layout);
lblISBN = new JLabel("ISBN: ");
txtISBN = new JTextField(10);
lblTitle = new JLabel("Book Title: ");
txtTitle = new JTextField(10);
lblAuthor = new JLabel("Author: ");
txtAuthor = new JTextField(10);
lblPrice = new JLabel("Price: ");
txtPrice = new JTextField(10);
btnSubmit = new JButton("Submit");
add(lblISBN);
add(txtISBN);
add(lblTitle);
add(txtTitle);
add(lblAuthor);
add(txtAuthor);
add(lblPrice);
add(txtPrice);
add(btnSubmit);
btnSubmit.addActionListener(new seeTextBookInfo());
}
}
您正在使用FlowLayout,它將在一個方向上添加組件,直到沒有更多空間,然后創建新行。
您可以使用BoxLayout從左到右添加元素: https : //docs.oracle.com/javase/8/docs/api/javax/swing/BoxLayout.html
BoxLayout layout = BoxLayout(this, BoxLayout.X_AXIS);
如何使布局整潔?
我們不知道“整潔”對您意味着什么?
通常,您將在一行上放置標簽/文本字段對,然后在其自己的行上放置按鈕。
我建議您可能要使用GridBagLayout
因為它允許您為組件提供靈活的行和列網格。
基本代碼可能類似於:
setLayout( new GridBagLayout() );
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(lblISBN, gbc);
gbc.gridx = 1;
add(txtISBN, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(lblTitle);
gbc.gridx = 1;
add(txtTitle);
... // add other components here
gbc.gridx = 0;
gbc.gridy = ?;
gbc.gridwidth = 2;
gbc.anchor = ???
add(btnSubmit);
閱讀Swing教程中有關如何使用GridBagLayout的部分,以獲取更多信息和示例,以幫助您入門。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.