gpt4 book ai didi

Java Swing 文本字段高度

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:07 25 4
gpt4 key购买 nike

我正在尝试为我已经完成的程序编写一个 GUI,该程序将各种文本文件编译成一个完整的笔记本。该 GUI 的一个功能是它在滚动 Pane 中具有多个文件浏览文本区域和按钮,并且可以使用加号或减号按钮添加或删除更多文件。

问题是,即使我设置了首选大小,文本区域也默认占据剩余空间。有什么方法可以让它与按钮高度相同或至少接近按钮吗?

这是我现在的 GUI 的样子。

这是我的代码。

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class GUI {

public static void main(String[] args) {

JFrame frame = new JFrame("Notebook Builder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));
contentPanel.setPreferredSize(new Dimension(860, 500));

JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
leftPanel.setPreferredSize(new Dimension(480, 500));

JScrollPane rightPanel = new JScrollPane();
rightPanel.setPreferredSize(new Dimension(480, 500));

frame.getContentPane().add(contentPanel);

contentPanel.add(leftPanel);
contentPanel.add(rightPanel);

JPanel headerPanel = new JPanel();
JLabel headerLabel = new JLabel("Directories");
headerLabel.setFont(new Font("serif", Font.BOLD, 32));
headerPanel.add(headerLabel);

JPanel directoryBrowserPanel = new JPanel();
directoryBrowserPanel.setLayout(new BoxLayout(directoryBrowserPanel, BoxLayout.Y_AXIS));
JScrollPane directoryBrowserPanelView = new JScrollPane(directoryBrowserPanel);

JPanel addOrRemoveButtonPanel = new JPanel();
addOrRemoveButtonPanel.setLayout(new BoxLayout(addOrRemoveButtonPanel, BoxLayout.X_AXIS));

JButton remove = new JButton("-");
JButton add = new JButton("+");

JPanel directoryBrowserFieldPanel = new JPanel();
directoryBrowserFieldPanel.setLayout(new BoxLayout(directoryBrowserFieldPanel, BoxLayout.X_AXIS));

JTextField filePathField = new JTextField();

JButton fileChooseButton = new JButton("Browse");

directoryBrowserFieldPanel.add(filePathField);
directoryBrowserFieldPanel.add(fileChooseButton);
addOrRemoveButtonPanel.add(remove);
addOrRemoveButtonPanel.add(Box.createRigidArea(new Dimension(80, 0)));
addOrRemoveButtonPanel.add(add);
directoryBrowserPanel.add(directoryBrowserFieldPanel);
directoryBrowserPanel.add(addOrRemoveButtonPanel);

JPanel buildButtonPanel = new JPanel();
JButton buildButton = new JButton("Build Notebook");
buildButton.setFont(new Font("serif", Font.PLAIN ,12));
buildButtonPanel.add(BorderLayout.CENTER, buildButton);

leftPanel.add(BorderLayout.NORTH, headerPanel);
leftPanel.add(BorderLayout.CENTER, directoryBrowserPanelView);
leftPanel.add(BorderLayout.SOUTH, buildButtonPanel);

frame.pack();
frame.setVisible(true);
}
}

抱歉,如果这是一个简单的问题,这是我第一次偏离Java控制台程序。

非常感谢。

最佳答案

使用 GridbagLayout 而不是 BoxLayout。我认为它更有用。示例代码如下

int xPos=0;
int yPos=0;
directoryBrowserFieldPanel.setLayout(new GridBagLayout());
directoryBrowserFieldPanel.add(filePathField, new GridBagConstraints(xPos++, yPos, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
directoryBrowserFieldPanel.add(fileChooseButton, new GridBagConstraints(xPos++, yPos, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));

ana请查看A Visual Guide to Layout Managers

关于Java Swing 文本字段高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24254679/

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