在GridBagLayout中展开JTextArea

问题描述 投票:0回答:1

我从来没有使用GridBagLayout,但我确信它可能是其他东西。目前我正在尝试添加TextArea作为正在执行的任何代码的控制台视口窗口。就目前而言,TextArea是微不足道的,实际上看不到任何文字。

我尝试咨询这个page的建议之一,但我仍然无法正确地让JTextArea显示。

代码粘贴在下面,如果您还有其他需要,请告诉我。我还附上了一张图片,展示了它目前在做什么。编辑:setup();是一个空洞的身体。

@SuppressWarnings("serial")
public abstract class MainComponent extends JPanel {

protected String componentName = "DEMO";

private JLabel title;
private GridBagConstraints gbc;
private Insets spacing; 
private Font buttonFont;

/*
 * Redirection manipulation for the project.
 */
private JTextArea localConsole;

public MainComponent(Dimension dim) {

    setup();

    /* Set main body */
    setLayout(new GridBagLayout());
    gbc = new GridBagConstraints();
    spacing = new Insets(100,5,100,5);
    buttonFont = new Font("Consolas", Font.ITALIC, 22);

    /* Set title */
    title = new JLabel(componentName);
    title.setFont(new Font("Consolas", Font.BOLD, 48));
    gbc.fill = GridBagConstraints.CENTER;
    gbc.gridx = 1;
    gbc.gridy = 0;
    add(title, gbc);

    JButton run = new JButton("Run Project");
    run.setFont(buttonFont);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = spacing;
    gbc.gridx = 0;
    gbc.gridy = 2;
    add(run, gbc);

    JButton open = new JButton("Open Codebase");
    open.setFont(buttonFont);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = spacing;
    gbc.gridx = 1;
    gbc.gridy = 2;
    add(open, gbc);

    JButton exit = new JButton("Exit Program");
    exit.setFont(buttonFont);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = spacing;
    gbc.gridx = 2;
    gbc.gridy = 2;
    add(exit, gbc);

    /* Setup console for output */
    localConsole = new JTextArea();
    DefaultCaret caret = (DefaultCaret) localConsole.getCaret();
    caret.setUpdatePolicy(2);
    localConsole.setEditable(false);
    localConsole.setFont(new Font("Consolas", Font.PLAIN, 16));
    localConsole.setWrapStyleWord(true);
    localConsole.setSize(new Dimension(400, 200));

    JScrollPane scrollPane = new JScrollPane(localConsole);
    scrollPane.setVerticalScrollBarPolicy(22);
    scrollPane.setSize(new Dimension(400, 200));
    gbc.fill = GridBagConstraints.CENTER;
    gbc.insets = new Insets(200, 0, 20, 0);
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 5;
    gbc.gridheight = 2;
    add(scrollPane, gbc);

}

Empty TextArea

java swing jtextarea gridbaglayout
1个回答
1
投票

给我们一张它目前所做的事情的图片并没有多大帮助,因为我们不知道你想要达到什么目标而难以提出一个确切的建议。

所以看到的是尝试:

  1. 在顶部显示3个按钮
  2. 有一个文本区域填充底部的剩余空间

所以我建议您永远不需要使用单个布局管理器。很多时候,布局管理器的组合更容易。

所以我建议你:

  1. 使用BorderLayout作为主要布局。
  2. 为按钮创建一个面板并添加到BorderLayout的Page_START
  3. 将滚动窗格添加到BorderLayout的CENTER

所以基本代码是:

setLayout( new BorderLayout() );

JPanel buttonsPanel = new JPanel();
buttonsPanel.add(run);
buttonsPane.add((open);
buttonsPanel.add(exit);
add(buttonsPanel, BorderLayout.PAGE_START);

JTextArea textArea = new JTextArea(10, 30); // give text area a default preferred size
add(new JScrollPane(textArea), BorderLayout.CENTER);

比尝试使用GridBagLayout更简单,更少的代码。

但是,如果您想要使用GridBagLayout,那么首先阅读How to Use GridBagLayout上的Swing教程,了解有关约束的更多信息。

基本代码可能是这样的:

gbc.gridx = 0;
gbc.gridy = 0;
add(run, gbc);

gbc.gridx = 1;
gbc.gridy = 0;
add(open, gbc);

gbc.gridx = 2;
gbc.gridy = 0;
add(exit, gbc);

localConsole = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(localConsole);

gbc.fill = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3; // try this with 1 and 2 to see the difference.
add(scrollPane, gbc);

那就是你不能随意使用gridx / gridy / gridwidth / gridheight值。

组件需要在网格中显示。你不能在网格中留下空白。

因此,如果您想要连续按钮,则需要按顺序增加gridx以获得相同的gridy值。

如果您希望按钮下方的文本区域按顺序递增贪婪并以新的gridx值开始。

© www.soinside.com 2019 - 2024. All rights reserved.