java - 使用 GridBayLayout 时 JTextArea 高度只有 1 行

标签 java swing layout-manager gridbaglayout

我正在做一个在文本文件中查找单词的程序。我正在使用 GridBagLayout 来定位元素。当我运行程序时,文本区域只显示一行。即使设置了 JTextArea results = new JTextArea(30, 30)

这是它现在显示的内容: enter image description here

我正在尝试做这样的事情: enter image description here

Java代码:

public class WordFinder extends JFrame {

    private WordList words = new WordList();

    private static final int WINDOW_WIDTH = 380;    
    private static final int WINDOW_HEIGHT = 380;  
    private static final int TEXT_WIDTH = 30; 

    private JLabel findLabel = new JLabel("Find:"); 
    private JLabel wordsContaining = new JLabel("words containing");    
    private JTextField findWord = new JTextField(TEXT_WIDTH);
    private JButton clear = new JButton("Clear");
    private JTextArea results = new JTextArea(30, 30);
    private JScrollPane scroll = new JScrollPane(results);
    private JFileChooser chooseFile = new JFileChooser();
    private JPanel pane = new JPanel(new GridBagLayout());

    public WordFinder() {

        super("Word Finder");

        // Initialize the menu bar
        //initMenu();

        results.setEditable(false);

        pane.setLayout(new GridBagLayout());
        pane.setBorder(new EmptyBorder(15, 20, 0, 10));
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.PAGE_START;
        results.setLineWrap(true);
        results.setWrapStyleWord(true);
        scroll.setViewportView(results);

        // Add label "Find"
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(findLabel, c);

        // Add text field
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 0;
        pane.add(findWord, c);

        // Add clear button
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = .1;
        c.gridx = 2;
        c.gridy = 0;
        pane.add(clear, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(5, 5, 0, 0);
        pane.add(wordsContaining, c);

        // Add text area
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 1;
        c.gridx = 1;
        c.gridy = 3;
        c.insets = new Insets(0, 3, 0, 5);
        pane.add(scroll, c);

        add(pane);
        setVisible(true);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);    
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run () {
                new WordFinder().show();
            }
        });
    }    
}

关于我缺少什么的任何想法?还是我做错了什么?

最佳答案

  1. c.fill = GridBagConstraints.HORIZONTAL; 更改为 c.fill = GridBagConstraints.BOTH;
  2. 使用pack代替setSize

例如

Example

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class WordFinder extends JFrame {

    private static final int WINDOW_WIDTH = 380;
    private static final int WINDOW_HEIGHT = 380;
    private static final int TEXT_WIDTH = 30;

    private JLabel findLabel = new JLabel("Find:");
    private JLabel wordsContaining = new JLabel("words containing");
    private JTextField findWord = new JTextField(TEXT_WIDTH);
    private JButton clear = new JButton("Clear");
    private JTextArea results = new JTextArea(30, 30);
    private JScrollPane scroll = new JScrollPane(results);
    private JFileChooser chooseFile = new JFileChooser();
    private JPanel pane = new JPanel(new GridBagLayout());

    public WordFinder() {

        super("Word Finder");

                // Initialize the menu bar
        //initMenu();
        results.setEditable(false);

        pane.setLayout(new GridBagLayout());
        pane.setBorder(new EmptyBorder(15, 20, 0, 10));
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.PAGE_START;
        results.setLineWrap(true);
        results.setWrapStyleWord(true);
        scroll.setViewportView(results);

        // Add label "Find"
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(findLabel, c);

        // Add text field
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 0;
        pane.add(findWord, c);

        // Add clear button
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = .1;
        c.gridx = 2;
        c.gridy = 0;
        pane.add(clear, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(5, 5, 0, 0);
        pane.add(wordsContaining, c);

        // Add text area
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;
        c.gridx = 1;
        c.gridy = 3;
        c.insets = new Insets(0, 3, 0, 5);
        pane.add(scroll, c);

        add(pane);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new WordFinder().setVisible(true);
            }
        });
    }
}

您遇到的问题是由fill 属性GridBagConstraints.HORIZONTALsetSize 的组合引起的。当您使用 setSize 时,容器的大小小于 JScrollPane'preferredSize 并且布局管理器求助于它的 minimumSize 代替。

通过使用 GridBagConstraints.BOTH,您允许布局管理器扩展组件以填充单元格的整个可用空间,而不管

关于java - 使用 GridBayLayout 时 JTextArea 高度只有 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225700/

相关文章:

java - static volatile boolean - 线程没有被终止

java - 什么是最好的 GUI builder for swing

java - JTextField 宽度

java - 尝试使用JButton

Java swingworker() process() 没有被调用

java - 调试 GridBagLayout - 组件聚集到中心

java - 让JFrame显示其他JFrame而不打开新窗口

java - ExpandableListView:从子项获取输入值

java - 如何设置 jackson 为假时忽略 boolean 属性?

java - 设置recyclerView项目高度wrap_content