【问题标题】:Resize a Jtable to fill the JFrame and still scroll调整 Jtable 的大小以填充 JFrame 并仍然滚动
【发布时间】:2012-01-04 15:35:41
【问题描述】:

我正在尝试调整位于选项卡视图中的滚动面板中的两个 JTable 的大小。我已经设法将框架的大小设置为 12000、5000,但是 Jtable/scrollPanel 没有打球。

我找到了一些代码来设置表格的列大小,但它没有填充 Jframe,它只是在表格/scrollPanel 的底部添加了一个滚动条

任何人都可以想出一种方法来让表格填满框架并且仍然可以滚动吗?

表格文件是:

public class ViewInTablePET extends JPanel {
private String[][] rowData = new String[0][];
private String columnNames[] = {"Shop", "Type", "price", "dateAcquired", "notes"};
private DefaultTableModel tableMod = new DefaultTableModel(rowData, columnNames);
private JTable table = new JTable(tableMod);

public ViewInTablePET() {

    String bob;
    String[] dan;

    for (Integer i = 0; i < AppModel.getArrayListPet().size(); i++) { 
        bob = AppModel.getArrayListPet().get(i).toString();
        dan = AppModel.getStringAsArray(bob, "\t");
        tableMod.insertRow(i, new Object[]{dan[0], dan[1], dan[2], dan[3], dan[4]});
    }
    JScrollPane scrollPane = new JScrollPane(table);
    this.add(scrollPane, BorderLayout.CENTER);    
}
}

Fram 文件是:

public class ViewInTabbs extends JFrame {

JLabel Average = new JLabel("Click a table row to get the average price");

public ViewInTabbs() {

    JTabbedPane tabs =              new JTabbedPane();
    ViewInTablePetShope petShop =   new ViewInTablePetShope();
    ViewInTablePET pet =            new ViewInTablePET();

    tabs.add("Pet Shope", (JPanel) petShop);
    tabs.add("Pets in the shop", (JPanel) pet);
    this.setTitle("Tabb View of Data");
    this.add(tabs, BorderLayout.CENTER);
    this.setSize(12000, 5000);
    this.setVisible(true);
    this.add(Average, BorderLayout.SOUTH);
}
public void setAverage(String ave) {
    this.Average.setText(ave);
}
}

【问题讨论】:

    标签: java swing resize jtable


    【解决方案1】:

    啊……二读:罪魁祸首是 ViewInTablePET 的 LayoutManager,它默认为 FlowLayout。更改为边框布局

     public ViewInTablePET() {
        super(new BorderLayout());
        // your code
        ...
     }
    

    【讨论】:

      【解决方案2】:

      在您的 ViewInTablePET 类中,您添加了带有约束 BorderLayout.CENTERJScrollPane,但我没有在该类中看到 setLayout( new BorderLayout() ) 调用。由于默认情况下JPanel 具有FlowLayout,因此不会产生预期的效果。但是,当您首先将布局设置为 BorderLayout 时,它应该可以工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        • 2013-01-19
        • 2011-03-21
        • 2011-01-03
        • 2012-04-12
        • 2016-09-29
        相关资源
        最近更新 更多