2

我有一个使用 MigLayout 作为其 LayoutManager 的组件。它设置为在 JScrollPane 内增长以占据整个可用宽度。因此,如果我调整应用程序框架的大小,JScrollPane 与框架一样宽,内部组件也是如此。

但是,当我缩小框架时,JScrollPane 内的增长组件只会保持其宽度而不缩小,因此会显示水平滚动条。

我知道JScrollpane 需要缩小它的宽度并使用 getScrollableTracksViewportWidth=true 实现 Scrollable-Interface 但 MigLayout 似乎并不尊重它。

有什么建议吗?谢谢

4

1 回答 1

3

我通过以下 scala-snippet 解决了它。Scala 的 Scrollable 只是一个假包装器,你应该实现 Java-Scrollable ......

import javax.swing.JPanel
import javax.swing.{Scrollable => JScrollable}
import javax.swing.BoxLayout
import java.awt.Dimension
import java.awt.Rectangle
import scala.swing.SequentialContainer
import scala.swing.Orientation
import scala.swing.Scrollable
import scala.swing.Panel

class ScrollablePanel extends Panel with SequentialContainer.Wrapper with Scrollable.Wrapper {

  var scrollIncrement: Int = 10
  var blockScrollIncrement: Int = 50

  val allowVerticalScrolling: Boolean   = true
  val allowHorizontalScrolling: Boolean = false

  override lazy val peer = new JPanel with SuperMixin with JScrollable {
    def getPreferredScrollableViewportSize: Dimension =
      getPreferredSize

    def getScrollableTracksViewportHeight: Boolean =
      !allowVerticalScrolling

    def getScrollableTracksViewportWidth: Boolean =
      !allowHorizontalScrolling

    def getScrollableBlockIncrement(visibleRect: Rectangle, orientation: Int, direction: Int): Int =
      scrollIncrement

    def getScrollableUnitIncrement(visibleRect: Rectangle, orientation: Int, direction: Int): Int =
      blockScrollIncrement
  }

  final protected def scrollablePeer: JScrollable = peer
}
于 2012-01-30T22:00:12.497 回答