首页 > 解决方案 > 如何设置列的Jtable名称

问题描述

我有这段代码

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);

但它不起作用:/ 在此处输入图像描述 它显示这个,而列应该有名称:/

我怎样才能解决这个问题?

标签: javaswingjtable

解决方案


mainPanel.add(table);

仅当您将表格添加到滚动窗格时,表格标题才会自动显示。

代码应该是:

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

推荐阅读