如何设置列的Jtable名称

How to set Jtable name of columns

我有这段代码

String [] titles = {"حذف", "شماره درس","گروه","واحد","نام","تعداد ثبت نامی","نام استاد","زمان امتحان","زمان کلاس","وضعیت پیشنیازی ها","محدودیت"};
    String[][] value = new String[1][11];
    JTable table = new JTable(value, titles);
    table.setBounds(800,450,1100,80);
    table.setBorder(BorderFactory.createLineBorder(Color.black, 2, true));
    table.setRowHeight(40);
    table.setForeground(Color.BLACK);
    table.setBackground(Color.lightGray);
    mainPanel.add(table);

但它不起作用:/ 它显示这个,而列应该有名称:/

我该如何解决这个问题?

mainPanel.add(table);

只有当您将 table 添加到滚动窗格时,table header 才会自动显示。

代码应该是:

//mainPanel.add(table);
JScrollPane scrollPane = new JScrollPane( table );
mainPanel.add( scrollPane );