在Swing GridLayout中动态添加新行 - 如何启用滚动

时间:2014-05-12 07:44:34

标签: java swing user-interface

我已经动态地将行添加到网格视图中。当行数大于屏幕上显示的行数时,其余内容将被隐藏。如何启用滚动查看未显示的行的其余部分?

    JPanel billItemsPanel = new JPanel();
    GridLayout billItemsLayout = new GridLayout(0,6);
    billItemsLayout.setVgap(20);
    billItemsPanel.setLayout(billItemsLayout);
    billItemsPanel.add(new Label("Mobile Number"));
    billItemsPanel.add(new TextField(20));
    billItemsPanel.add(new Label(""));
    billItemsPanel.add(new Label(""));
    billItemsPanel.add(new Label(""));
    billItemsPanel.add(new Label(""));  

for(int i=0; i<10; i++){
    billItemsPanel.add(new TextField("Hi SKU"));
    billItemsPanel.add(new Label("Hi Title"));
    billItemsPanel.add(new Label("Hi Type"));
    billItemsPanel.add(new TextField("Hi Qty"));
    billItemsPanel.add(new Label("Hi Price"));
    billItemsPanel.add(new Label("Hi Amount"));

    billItemsPanel.add(new TextField("Bye SKU"));
    billItemsPanel.add(new Label("Bye Title"));
    billItemsPanel.add(new Label("Bye Type"));
    billItemsPanel.add(new TextField("Bye Qty"));
    billItemsPanel.add(new Label("Bye Price"));
    billItemsPanel.add(new Label("Bye Amount"));
}

1 个答案:

答案 0 :(得分:0)

将其包装在新的JScrollPane()