带有 GridBagLayout 索引的 Clojure 序列
我正在编写代码来用控件“行”填充 java.awt.GridBagLayout 。对于每一行,我都有一个以下形式的方法调用
(.add panel CONTROL (fill-gbc 0 INDEX ...))
,其中 CONTROL 是要放置在该行的 Swing 控件(即:(JLabel."Hello")
),INDEX 是 gridy< /code> 该控件(
fill-gbc
填充单个可变的 GridBagContraints
对象并返回它 - 它接受 gridwidth
的关键字可选参数>、gridheight
等)
我想创建行内容的向量((.add panel ...)
调用)并使用 ( map-indexed ...)
填写 INDEX 值。
我能想到的唯一方法是使每个 (.add panel ...)
成为一个参数(索引)的匿名函数:
(dorun (map-indexed #(%2 %1)
[#(.add panel (.JLabel "Hello") (fill-gbc 0 %)) ...]))
是否有更好的方法来做到这一点,也许使用宏(我在应用程序中的各种对话框中多次需要此模式)?
I am writing code to fill a java.awt.GridBagLayout with "rows" of controls. For each row, I have a method call of the form
(.add panel CONTROL (fill-gbc 0 INDEX ...))
where CONTROL is the Swing control to place at this row (i.e.: (JLabel. "Hello")
) and INDEX is the gridy
for that control (fill-gbc
fills a single, mutable, GridBagContraints
object and returns it -- it accepts keyword optional parameters for gridwidth
, gridheight
, etc.)
I would like to create a vector of the row contents (the (.add panel ...)
calls) and use (map-indexed ...)
to fill in the INDEX value.
The only way that I can come up with do do this is to make each (.add panel ...)
an anonymous function of one parameter (the index):
(dorun (map-indexed #(%2 %1)
[#(.add panel (.JLabel "Hello") (fill-gbc 0 %)) ...]))
Is there a better way to do this, perhaps with a macro (I'll need this pattern several times in my application for various dialog boxes)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其抽象为一个函数,然后您可以在任何需要的地方使用它。
您只需传递任何可能变化的信息的参数。
此外,我编写了一个小宏,用于将一堆东西添加到容器中。
这让你可以写这样的东西:
不确定在这种情况下这对你是否有帮助,但可能有。
You could abstract this away into a function, then you can use it wherever you need it.
You'd just pass parameters for whatever information will ever vary.
Furthermore, I wrote a little macro for adding a bunch of things to a container.
That let's you write stuff like this:
Not sure if that's helpful for you in this situation, but it might be.