我对 Java 完全陌生,并且被分配了一项任务,我们必须创建一个图书库系统。该表必须有 7 列。但是我目前只能得到 2,然后代码就停止工作了。在下面的示例中,我尝试获取 3 列,但也失败了。有人能发现错误或我遗漏的任何内容吗?当运行表而不是数据时,它显示 [Ljava.lang.Object;@3dda9c8b
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mark
*/
public class BookDatabase_frame extends javax.swing.JFrame {
Object [][][] data = null;
String [] columnNames = new String [3];
/**
* Creates new form BookDatabase_frame
*/
public BookDatabase_frame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 80, 375, 275));
jButton1.setText("Initiate table");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 40, -1, -1));
jButton2.setText("Add row");
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, -1, -1));
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
columnNames = new String [] {"Name", "Country","age"};
data = new Object [1][2][3];
data [0][0][0]= JOptionPane.showInputDialog("Enter your Name");
data [0][1][0] = JOptionPane.showInputDialog("Enter your country");
data [0][0][2] = JOptionPane.showInputDialog("Enter your age");
jTable1.setModel(new DefaultTableModel(data, columnNames));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* 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(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.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 BookDatabase_frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
最佳答案
"The table must have 7 columns. however I can only currently get 2 and then the code stops working.
如果您想要 7 列,则让列名数组有 7 个字符串。
对于行,您只需要一维数组,而不是双维或三元数组。并将 actionPerformed
中的每一行单独添加到模型中。只需使用 0 行和列名称初始化模型即可。
String colNames = {"1", "2", "3", "4", "5", "6", "7"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); <-- 0 rows
....
public void actionPerformed(ActionEvent e){
// get 7 fields of data
Object[] row = {data1, data2, data3, data4, data5, data6, data7};
model.addRow(row);
}
我假设您不希望所有数据最初都位于 JTable 中,并且您希望每次按下按钮时都能够添加新数据。所以你需要做的是在actionPerformed
之外初始化模型和表格,并且每次按下按钮时,获取所有7条数据,将它们放入一个数组中,然后将该数组添加到模型中,这将自动在表格中添加另一行
来自 Netbeans GUI 生成器
- 突出显示表格图形
- 转到“属性面板”(您应该会在屏幕的最右侧看到它)
- 转到显示模型广告的属性,然后单击...按钮
- 将行设置为 0
在你的actionPerformed
中只需做这样的事情
public void actionPerformed(ActionEvent e){
// get 7 fields of data
Object[] row = {data1, data2, data3, data4, data5, data6, data7};
((DefaultTableModel)jTable1.getModel()).addRow(row);
}
关于Java 表系统不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20885596/