首页 文章

使用JTable将JButton添加到框架中

提问于
浏览
1

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

class SimpleTableExample扩展JFrame {//此示例中使用的实例属性私有JPanel topPanel;私有JTable表; private JScrollPane scrollPane;私人JButton update_Button;

// 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 );

}

}

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

提前致谢

1 回答

  • 1

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

    topPanel.add( button, BorderLayout.SOUTH );
    

    你是这个意思吗?

相关问题