【发布时间】:2016-09-30 02:28:57
【问题描述】:
我正在使用添加元素的 JscrollPane,当我有超过 7 个元素时,我的 JScrollPane 中的 JScrollBar 被激活。我需要当我引入一个新元素时,滚动条向右移动
public void addElement(String nombre, Wheel wheel)
{
if(actual != null)
{
actual.bordeOn(false);
}
if(this.actual != null && this.index != 0 && this.index != this.actual.getIndex()+1)
{//Se ha producido navegacion en medio de la pila
for(int i = this.stack.size()-1 ; i > this.actual.getIndex(); i--)
{//Borramos todos los elementos del actual en adelante.
BreadCrump b = this.stack.get(i);
this.panelBCD.remove(b);
this.stack.remove(i);
}
this.index = this.actual.getIndex()+1;
}
BreadCrump bc = new BreadCrump(nombre, this);
bc.setIndex(this.index);
this.index++;
this.stack.add(bc);
panelBCD.add(bc);
actual = bc;
bc.setWheel(wheel);
bc.bordeOn(true);
numberElements++;
JScrollBar scroll = sp.getHorizontalScrollBar();
scroll.setValue(scroll.getMaximum());
this.revalidate();
this.repaint();
}
构造器:
public Displayer(JPanel father)
panelBCD = new JPanel();
this.setBackground(new java.awt.Color(200, 200, 200));
this.setPreferredSize(new java.awt.Dimension(father.getSize().width, height));
BoxLayout aaa = new BoxLayout(this,1);
this.setLayout(aaa);
FlowLayout mg = new FlowLayout(0,80,0); //Este es la separacion entre elementos
//a.setLayout(null);
panelBCD.setLayout(mg);
mg.setAlignment(FlowLayout.LEFT);
sp = new JScrollPane(panelBCD);
sp.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
sp.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//sp.setBounds(0, 0,father.getSize().width, height);
this.add(sp);
father.add(this, java.awt.BorderLayout.PAGE_END);
addMouseListener(this);
addMouseMotionListener(this);
sp.setViewportBorder(new LineBorder(Color.BLACK));
滚动条向右移动但永远不会达到最大值,总是有更多的距离可以滚动。
有人知道为什么会这样吗?我看到使用 setValue 和 getMaximum 将滚动条向右移动,但对我来说它保持在右侧但不正确。
感谢您的宝贵时间。
这是一个屏幕截图。
编辑:包含该代码的类是 JPanel
EDIT2:问题是当我向右移动时,滚动没有更新,所以我设置值时的最大值,不是最终的最大值。
此 JPanel 位于另一个 JPanel 内,该 JPanel 具有与其他组件的布局。
所以:在这个 JPanel 中,我有一个 JScrollBarPane 和一个 JPanel,其中包含我在运行时添加的组件。这个 JPanel 在另一个 JPanel 中。当我更新滚动条位置时,最大位置不等于本次迭代的最终最大位置。
JScrollPane 不会影响窗口的大小
我希望这是足够的信息。
【问题讨论】:
标签: java swing jscrollpane jscrollbar