gpt4 book ai didi

java - 在 GridBagLayout 中展开 JTextArea

转载 作者:行者123 更新时间:2023-12-02 10:01:38 24 4
gpt4 key购买 nike

我从未使用过 GridBagLayout 但我确信它可能是其他东西。目前,我正在尝试添加一个 TextArea 来充当正在执行的任何代码的控制台视口(viewport)窗口。就目前而言,TextArea 很小,实际上看不到任何文本。

我尝试咨询此page对于建议之一,但我仍然无法正确显示 JTextArea。

代码粘贴在下面,如果您需要其他内容,请告诉我。我还附加了一张显示其当前正在执行的操作的图片。编辑:setup();是一个空的 body 。

@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

最佳答案

向我们提供其当前功能的图片并没有多大帮助,因为我们不知道您想要实现什么目标,因此很难提出准确的建议。

所以我们看到的是尝试:

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

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

所以我建议你:

  1. 使用 BorderLayout 作为主布局。
  2. 为按钮创建一个面板并将其添加到 BorderLayout 的 Page_START
  3. 将滚动 Pane 添加到 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 值开始。

关于java - 在 GridBagLayout 中展开 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55578450/

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