为JTable添加标题 [英] Add a title to a JTable
问题描述
我有一个使用TableModel创建的JTable JTable t = new JTable(tableModel)
我想为它添加一个标题。我希望像 t.setTitle(graphTitle);
之类的东西,但我无法在那条线上找到任何东西。如果标题位于表格的顶部或下方,我不介意。
我使用的是JLabel,但它看起来很乱。
I have a JTable which is created using a TableModel JTable t = new JTable(tableModel)
I want to add a title to it. I was hoping for something like t.setTitle(graphTitle);
but i cant find anything on that lines. I dont mind if the title is on top or below the table.
I was using JLabels but it just looks messy.
任何人都可以帮忙吗?
提前干杯
Can anyone help? Cheers in advance
推荐答案
您可以考虑的其他选项是附上 JTable
在 JPanel
中并将 TitledBorder
设置为 JPanel
。
Another option you could consider is enclosing the JTable
in a JPanel
and setting a TitledBorder
to that JPanel
.
喜欢这样:
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class TableTitle
{
public TableTitle ()
{
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel ();
panel.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
"Table Title",
TitledBorder.CENTER,
TitledBorder.TOP));
JTable table = new JTable (3, 3);
panel.add (table);
frame.add (panel);
frame.setLocationRelativeTo (null);
frame.pack ();
frame.setVisible (true);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override public void run ()
{
TableTitle t = new TableTitle ();
}
});
}
}
看起来像这样:
这篇关于为JTable添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!