为什么表数据不显示?

时间:2017-05-17 10:58:18

标签: java jframe jtable

任何人都可以告诉我为什么我的表没有显示,框架会出现数据,列和行。 我删除了任何不必要的字段和方法。

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];
    }

}

}

1 个答案:

答案 0 :(得分:0)

尝试删除该行

contentPane.add(table, gbc_table);

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