我创建了一个显示项目列表的 GUI 类。当用户选择一个项目时,它会弹出一个新窗口,其中显示一个 JTable,该窗口应该提供该项目的详细信息(两列:左侧为参数,右侧为值)。这(有点)有效,直到我决定将表类中的所有方法和变量设为非静态,以防用户想要同时打开多个表窗口。
然后它就停止工作了。表格不会显示。所有代码都执行了,但表是空的,即使在我使用其模型的 add 方法之后也是如此。
(另外,我很感激有关 GUI 更好想法的评论,主要是因为其中一个字段称为“描述”并且可能很长。我正在为自己编写应用程序,因此功能是第一要务)
编辑:这是一些代码,请随时向我询问更多信息。 (我一开始就没有复制它,因为我认为它很简单)
JTable 类,从第 82 行开始 - 其余的都是自动生成的。
public void main(ArrayList<String> l, ArrayList<String> d) {
labels = l;
data = d;
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PopupTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PopupTable().setVisible(true);
model.addColumn("Description");
model.addColumn("Value");
for (int i = 0; i < data.size(); i++) {
Object[] r = {labels.get(i), data.get(i)};
System.out.println(labels.get(i) + data.get(i));
model.addRow(r);
}
Object[] asd = {"Name", "Skelet"};
model.addRow(asd);
}
});
}
public static void asd() {
System.out.println("Bazinga!");
}
private ArrayList<String> labels;
private ArrayList<String> data;
private DefaultTableModel model = new DefaultTableModel();
;
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table;
// End of variables declaration
}
另一个类通过发送两个列表来调用这个类的“main”方法,一个用于字段数据,另一个用于每个数据的描述(第一个将发送“2000”,第二个将发送“经验”)。正如我所说,如果变量和方法是静态的,一切都会正常。如果不是,则一切正常,但表格除外,该表格不会显示。
这是外部类的代码,我用它来创建 Table 类:
new PopupTable().main(list1, list2);
最佳答案
我刚刚注意到 run() 方法有:
new PopupTable().setVisible(true);
所以我删除了它,然后像这样运行该类:
PopupTable pt = new PopupTable();
pt.main(MagiciaFileManipulator.getMetadata(), itemList.get(0));
pt.setVisible(true);
现在一切都很完美!我不敢相信它是如此简单,我以为我查看了所有内容并错误地得出结论这是 JTables 和静态/非静态的根本问题。
关于java - 非静态对象中的 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12656903/