簡體   English   中英

Java Swing文本字段高度

[英]Java Swing Text Field Height

我正在嘗試為已經完成的程序編寫GUI,該程序將各種文本文件編譯成一個完整的筆記本。 此GUI的功能之一是,它在滾動窗格中具有多個文件瀏覽文本區域和按鈕,並且可以使用加號或減號按鈕添加或刪除更多內容。

問題是,即使我設置了首選大小,textarea也默認占用剩余空間。 有什么辦法可以使它與按鈕高度相同或至少接近按鈕?

這是我的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));

安娜,請看一下《布局管理器的視覺指南》

暫無
暫無

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

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