gpt4 book ai didi

Java Swing : JScrollPane dosen't expand to fit JPanel in it

转载 作者:行者123 更新时间:2023-12-02 10:38:19 25 4
gpt4 key购买 nike

我有一个JTabel我想将其添加到 JScrollPane 。但是JScrollPane尺寸非常小。我尝试将表设置为 JPanel (已调整大小)然后输入JPanelJScrollPane但它不起作用。

这是我的代码:

 static public class PnGiaoDich extends JPanel{
public PnGiaoDich(){
//size & background
setPreferredSize( new Dimension(1050, 435));
setBackground(new Color(255,255,255));

//layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//create table
String[] columnNames = {"Ngày giao dịch",
"Loại gio dịch | Mô tả",
"Tình trạng",
"Số tiền"
};

Object[][] data = {
{"","","",""},
{"","","",""},
{"","","",""},
{"","","",""}
};

JTable table = new JTable(data,columnNames);
TableColumn column = null;
table.getColumnModel().getColumn(0).setPreferredWidth(150);
table.getColumnModel().getColumn(1).setPreferredWidth(525);
table.getColumnModel().getColumn(2).setPreferredWidth(150);
table.getColumnModel().getColumn(3).setPreferredWidth(150);

// create table to put into JScrollPane
JPanel pnTable = new JPanel();
pnTable.setBorder(BorderFactory.createLineBorder(Color.red));
pnTable.setPreferredSize(new Dimension(800, 600));
pnTable.add(table);
//Add panel to JScrollPane
JScrollPane scrollpn = new JScrollPane(pnTable);
//table.setFillsViewportHeight(true);

c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
c.insets = new Insets(20,20,20,20);
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.NORTH;
add(scrollpn,c);

//
}
}

但是如果我将这些代码发布到主 JFrame,它就可以正常工作。

这是我的完整代码:enter link description here

我的 JScrollPane 的图像:

image

最佳答案

好吧,让我们来看看......

setPreferredSize( new Dimension(1050, 435));

...尽量避免这样做。如果您“必须”这样做,请重写 getPreferredSize,因为 setPreferredSize 是您的“稳定”UI 的一个漏洞

setBackground(new Color(255,255,255));

//layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//create table
String[] columnNames = {"Ngày giao dịch",
"Loại gio dịch | Mô tả",
"Tình trạng",
"Số tiền"
};

Object[][] data = {
{"","","",""},
{"","","",""},
{"","","",""},
{"","","",""}
};

JTable table = new JTable(data,columnNames);
TableColumn column = null;
table.getColumnModel().getColumn(0).setPreferredWidth(150);
table.getColumnModel().getColumn(1).setPreferredWidth(525);
table.getColumnModel().getColumn(2).setPreferredWidth(150);
table.getColumnModel().getColumn(3).setPreferredWidth(150);

好吧,到目前为止还不错......

// create table to put into JScrollPane
JPanel pnTable = new JPanel();
pnTable.setBorder(BorderFactory.createLineBorder(Color.red));
pnTable.setPreferredSize(new Dimension(800, 600));
pnTable.add(table);
//Add panel to JScrollPane
JScrollPane scrollpn = new JScrollPane(pnTable);
//table.setFillsViewportHeight(true);

然后...什么?为什么将 JTable 添加到使用 FlowLayoutJPanel,然后再添加 JScrollPane?为什么不直接将 JTable 添加到 JScrollPane 本身呢?

此外,这样做时,您还需要利用 JTableJScrollPane 相互通信并确定何时进行滚动的能力。

c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
c.insets = new Insets(20,20,20,20);
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.NORTH;
add(scrollpn,c);

c.gridwidth = 2;?虽然它可能不会产生效果,但它引发了更多关于为什么的问题?

解决方案...

好吧,在考虑了约束并删除了额外的 JPanel 之后,问题归结为

setPreferredSize( new Dimension(1050, 435));

并且可能没有帮助 pnTable.setPreferredSize(new Dimension(800, 600));

我基本上将代码剥离回以下内容,“基本上”有效......

static public class PnGiaoDich extends JPanel {

public PnGiaoDich() {
setBackground(Color.WHITE);

//layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//create table
String[] columnNames = {"Ngày giao dịch",
"Loại gio dịch | Mô tả",
"Tình trạng",
"Số tiền"
};

Object[][] data = {
{"", "", "", ""},
{"", "", "", ""},
{"", "", "", ""},
{"", "", "", ""}
};

JTable table = new JTable(data, columnNames);
table.getColumnModel().getColumn(0).setPreferredWidth(150);
table.getColumnModel().getColumn(1).setPreferredWidth(525);
table.getColumnModel().getColumn(2).setPreferredWidth(150);
table.getColumnModel().getColumn(3).setPreferredWidth(150);

//Add panel to JScrollPane
table.setBorder(BorderFactory.createLineBorder(Color.RED));
JScrollPane scrollpn = new JScrollPane(table);
//table.setFillsViewportHeight(true);

c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(20, 20, 20, 20);
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.NORTH;
add(scrollpn, c);
}
}

关于Java Swing : JScrollPane dosen't expand to fit JPanel in it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53112971/

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