英文:
Unreliable Container width when using JScrollPane
问题
我制作了一个自定义的LayoutManager,用于在给定的空间中以均匀的正方形渲染组件,它通过使用以下方法实现:
@Override
public void layoutContainer(Container parent) {
if(parent.getWidth() == 0) {
return;
}
int columns = parent.getWidth() / gridSizeWidth;
int x = 0, y = 0;
for(int i = 0; i < parent.getComponentCount(); i++) {
Component comp = parent.getComponent(i);
comp.setBounds(x * gridSizeWidth, y * gridSizeHeight, gridSizeWidth, gridSizeHeight);
if(++x == columns) {
x = 0;
y++;
}
}
System.out.println(parent.getClass().getSimpleName() + ": " + parent.getWidth());
}
当将此代码直接应用于JFrame时,它按预期工作,但如果我尝试将其插入JScrollPane中,它会很快崩溃。
当它只添加到JFrame中并且显示正确时,控制台输出为:
TileScroller: 422
然而,当添加到JScrollPane中时...
TileScroller: 419
TileScroller: 26816
TileScroller: 1716224
TileScroller: 109838336
TileScroller: 419
TileScroller: 26816
TileScroller: 1716224
TileScroller: 109838336
TileScroller: 419
...
它会很快重复这个过程,并导致布局迅速更改而产生闪烁效果。据我所见,它确实将每个组件放置在正确的位置,但问题在于大小在波动。
这是我创建JScrollPane的代码。
JPanel modeIndividualTile = new JPanel();
JScrollPane tileScroller = new JScrollPane(tileSelector = new TileScroller(creator),
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tileScroller.getVerticalScrollBar().setUnitIncrement(8);
modeIndividualTile.add(tileScroller);
// 自定义布局管理器,根据以下指定的值相对地呈现所有内容。 (20,65) 会在宽度的 x=20% 处,高度的 y=65% 处呈现。
rl.addComponent(frame, tileScroller, 20, 65, 80, 98);
这可能的原因是什么,我该如何防止这种情况发生?
英文:
I made a custom LayoutManager to render components in uniform squares in the space provided, and it does that by using the following method:
@Override
public void layoutContainer(Container parent) {
if(parent.getWidth() == 0) {
return;
}
int columns = parent.getWidth() / gridSizeWidth;
int x = 0, y = 0;
for(int i = 0; i < parent.getComponentCount(); i++) {
Component comp = parent.getComponent(i);
comp.setBounds(x * gridSizeWidth, y * gridSizeHeight, gridSizeWidth, gridSizeHeight);
if(++x == columns) {
x = 0;
y++;
}
}
System.out.println(parent.getClass().getSimpleName() + ": " + parent.getWidth());
}
This code works as intended when applied directly to the JFrame, but if I try to insert it inside of a JScrollPane, then it very quickly falls apart.
When it is only added to the JFrame and looks correct, the console reads:
TileScroller: 422
However, when adding it into a JScrollPane...
TileScroller: 419
TileScroller: 26816
TileScroller: 1716224
TileScroller: 109838336
TileScroller: 419
TileScroller: 26816
TileScroller: 1716224
TileScroller: 109838336
TileScroller: 419
...
It repeats this very quickly and causes a flicker effect from changing the layout so rapidly. As far as I can see, it is correctly placing each component at the correct spot for each size, but the issue is that the size is fluctuating.
Here is my code for creating the JScrollPane.
JPanel modeIndividualTile = new JPanel();
JScrollPane tileScroller = new JScrollPane(tileSelector = new TileScroller(creator),
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tileScroller.getVerticalScrollBar().setUnitIncrement(8);
modeIndividualTile.add(tileScroller);
// Custom layout manager that renders everything relatively to the designated values below. (20,65) will render at x=20% of width, y=65% of height.
rl.addComponent(frame, tileScroller, 20, 65, 80, 98);
What could be the cause of this, and how could I prevent it?
答案1
得分: 0
问题是因为preferredLayoutSize和minimumLayoutSize的实现不正确。正确使用这些方法后问题得以解决。
英文:
The problem was because of an inpromper implementation of preferredLayoutSize and minimumLayoutSize. Properly using these methods fixed it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论