我有一个GUI操作图像的实验。我需要有一个GUI所示的图像,与周围其他几个部件。如果调整GUI窗口,只有图像区域需要用它来调整。
我想用一个GridBagLayout
是要走的路,但是当我做的ScrollPane
保持图像的缩放行为是意外的:当在某种程度上小型化的GUI窗口的可用空间是在ScrollPane
图像太小中,ScrollPane
部件的尺寸减小立即于一维它是可用的空间小得多。
图像代码例如:qm.png
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
的行为更像是未来一段代码的一个:
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
两个GUI实现的行为不同。
任何人都可以告诉我吗?
在您添加到滚动组件,覆盖
public Dimension getMinimumSize()
要么
public Dimension getPreferredSize()
不同的布局管理器使用这些值。
Andrew Thompson的建议让我半途而废。我需要bothc.fill = Fill.Both
和c.weightx = 1.0
添加到滚动窗格中的约束。
该填充约束是解决方案的一部分,这一事实是直觉上我,因为tutorial on GridBagLayout
介绍«当组件的显示区域大于组件的所需大小,以确定是否以及如何调整组件,用于»填充约束,而在我的情况下滚动窗格组件的请求的大小实际上比现有的显示面积。