【发布时间】:2023-03-17 19:55:01
【问题描述】:
我正在尝试将一个数字数组输入到 JTable 中,但遇到了问题。即错误'java.lang.double[][] 无法转换为javax.swing.TableModel' 我假设问题与JTable 无法读取双精度但一些澄清或修复将非常有帮助谢谢。
// Data to be displayed in the JTable,
double[][] mile = {
{1},
{2},
{3 },
{4 },
{5 },
{6 },
{7 },
{8 },
{9 },
{10 },
{11 },
{12 },
{13 },
{14 },
{15 },
{16 },
{17 },
{18 },
{19 },
{20 },
};
double[][] kilo = {
{ 1.609 },
{ 3.218 },
{ 4.827 },
{ 6.436 },
{ 8.045 },
{ 9.654 },
{ 11.263 },
{ 12.872 },
{ 14.481 },
{ 16.09 },
{ 17.699 },
{ 19.308 },
{ 20.917 },
{ 22.526 },
{ 24.135 },
{ 25.825 },
{ 27.434 },
{ 29.043 },
{ 30.654 },
{ 32.261 },
};
// Column Names for the table.
String[] titles = { "Miles", "Kilometers" };
// Create the table which is going to display the information.
JTable table = new JTable(mile, kilo, titles);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(20,20,650,250);
table.setFillsViewportHeight(true);
infoPanel.add(scrollPane);
【问题讨论】:
-
1)
scrollPane.setBounds(20,20,650,250);Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2)为了尽快获得更好的帮助,edit添加minimal reproducible example或Short, Self Contained, Correct Example。 -
在不知道您希望它的外观的情况下,很难回答。 但是猜测你想组合这两个数组:
{1, 1.609}, {2, 3.218}, ...得到你想要的,因为它们成为你表格的行。 -
@BeUndead 是的,我愿意,但目前我的问题是该表不接受双精度并抛出错误代码。我猜你的第一反应是回答这个问题?
-
是的。
JTable的构造函数(上面的链接)采用 one 数组(数组),它成为表中的数据。你用 2 调用它,它没有构造函数(因此编译器错误)。
标签: java swing compiler-errors jtable