java - 如何将 JTextArea 放在另一个 JTextArea 旁边?
问题描述
我正在尝试在 GUI 中将一个并排JTextArea
放置JTextArea
我正在为数据库编写 GUI,并希望将每列中的数据放在不同的 JTextArea 中。这将使我的 GUI 看起来更好,并且更容易查看数据。我已经尝试将其添加JTextAreas
到 a JPanel
,但这似乎不起作用。
这是我迄今为止尝试过的:
public class GUIDisplayBooks extends JFrame{
JPanel panel = new JPanel();
JTextArea textAreaIsbn = new JTextArea();
JTextArea textAreaTitle = new JTextArea();
JTextArea textAreaSurname = new JTextArea();
JTextArea textAreaForename = new JTextArea();
JTextArea textAreaCategory = new JTextArea();
JScrollPane scrollPane = new JScrollPane(panel);
GUIDisplayBooks(ArrayList<Book> books)
{
this.add(panel);
this.setSize(600,200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
for(Book book : books){
textAreaIsbn.append(book.getIsbn() + "\n");
textAreaTitle.append(book.getTitle() + "\n");
textAreaSurname.append(book.getSurname() + "\n");
textAreaForename.append(book.getForename() + "\n");
textAreaCategory.append(book.getCategory() + "\n");
}
panel.add(textAreaIsbn);
panel.add(textAreaTitle);
panel.add(textAreaSurname);
panel.add(textAreaForename);
panel.add(textAreaCategory);
add(scrollPane);
}
}
我不断得到一个空白的 GUI 窗口。也许这真的很明显,任何帮助
解决方案
Swing 组件只能有一个父组件:
JScrollPane scrollPane = new JScrollPane(panel);
这个我基本一样:
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel);
然后你将同样的添加panel
到你的JFrame
:
this.add(panel);
它将它从中删除JScrollPane
,然后将空添加JScrollPane
到JFrame
:
add(scrollPane);
所以,删除这一行,应该让你的程序工作:
this.add(panel);
推荐阅读
- python - 用指定(和有序)键在字典中总结的最快方法
- haskell - 复合模式是否可以用于从树生成 HTML 并处理缩进,或者这本质上是不可能的?
- c - 将文件发送到远程 FTP 服务器
- laravel - 急切加载非直接关系laravel
- wordpress - Wordpress:如何使用 ACF 子字段进行自定义查询
- swift - 以编程方式添加 SearchBar 和范围 (iOS)
- amazon-web-services - AWS 根据 API 的结果自动扩展
- r - 我正在尝试以 wordcloud 格式显示前 50 个常用术语。我相信我在服务器代码上遗漏了一些东西
- reactjs - 尽管匹配了确切的路线,但 React Router 组件不会呈现
- go - 使用 rand.Seed() 填充结构映射仅插入最后一个对象