gpt4 book ai didi

java - 自下而上堆叠/插入 JComponent(类似于 CLI 的作用)

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

我正在使用 Swing 和 Graphics2d API 在 Java 中编写一个时尚的 CLI,但在反向堆叠任何给定输入的输出时遇到问题。换句话说,大多数布局管理器似乎都是从左到右或从上到下定位,并且它们似乎都无法插入到中间或两个 JComponent 之间,或者在添加/重新绘制新组件后将一个组件向上或向上移动/重新验证。非常感谢任何帮助或指导,因为我尝试过搜索但无法找到这个问题的答案。

这本质上就是我正在寻找的:

enter image description here

用户输入命令并按 Enter 键,输入的内容与需要遵循的其他内容一起输出,然后用户可以输入下一个命令。

最佳答案

most layout managers seem to position from Left to Right or Top to Bottom and none of them seem to be able to insert into the middle or between two JComponents

取决于您使用的布局管理器。您可以使用 FlowLayout、BoxLayout 或 GridLayout 并将组件插入到您想要的任何位置:

panel.add(component, index);

其他布局管理器(例如 BorderLayout 和 GridBagLayout)需要约束才能添加组件,因此您无法控制组件的顺序。

ADD a Swing component(s) above the input component

我猜想 BoxLayout 将是值得关注的布局。

关于java - 自下而上堆叠/插入 JComponent(类似于 CLI 的作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147384/

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