为什么 table 上面有一个A?
我将 JTable 放置在 JScrollPane 中以使其可滚动。 我需要放置一些方法吗? 但我没有放置字母 A,所以我无法追踪。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class Rawr {
private JFrame frame;
private JScrollPane scrollPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Rawr window = new Rawr();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Rawr() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
scrollPane = new JScrollPane();
scrollPane.setToolTipText("");
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scrollPane);
table = new JTable(100, 1);
table.setBorder(new LineBorder(new Color(0, 0, 0)));
scrollPane.setViewportView(table);
}
}
如何删除它?谢谢!
最佳答案
使用以下方式显式设置列名称:
String[] colNames = new String[]{"Your Column Name"};
DefaultTableModel defaultTableModel = new DefaultTableModel(colNames, 100);
table = new JTable(defaultTableModel);
如果使用 new JTable(100, 1)
创建表,您将看到 A
、B
等列标题,因为构造函数 javadoc 表示:
Constructs a
JTable
withnumRows
andnumColumns
of empty cells usingDefaultTableModel
.
因为 JTable
构造函数没有有关列标题的任何信息。它只能创建一个不知道任何列标题名称的 DefaultTableModel
。
DefaultTableModel
扩展了 AbstractTableModel
和 AbstractTableModel.getColumnName()
的 javadoc说
Returns a default name for the column using spreadsheet conventions: A, B, C, ... Z, AA, AB, etc. If
column
cannot be found, returns an empty string.
关于java - 如何删除 JScrollPane 内 JTable 顶部的 A?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536687/