为什么我的 JTable 没有出现在我的 JFrame (Java) 中?
JTable 未在 JFrame (Java) 中显示
问题:
JTable 未出现在 JFrame 中,尽管被添加到
调查:
提供的代码尝试使用 add(tbl_Accounts) 和 add(scrollPane) 将 JTable 添加到 JFrame。然而,问题似乎出在其他地方。
根本原因:
正如评论中提到的,使用 setLayout(null) 可能会导致组件放置问题。虽然表格已添加到 JFrame,但由于布局管理不正确而未显示。
解决方案:
要解决此问题,应该使用适当的布局管理器用过的。在此示例中,可以采用 BorderLayout 和 GridLayout 的组合。
修改后的代码:
以下修改后的代码使用 GridLayout 作为主面板,使用 BorderLayout 作为主面板。顶部面板和底部面板的 BoxLayout:
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; public class JTableFrameExample { private JFrame frame; private JPanel mainPane; private JPanel topPane; private JPanel tablePane; private JPanel bottomPane; // ... (code continues as before) public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new JTableFrameExample().createAndShowGui(); } }); } public void createAndShowGui() { frame = new JFrame(getClass().getSimpleName()); int rows = 30; int cols = 3; String[][] data = new String[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { data[i][j] = i + "-" + j; } } String[] columnNames = { "Column1", "Column2", "Column3" }; table = new JTable(data, columnNames); scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); table.setPreferredScrollableViewportSize(new Dimension(420, 250)); table.setFillsViewportHeight(true); frame.setLayout(new BorderLayout()); topPane = new JPanel(); topPane.setLayout(new BorderLayout()); JLabel selectAccountLabel = new JLabel("Select Account"); topPane.add(selectAccountLabel, BorderLayout.WEST); JButton selectAccountButton = new JButton("Select Account"); topPane.add(selectAccountButton, BorderLayout.EAST); frame.add(topPane, BorderLayout.NORTH); tablePane = new JPanel(); tablePane.add(scroll); frame.add(tablePane, BorderLayout.CENTER); bottomPane = new JPanel(); bottomPane.setLayout(new GridLayout(0, 5, 3, 3)); // ... (code continues as before) frame.add(bottomPane, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
在此更新的代码中,BoxLayout 已已删除。这允许底部窗格中的组件以更像网格的方式排列。框架中的 BorderLayout 已用于 topPane 和 tablePane 组件,可以轻松放置这些元素。
通过这些更改,JTable 现在应该在 JFrame 中正确显示。
以上是为什么我的 JTable 没有出现在我的 JFrame (Java) 中?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

2025年的前4个JavaScript框架:React,Angular,Vue,Svelte

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?

Spring Boot Snakeyaml 2.0 CVE-2022-1471问题已修复

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?
