繁体   English   中英

为什么表格数据不显示?

[英]Why does not table data show?

谁能告诉我为什么我的表格没有显示,但数据,列和行却出现了框架。 我删除了任何不必要的字段和方法。


public class DDPSPLandscaper extends JFrame {

private static final long serialVersionUID = -49372444918464235883L;
private JPanel contentPane;
private JTable table = new JTable(new TableData());

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                DDPSPLandscaper frame = new DDPSPLandscaper();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public DDPSPLandscaper() {
    super("DDPSPLandscaping Tool");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 780, 494);

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JTextArea textArea_4 = new JTextArea();
    menuBar.add(textArea_4);
    textArea_4.setEditable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0, 0, 145, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0};
    gbl_contentPane.rowHeights = new int[]{33, 0, 0, 0, 0, 0, 0, 0, 0, -7, 0, 0, 0};
    gbl_contentPane.columnWeights = new double[]{1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    contentPane.setLayout(gbl_contentPane);

    ...

    table = new JTable();
    GridBagConstraints gbc_table = new GridBagConstraints();
    gbc_table.gridwidth = 5;
    gbc_table.gridheight = 3;
    gbc_table.insets = new Insets(0, 0, 5, 5);
    gbc_table.fill = GridBagConstraints.BOTH;
    gbc_table.gridx = 3;
    gbc_table.gridy = 5;
    contentPane.add(table, gbc_table);
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    contentPane.add(scrollPane, gbc_table);

}

class TableData extends AbstractTableModel {

    private static final long serialVersionUID = 1L;

    private String[] columnNames = {"Item", "Name", "whight/size", "selct"};

    private Object[][] data = {
        { "Campione", "Snowboarding", new Integer(5),
            new Boolean(false) },
        { "Huml", "Rowing", new Integer(3), new Boolean(true) },
        { "Kathy", "Walrath", "Knitting", new Integer(2),
            new Boolean(false) },
        { "Zakhour", "Speed reading", new Integer(20),
            new Boolean(true) },
        { "Milne", "Pool", new Integer(10),
            new Boolean(false) } };

    public int getColumnCount() {
      return columnNames.length;
    }

    public int getRowCount() {
      return data.length;
    }

    public String getColumnName(int col) {
      return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
      return data[row][col];
    }

}

}

尝试删除线

contentPane.add(table, gbc_table);

该表是JScrollPane的视图组件; 它可能也不应添加到JFrame的内容窗格中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM