gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:03:48 28 4
gpt4 key购买 nike

我正在做一个在文本文件中查找单词的程序。我正在使用 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/

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