1

我想知道如何在包含 JTable 的框架内放置一个按钮。(按钮不应该在单元格内,而是在表格结束之后)这是我到目前为止编写的示例代码:

类 SimpleTableExample 扩展 JFrame

{

// 本例中使用的实例属性

私人JPanel topPanel;

私有 JTable 表;

私有 JScrollPane 滚动窗格;私有 JButton 更新按钮;

// Constructor of main frame
public SimpleTableExample()
{
    // Set the frame characteristics
    setTitle("Add new item" );
    setSize(300, 200);
    setBackground( Color.gray );

    // Create a panel to hold all other components
    topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );



    // Create columns names
    String columnNames[] = {"Item Description", "Item Type", "Item Price"};

    // Create some data
    String dataValues[][] = {{ "0", "Entree", "0" }};

    // Create a new table instance
    table = new JTable( dataValues, columnNames );

    ////////////////////////////

    JComboBox item_Type_Combobox = new JComboBox();
    item_Type_Combobox = new JComboBox(item_Type.values());
    TableColumn column = table.getColumnModel().getColumn(1);
    column.setCellEditor(new DefaultCellEditor(item_Type_Combobox));


    ////////////////////////////    



    // Add the table to a scrolling pane
    scrollPane = new JScrollPane( table );
    topPanel.add( scrollPane, BorderLayout.CENTER );

}

}

如何在创建的表格后添加按钮?

提前致谢

4

1 回答 1

1

如果您想在(例如)表格下方添加另一个组件,您可以使用:

topPanel.add( button, BorderLayout.SOUTH );

你是这个意思吗?

于 2010-04-26T10:34:15.377 回答