我正在尝试设计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);
}}
答案 0 :(得分:5)
问题是,您已将表添加到内容窗格中。这有效地将其从滚动窗格中删除,滚动窗格负责显示表列标题。
删除以下行
Container cp = getContentPane();
cp.add(table);
答案 1 :(得分:0)
package com.tom.mom;
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);
}
}
因为你已经扩展了JFrame并且你正在使用类的构造函数,它只是一个JFrame,因此你处于那个不需要获取区域并放置组件的位置。当我们这样做时在构造函数中编码,然后我们不采用getContentPane并放置我们的组件。当你在构造函数之外工作并试图将一些组件放在框架中时,它是必需的