java 如何在 JTable Swing 中增加标题栏的字体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7442555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:00:01  来源:igfitidea点击:

How to increase the Font of the title column in JTable Swing?

javaswingfontsjtablejtableheader

提问by Daniel Pereira

I would like to know how to increase the Font size of the title column in JTable Swing?

我想知道如何增加 JTable Swing 中标题栏的字体大小?

I'm usning Netbeans.

我正在使用 Netbeans。

Best regards.

最好的祝福。

Daniel

丹尼尔

回答by Damian Leszczyński - Vash

You just need to call the getTableHeader()method. Then on the object of class JTableHeaderuse the setFont(/*font*/)method to set new font.

您只需要调用该getTableHeader()方法。然后在类的对象上JTableHeader使用该setFont(/*font*/)方法设置新字体。

table.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 15 ));

回答by camickr

To keep the same Font family and just change the size you can use:

要保持相同的字体系列并更改大小,您可以使用:

JTableHeader header = table.getTableHeader();
header.setFont( header.getFont().deriveFont(16) );

回答by mKorbel

not sure from your question, then I post both options

不确定你的问题,然后我发布了两个选项

1) set Font for JTable myTable.setFont(new Font("Arial", Font.PLAIN, 10))

1) 为 JTable 设置字体 myTable.setFont(new Font("Arial", Font.PLAIN, 10))

2) set Font for TableHeader

2) 为 TableHeader 设置字体

    final TableCellRenderer tcrOs = table.getTableHeader().getDefaultRenderer();
    table.getTableHeader().setDefaultRenderer(new TableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));
            lbl.setHorizontalAlignment(SwingConstants.LEFT);
            if (isSelected) {
                lbl.setForeground(Color.red);
                lbl.setFont(new Font("Arial", Font.BOLD, 12));
            } else {
                lbl.setForeground(Color.darkGray);
                lbl.setFont(new Font("Arial", Font.PLAIN, 10));
            }
            return lbl;
        }
    });