gpt4 book ai didi

java - JTable 彩色行标题出现在表格未出现的位置

转载 作者:行者123 更新时间:2023-12-01 22:36:18 27 4
gpt4 key购买 nike

我正在创建一个包含 3 列的 JTable,并将标题的颜色设置为 color.CYAN。

问题是,JTable 结束后,该行的其余部分仍然是青色,即使那里根本不应该有表。

我尝试修复列数,但似乎无法做到。

下面是我的代码:

class SimpleTableExample extends JFrame {

public static JFrame frame = new JFrame();

public static void frameGui(JPanel panel, String name){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setSize(500,200);
frame.setVisible(true);
}

public static JPanel Table(){
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
String columnNames[] = { "Column 1", "Column 2", "Column 3" };
String dataValues[][] =
{
{ "12", "234", "67" },
{ "-123", "43", "853" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "279", "9033", "3092" }
};

JTable table = new JTable( dataValues, columnNames );
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
JTableHeader header = table.getTableHeader();
header.setBackground(Color.cyan);
JScrollPane scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
return topPanel;
}

public static void main( String args[] )
{
frameGui(Table(),"TEST");
}
}

最佳答案

这将是预期的行为,因为 JTableHeader 扩展了 JScrollPane header 区域的整个宽度。

相反,请更改 JTableHeader 的渲染器,就像更改 JTable 的 dell 渲染器

看看JTableHeader#setDefaultRenderer了解更多详情

关于java - JTable 彩色行标题出现在表格未出现的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824695/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com