简体   繁体   中英

Java Swing Text Field Height

I am trying to write a GUI for a program I already finished which compiles various text files into a complete notebook. One feature of this GUI will be that it has multiple file browsing textareas and buttons in a scroll pane, and more can be added or removed with a plus or minus button.

The problem is, the textarea is defaulting to occupy the remaining space, even if I set a preferred size. Is there any way I can get it to be just the same height as the button or near it at least?

Here is what my GUI looks like now.

Here is my code.

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);
    }
}

Sorry if it is a simple question, this is my first time straying from console programs in Java.

Thanks so much.

Use GridbagLayout instead of BoxLayout. I think it is more useful. Sample code is below

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 please take a look to A Visual Guide to Layout Managers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM