gpt4 book ai didi

java - 列名未显示在 JTable 中

转载 作者:行者123 更新时间:2023-12-01 17:17:10 25 4
gpt4 key购买 nike

我正在尝试设计 JTable,但列名称没有显示,我不知道为什么要声明数组,有人可以看一下这段代码table = new JTable(data, columnNames); 我试图查看 java 教程,但现在找不到它

package AnimeAid;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;


/**
*
* @author isslam
*/
public class GuiInterface extends JFrame {
JTable table;

public static void main(String[] args) {
GuiInterface is = new GuiInterface("t");
is.setVisible(true);
}

public GuiInterface(String title){
setSize(900, 700);
setTitle(title);
setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
String[] columnNames = {"#","Start","End","Translation column"};
Object[][] data = {
{"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
"full name for the record."},
{"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};

table = new JTable(data, columnNames);
table.setFillsViewportHeight(true);
table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
TableColumn columnA = table.getColumn("#");
columnA.setMinWidth(10);
columnA.setMaxWidth(20);
TableColumn columnB= table.getColumn("Start");
columnB.setMinWidth(80);
columnB.setMaxWidth(90);

TableColumn columnC= table.getColumn("End");
columnC.setMinWidth(80);
columnC.setMaxWidth(90);


JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);


//contaner part
Container cp = getContentPane();
cp.add(table);



}}

最佳答案

问题是,您已将表格添加到内容 Pane 中。这有效地将其从负责显示表格列标题的滚动 Pane 中删除。

删除以下行

    Container cp = getContentPane();
cp.add(table);

关于java - 列名未显示在 JTable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21171441/

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