gpt4 book ai didi

java - JTable 根据 ScrollPane 大小调整大小

转载 作者:行者123 更新时间:2023-12-01 13:05:06 25 4
gpt4 key购买 nike

我试图让 JTable 在调整包含 JTable 的窗口大小时自动调整其大小。

如下例: http://docs.oracle.com/javase/tutorial/uiswing/examples/components/SimpleTableDemoProject/src/components/SimpleTableDemo.java

JTable会调整其宽度,如果高度太小而无法全部显示,它的右侧还会有一个滚动条。

这个是JFrame,我使用单独的类扩展JPanel:

public class GUIFrame 
{

public static void main(String[] args)
{

//Setting up JFrame's basic properties
JFrame mainFrame = new JFrame("Muney Manager");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Setting up panels:
JPanel mPanel = new JPanel();
mPanel.setLayout(new BoxLayout(mPanel,BoxLayout.PAGE_AXIS));

TablePanel tPanel = new TablePanel();
ToolBar buttonPanel = new ToolBar(tPanel);

mPanel.add(buttonPanel);
mPanel.add(tPanel);

//Setting up Panels and Frame to be displayed :3
mainFrame.getContentPane().add(mPanel);
mainFrame.pack();
mainFrame.setVisible(true);

}

这是包含 JTable 的:

public class TablePanel extends JPanel
{

//DefaultTableModel is needed for adding new rows
private JTable table;
private DefaultTableModel tmodel;

private JScrollPane scrollPane;

//Table's column name
private String[] columnNames = {"Date",
"Category",
"Details",
"Add/Subtract",
"Total"};


Object[][] data = {
{20140925, "Grocery", "Supermarket", -5.23,600.00},
{20141013,"Car Maintenance", "Changing Tires", -200.00, 400.00}
};




public TablePanel()
{

//Initializing tmodel and putting it into table
//It's needed for "adding rows" method
tmodel = new DefaultTableModel(data, columnNames);
table = new JTable();

table.setModel(tmodel);


//Setting up JScrollPane, table Headder
//and make scrollPane visible
table.setFillsViewportHeight(true);
add(new JScrollPane(table));

}

由于某种原因,我无法让它工作,我的代码有什么问题?JTable 宽度没有调整大小,而且我似乎无法在右侧获得滚动条

enter image description here

最佳答案

考虑使用 BorderLayout 代替...

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame mainFrame = new JFrame("Muney Manager");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Setting up panels:
JPanel mPanel = new JPanel();
mPanel.setLayout(new BorderLayout());

TablePanel tPanel = new TablePanel();
ToolBar buttonPanel = new ToolBar(tPanel);

mPanel.add(buttonPanel, BorderLayout.NORTH);
mPanel.add(tPanel);

//Setting up Panels and Frame to be displayed :3
mainFrame.getContentPane().add(mPanel);
mainFrame.pack();
mainFrame.setVisible(true);
}
});
}

或者,如果您想要更多控制,甚至可能是 GridBagLayout

已更新

JPanel 默认情况下使用 FlowLayout,尝试更改 TablePanel 以使用 BorderLayout 。 .

public TablePanel()
{
setLayout(new BorderLayout());

//Initializing tmodel and putting it into table
//It's needed for "adding rows" method
tmodel = new DefaultTableModel(data, columnNames);
table = new JTable();

table.setModel(tmodel);


//Setting up JScrollPane, table Headder
//and make scrollPane visible
table.setFillsViewportHeight(true);
add(new JScrollPane(table));

}

关于java - JTable 根据 ScrollPane 大小调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23329496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com