java - 在 JPanel 中对齐 JTextArea

标签 java swing layout-manager

我目前正在使用 Java Swing 构建 GUI。

我当前的代码产生了这个。 Output

Product List 的 JTextArea 使 GUI 看起来很尴尬,我怎样才能使 JTextArea 看起来像这样,它似乎有一个额外的行: Desired Output

我使用的 GroupLayout 代码是:

 gr.setVerticalGroup(gr.createSequentialGroup()
    .addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(productName).addComponent(productText).addComponent(productList))
    .addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(amount).addComponent(amountText).addComponent(prodScroll))
    .addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(description).addComponent(desScroll))
    .addGroup(gr.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(addButton).addComponent(remButton)));

    gr.setHorizontalGroup(gr.createSequentialGroup()
            .addGroup(gr.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(productName).addComponent(amount).addComponent(description).addComponent(addButton))
            .addGroup(gr.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(productText).addComponent(amountText).addComponent(desScroll).addComponent(remButton))
            .addGroup(gr.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(productList).addComponent(prodScroll)));

最佳答案

我认为少数人会选择使用GridBagLayout .但是,我不喜欢它(与 GroupLayout 一样),因为它“难以使用”。我使用嵌套面板代替各种 Layout Managers .只使用 BorderLayoutGridLayout 你可以实现类似下面的例子,它完全可以调整大小,强调“交互”组件(我的意思是,没有理由调整大小一个常量文本 JLabel,对吧?)

我没有故意添加任何评论,因此您可以在打开文档的同时试验常量(和布局约束)并查看它们存在的原因。

代码:

public class NestedLayoutManagersExample extends JFrame {
    private static final long serialVersionUID = -7042997375941726246L;
    private static final int labelsWidth = 80;
    private static final int textFieldColumns = 15;
    private static final int spaceBetweenAllComponents = 10;

    public NestedLayoutManagersExample() {
        super("test");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel contentPane = new JPanel(new GridLayout(1, 2, 50, 50));
        contentPane.setBorder(BorderFactory.createEmptyBorder(spaceBetweenAllComponents, spaceBetweenAllComponents,
                spaceBetweenAllComponents, spaceBetweenAllComponents));
        setContentPane(contentPane);

        add(createLeftPanel());
        add(createRightPanel());

        setLocationByPlatform(true);
        pack();
    }

    private Component createRightPanel() {
        JPanel mainPanel = new JPanel(new BorderLayout());

        JLabel productListLabel = new JLabel("Product list");
        mainPanel.add(productListLabel, BorderLayout.PAGE_START);

        JList<String> productList = new JList<>();
        DefaultListModel<String> listModel = new DefaultListModel<>();
        Arrays.asList("Small Chair", "Big Chair", "Flying Chair").forEach(listModel::addElement);
        productList.setModel(listModel);

        JScrollPane listScrollPane = new JScrollPane(productList);
        mainPanel.add(listScrollPane, BorderLayout.CENTER);
        return mainPanel;
    }

    private Component createLeftPanel() {

        JPanel mainPanel = new JPanel(new BorderLayout(spaceBetweenAllComponents, spaceBetweenAllComponents));

        JPanel topPanel = new JPanel(new GridLayout(2, 1, spaceBetweenAllComponents, spaceBetweenAllComponents));
        topPanel.add(createStraightPanel("Product Name"));
        topPanel.add(createStraightPanel("Amount"));
        mainPanel.add(topPanel, BorderLayout.PAGE_START);

        JPanel centerPanel = new JPanel(new BorderLayout());

        JLabel label = new JLabel("<html><p style='width:" + labelsWidth + "px';> Description");
        label.setVerticalAlignment(JLabel.TOP);
        centerPanel.add(label, BorderLayout.LINE_START);

        centerPanel.add(createTextAreaPanel());

        mainPanel.add(centerPanel, BorderLayout.CENTER);
        return mainPanel;
    }

    private JPanel createTextAreaPanel() {
        JPanel mainPanel = new JPanel(new BorderLayout(spaceBetweenAllComponents, spaceBetweenAllComponents));

        JTextArea textArea = new JTextArea(1, textFieldColumns);

        JScrollPane textAreaScrollPane = new JScrollPane(textArea);
        mainPanel.add(textAreaScrollPane, BorderLayout.CENTER);

        JPanel buttonsPanel = new JPanel(new BorderLayout());

        JButton addButton = new JButton("Add");
        buttonsPanel.add(addButton, BorderLayout.LINE_START);

        JButton removeButton = new JButton("Remove");
        buttonsPanel.add(removeButton, BorderLayout.LINE_END);

        mainPanel.add(buttonsPanel, BorderLayout.PAGE_END);
        return mainPanel;
    }

    private Component createStraightPanel(String labelText) {
        JPanel mainPanel = new JPanel(new BorderLayout());

        JLabel label = new JLabel("<html><p style='width:" + labelsWidth + "px';>" + labelText);
        mainPanel.add(label, BorderLayout.LINE_START);

        JTextField textField = new JTextField(textFieldColumns);

        mainPanel.add(textField, BorderLayout.CENTER);
        return mainPanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new NestedLayoutManagersExample().setVisible(true));
    }
}

预览:

preview

关于java - 在 JPanel 中对齐 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59068440/

相关文章:

java - 设置游戏计时器

java - H2数据库备份恢复序列已存在异常

java - 不同列的网格颜色不同

java - setPreferredSize 事后更改,但 GridBagLayout 未更新

java - 在 Swing 面板之间传递信息?

java - GridBagLayout 动态使用面板内的额外空间

java - 查看解析文件输出的方法?

java - 如何用Java打印扑克中所有可能的 "full houses"?

Java:GridLayout 开头的间隙

Java使用grouplayout对齐3个面板