在GridBagLayout中控制ScrollPane的缩小行为

时间:2019-02-08 15:27:52

标签: scala swing layout-manager jscrollpane gridbaglayout

我正在尝试在GUI中处理图像。我需要在GUI中显示一个图像,周围还有其他几个组件。如果调整了GUI窗口的大小,则只有图像区域需要随之调整大小。

我认为使用ScrollPane是可行的方法,但是当我这样做时,保存图像的ScrollPane的调整大小行为是出乎意料的:当GUI窗口的尺寸减小到对于import scala.swing.{ Button, GridBagPanel, Label, MainFrame, ScrollPane, SimpleSwingApplication } import javax.swing.ImageIcon object GridBag_vs_ScrollPane extends SimpleSwingApplication { def top = new MainFrame { title = "GridBag vs ScrollPane" contents = gui } val gui = new GridBagPanel { val fp_img = """.\resources\qm.png""" val scrPane = new ScrollPane( new Label { icon = new ImageIcon(fp_img) } ) val button1 = new Button("Button 1") val c = new Constraints c.gridx = 0 c.gridy = 0 layout(button1) = c c.weighty = 1.0 c.gridx = 0 c.gridy = 1 layout(scrPane) = c } } 中的图像而言,可用空间太小,ScrollPane组件会立即缩小到一个小于可用空间的尺寸。

代码示例图片:qm.png

import javax.swing.ImageIcon

import scala.swing.{Label, MainFrame, ScrollPane, SimpleSwingApplication}

object SimpleScrollPane extends SimpleSwingApplication {
    def top = new MainFrame {
        title = "Simple ScrollPane"
        val fp_img = """.\resources\qm.png"""
        contents = new ScrollPane( new Label { icon = new ImageIcon(fp_img) } )
    }
}

我一直在寻找的ScrollPane的行为更像下一段代码中的行为:

if parameter = 'xyz'
then false
else true

我既不知道如何获得所需的行为,也不知道CXXFLAGS = defaults ifneq (,$(findstring release,$(MAKECMDGOALS))) CXXFLAGS += release else ifneq (,$(findstring debug,$(MAKECMDGOALS))) CXXFLAGS += debug endif all: @echo CXXFLAGS = $(CXXFLAGS) # Rules for release / debug. The `; @:` part means the body of the rule is empty (do nothing). It just "calls" the dependency rule `all`. release: all ; @: debug: all ; @: 为什么在两种GUI实现中表现不同。

有人可以启发我吗?

2 个答案:

答案 0 :(得分:1)

在添加到滚动窗格的组件中,覆盖

public Dimension getMinimumSize() 

public Dimension getPreferredSize()

不同的布局管理器使用这些值。

答案 1 :(得分:1)

Andrew Thompson的建议使我半途而废。我需要将c.fill = Fill.Bothc.weightx = 1.0都添加到滚动窗格的约束中。

填充约束是解决方案的一部分这一事实对我来说很直观,因为tutorial on GridBagLayout将填充约束描述为«当组件的显示区域大于组件的请求大小时使用,以确定是否以及如何使用来调整组件的大小。»,在我的情况下,滚动窗格组件的请求大小实际上大于可用的显示区域。