簡體   English   中英

如何在JPanel中左對齊秋千組件

[英]How to left align the swings components in a JPanel

我有以下揮桿組件。 請看圖片。 在這里,標簽和組合框以及標簽和文本字段未正確對齊。 知道是什么原因導致此問題嗎?

標簽和組合框類別未正確對齊

我在面板中使用GridBagLayout。 然后將諸如label和combobox以及textfield之類的組件添加到面板中。

以下是我使用的代碼。

    srchCategoryLbl = new JLabel("Category");
    categoryCmb = new JComboBox<>();
    categoryCmb.setPreferredSize(dimensionTxt);
    categoryCmb.setBounds(0, 0, 0, 0);

    srchProductCodeLbl = new JLabel("Product Code");
    productCodeTxt = new JTextField();
    productCodeTxt.setPreferredSize(dimensionTxt);

    srchProductDescLbl = new JLabel("Product Desc");
    productDescTxt = new JTextField();
    productDescTxt.setPreferredSize(dimensionTxt);

    searchBtn = new JButton("Search");

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.WEST;
    searchPanel.add(srchCategoryLbl, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    searchPanel.add(categoryCmb, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    searchPanel.add(srchProductCodeLbl, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    searchPanel.add(productCodeTxt, gbc);

    gbc.gridx = 2;
    gbc.gridy = 0;
    searchPanel.add(srchProductDescLbl, gbc);

    gbc.gridx = 2;
    gbc.gridy = 1;
    searchPanel.add(productDescTxt, gbc);

    gbc.gridx = 3;
    gbc.gridy = 1;
    searchPanel.add(searchBtn, gbc);

選擇一個字段時,可以看到MacOS在該字段周圍放置了一個“焦點”矩形,這是通過使用某種自定義Border來實現的。

領域

您可以將邊界設置為null (或其他方式),但這可能會以意外的方式影響其他外觀,並以他們可能不欣賞的方式改變用戶體驗

更好的解決方案是使用GridBagConstraints#insets屬性將標簽稍稍墊上...

一些填充

JLabel srchCategoryLbl = new JLabel("Category");
JComboBox<Object> categoryCmb = new JComboBox<>();
categoryCmb.setPrototypeDisplayValue("This is a really long test string");

JLabel srchProductCodeLbl = new JLabel("Product Code");
JTextField productCodeTxt = new JTextField(20);

JLabel srchProductDescLbl = new JLabel("Product Desc");
JTextField productDescTxt = new JTextField(20);

JButton searchBtn = new JButton("Search");

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Insets labelInsets = new Insets(0, 4, 0, 4);
Insets fieldInsets = new Insets(0, 0, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = labelInsets;
add(srchCategoryLbl, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = fieldInsets;
add(categoryCmb, gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = labelInsets;
add(srchProductCodeLbl, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = fieldInsets;
add(productCodeTxt, gbc);

gbc.gridx = 2;
gbc.gridy = 0;
gbc.insets = labelInsets;
add(srchProductDescLbl, gbc);

gbc.gridx = 2;
gbc.gridy = 1;
gbc.insets = fieldInsets;
add(productDescTxt, gbc);

gbc.gridx = 3;
gbc.gridy = 1;
add(searchBtn, gbc);

同樣,這是跨平台UI的不幸問題之一,因為您將需要設計一種方法,通過該方法可以根據當前平台確定所需的插圖。

我也強烈建議您避免使用setPreferredSize因為setPreferredSize很多時間來計算組件的適當大小,而要使用已經可用的功能(例如JTextFieldsetColumns ),有關一些信息,請參見上面的代碼。例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM