JScrollBar布局管理器和SpringLayout布局管理器不能一起使用。

huangapple go评论74阅读模式
英文:

JScrollBar Layout Manager and SpringLayout Manager not working together

问题

public void createSpringLayout(SpringLayout spring, JLabel label, JScrollPane scrollPane, JPanel buttonPanel) {
    spring.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, this);
    spring.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, this);

    spring.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, label);
    spring.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, label);
    spring.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, this);

    spring.putConstraint(SpringLayout.NORTH, buttonPanel, Spring.constant(10, 30, 30), SpringLayout.SOUTH, scrollPane);

    spring.putConstraint(SpringLayout.SOUTH, buttonPanel, -10, SpringLayout.SOUTH, this);
    spring.putConstraint(SpringLayout.WEST, buttonPanel, 0, SpringLayout.WEST, label);
}

JScrollBar布局管理器和SpringLayout布局管理器不能一起使用。

The two Jpanels with all the stuff on them are using the same superclass for better looks.
However as you can see if the contents of the scrollpane are too wide, the scrollpane just uses extra space at the bottom to create a horizontal scrollbar.
Even if I specify that the Spring between the scrollpane and the buttonPanel can be between 10 and 30.

I believe that first, the SpringLayoutManager is called to lay out its components, and then the ScrollPane comes along and notices that its displayed components do not fit in the viewport and creates a scrollbar, which the SpringLayoutManager is unaware of.

I can't find any solution that tells the ScrollPane beforehand to calculate its needed size or not to use more space than it has from the beginning.

英文:
public void createSpringLayout(SpringLayout spring, JLabel label, JScrollPane scrollPane, JPanel buttonPanel) {
    spring.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, this);
    spring.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, this);

	spring.putConstraint(SpringLayout.NORTH, scrollPane, 10, SpringLayout.SOUTH, label);
	spring.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, label);
	spring.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, this);

	spring.putConstraint(SpringLayout.NORTH, buttonPanel, Spring.constant(10, 30, 30), SpringLayout.SOUTH, scrollPane);

	spring.putConstraint(SpringLayout.SOUTH, buttonPanel, -10, SpringLayout.SOUTH, this);
	spring.putConstraint(SpringLayout.WEST, buttonPanel, 0, SpringLayout.WEST, label);
}

JScrollBar布局管理器和SpringLayout布局管理器不能一起使用。

The two Jpanels with all the stuff on them are using the same superclass for better looks.
However as you can see if the contents of the scrollpane are to wide the scrollpane just uses extra space in the bottom to create a horizontal scrollbar.
Even if I tell the Springlayout that the spring between the scrollpane and the buttonPanel can be between 10 and 30.

I think that first the SpringLayoutManager is called to layout its components and then the ScrollPane comes along and notices that its displayed Components do not fit in the Viewport and creates a Scrollbar, which the SpringLayoutManager is unaware of.

I can't find any solution tha tell the ScrollPane beforehand to calculate its needed Size or for it to just dont use more Space than it has from the beginning.

答案1

得分: 2

不要将负数传递给 putConstraint

来自 putConstraint 的文档

将组件 c1 的边缘 e1 与组件 c2 的边缘 e2 连接,并固定边缘之间的距离。

pad 值不是绝对像素偏移,它是组件和连接边缘之间的填充量(距离)。它应始终为正值。

同样的 javadoc 还描述了 pad 参数为:

pad - 依赖项和锚点之间的固定距离

SpringLayout 很复杂,这意味着很容易出错。您可以轻松地使用 BorderLayout 实现相同的布局:

setLayout(new BorderLayout());
add(label, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);

label.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
英文:

Do not pass negative numbers to putConstraint.

From the documentation of putConstraint:

>Links edge e1 of component c1 to edge e2 of component c2, with a fixed distance between the edges.

The pad value is not an absolute pixel offset, it is the amount of padding (the distance) between the component and the linked edge. It should always be positive.

The same javadoc also describes the pad parameter as:

>pad - the fixed distance between dependent and anchor

SpringLayout is complex, which means it’s easy to make mistakes with it. You can easily accomplish the same layout with a BorderLayout:

setLayout(new BorderLayout());
add(label, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);

label.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

huangapple
  • 本文由 发表于 2023年2月8日 22:12:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定