如何使用Java在JPanel中显示JTable? [英] How to display a JTable in a JPanel with Java?

查看:272
本文介绍了如何使用Java在JPanel中显示JTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Java在JPanel中显示JTable?

How to display a JTable in a JPanel with Java?

推荐答案

作为练习留给用户的导入和表格模型这段代码。此外,为简单起见,任意选择面板布局。

Imports and table model left as an exercise to the user of this code. Also, the panel layout is arbitrarily chosen for simplicity.

public class JTableDisplay {
    public JTableDisplay() {
        JFrame frame = new JFrame("JTable Test Display");

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JTable table = new JTable();

        JScrollPane tableContainer = new JScrollPane(table);

        panel.add(tableContainer, BorderLayout.CENTER);
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new JTableDisplay();
    }
}

滚动窗格非常重要。没有它,如果内容大于显示,您的表将没有标题或滚动。

The scroll pane is fairly important to note. Without it, your table won't have a header or scroll if the content becomes larger than the display.

这篇关于如何使用Java在JPanel中显示JTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆