gpt4 book ai didi

java - 为什么 AUTO_RESIZE_LAST_COLUMN 的作用与 AUTO_RESIZE_ALL_COLUMNS 类似?

转载 作者:行者123 更新时间:2023-12-02 23:45:56 25 4
gpt4 key购买 nike

当我使用 AUTO_RESIZE_OFFAUTO_RESIZE_ALL_COLUMNS 调用 setAutoResizeMode 时,我会得到描述的预期结果 here当我调整窗口大小时。

当我使用 AUTO_RESIZE_LAST_COLUMN 调用它时,我得到的行为与我指定 AUTO_RESIZE_ALL_COLUMNS 完全相同。

为什么?

这是 MCV 示例:

import javax.swing.*;
import java.awt.*;
public class ScrollableJTable extends JPanel {
public ScrollableJTable() {
setLayout(new BorderLayout());
JTable table = new JTable(10, 10);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); // vary the constant
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel panel = new ScrollableJTable();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}

$ javac -version
javac 1.8.0_221

$ java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

# compile && go
$ javac ScrollableJTable.java && java ScrollableJTable

最佳答案

似乎JTable自动调整大小模式仅适用于单个列更改其自身大小而不是整个表的情况。根据JTable.doLayout() javadoc:

If the resizingColumn is not null, it is one of the columns in the table that has changed size rather than the table itself. In this case the auto-resize modes govern the way the extra (or deficit) space is distributed amongst the available columns.

您可以通过使用 JTableHeader.setResizingColumn() 手动设置列来查看此行为使用以下代码,它将自动调整最后一列的大小:

JTableHeader header = table.getTableHeader();
TableColumn lastColumn = header.getColumnModel().getColumn(9);
header.setResizingColumn(lastColumn);

不确定这是否是错误或预期行为。可能是bug JDK-4098154setResizingColumn javadoc 中揭示了这是一个错误的线索。 :

Application code will not use this method explicitly, it is used internally by the column sizing mechanism.

他们本可以轻松(且准确)地写出这样的内容:

If the only way you can make your code work is to use this method then our code has a bug.

关于java - 为什么 AUTO_RESIZE_LAST_COLUMN 的作用与 AUTO_RESIZE_ALL_COLUMNS 类似?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58771091/

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