调整 Jtable 的大小以填充 JFrame 并且仍然滚动

发布于 2024-12-25 00:10:33 字数 1715 浏览 1 评论 0原文

我正在尝试调整选项卡视图中的scrollPanel 中的两个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);
}
}

I am trying to resize two JTable that are in scrollPanels that are in a Tab view. I have managed to set the size of the frame to 12000, 5000 but the Jtable/scrollPanel are not playing ball.

I found some code to set the size of the columns of the table that worked BUT instead of filling the Jframe it just added a scroll bar at the bottom of the table/scrollPanel

can anyone come up with a way of getting the table to fill the frame and still have scrolling?

The Table File is:

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

The Fram file is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪眸﹌ 2025-01-01 00:10:33

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

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

ahh .. on second reading: the culprit is the LayoutManager of ViewInTablePET which defaults to FlowLayout. Change to BorderLayout

 public ViewInTablePET() {
    super(new BorderLayout());
    // your code
    ...
 }
金兰素衣 2025-01-01 00:10:33

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

In your ViewInTablePET class you add the JScrollPane with the constrains BorderLayout.CENTER, but I do not see the setLayout( new BorderLayout() ) call in that class. Since a JPanel by default has a FlowLayout this will not have the desired effect. It should however work when you set the layout to BorderLayout first

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文