带有 GridBagLayout 索引的 Clojure 序列

发布于 2024-10-31 02:14:25 字数 743 浏览 1 评论 0原文

我正在编写代码来用控件“行”填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-11-07 02:14:25

您可以将其抽象为一个函数,然后您可以在任何需要的地方使用它。

(defn add-on-row [panel c]
  (dorun
   (map-indexed
    #(%2 %1)
    [#(.add panel c (fill-gbc 0 %)) ...])))

您只需传递任何可能变化的信息的参数。

此外,我编写了一个小宏,用于将一堆东西添加到容器中。

(defmacro add [cmp & things]
  (cons
   'do
   (for [thing things]
     `(.add ~cmp ~@(if (vector? thing) thing [thing])))))

这让你可以写这样的东西:

(add 
 panel 
 [(JLabel. "Hello") "more arguments"] 
 (JLabel "Hello!"))

不确定在这种情况下这对你是否有帮助,但可能有。

You could abstract this away into a function, then you can use it wherever you need it.

(defn add-on-row [panel c]
  (dorun
   (map-indexed
    #(%2 %1)
    [#(.add panel c (fill-gbc 0 %)) ...])))

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.

(defmacro add [cmp & things]
  (cons
   'do
   (for [thing things]
     `(.add ~cmp ~@(if (vector? thing) thing [thing])))))

That let's you write stuff like this:

(add 
 panel 
 [(JLabel. "Hello") "more arguments"] 
 (JLabel "Hello!"))

Not sure if that's helpful for you in this situation, but it might be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文