java - Clojure Swing 异常 "No implementation of method: :children"

标签 java eclipse user-interface clojure windowbuilder

学习如何使用 swing 库和 eclipse 编辑器使用 clojure 制作 GUI。目前,我遇到了在 Eclipse 中通过 leiningen 执行应用程序的麻烦。

我在 Java 上制作网格,并尝试将 Java 代码内的所有小部件名称连接到 clojure id,以按照官方 github 文档中指定的方式进一步操作,如下所示:

core.clj

 (ns my.core
  (:gen-class)
  (:use [seesaw.core])
  (:require [seesaw.selector :as selector]))

(defn identify
  [root]
  (doseq [w (select root [:*])]
    (if-let [n (.getName w)]
      (selector/id-of! w (keyword n))))
  root)


(defn my-form
  []
  (let [form (identify (my.Gui.))]

    form))

(defn -main [& args]
;;  With two follow line of code project compiles fine without any errors if replace bottom lines
;;  (invoke-later
;;    (show! (frame :title "Hello" :content (button :text "Push me")))))
  (invoke-later
   (let [form  (my-form)
         output (-> form pack! show!)]
       nil))

project.clj

(defproject my "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.8.0"]
[seesaw "LATEST"]]
  :main ^:skip-aot my.core
  :java-source-paths ["src"])

Gui.java

package my;

import java.awt.EventQueue;

import javax.swing.JFrame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import net.miginfocom.swing.MigLayout;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import java.awt.Color;

public class Gui{

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Gui window = new Gui();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Gui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.RED);
    frame.setForeground(Color.PINK);
    frame.setBackground(Color.ORANGE);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JComboBox comboBox = new JComboBox();
    comboBox.setName("cmb");
    comboBox.setBackground(Color.CYAN);
    comboBox.setForeground(Color.CYAN);
    comboBox.setBounds(10, 11, 101, 50);
    frame.getContentPane().add(comboBox);

    JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
    rdbtnNewRadioButton.setName("rdbtn");
    rdbtnNewRadioButton.setBackground(Color.RED);
    rdbtnNewRadioButton.setForeground(Color.MAGENTA);
    rdbtnNewRadioButton.setBounds(10, 84, 109, 23);
    frame.getContentPane().add(rdbtnNewRadioButton);
}
}

错误消息

Compiling 1 source files to C:\Users\Samsung\my\target\classes
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No implementation of method: :children of protocol: #'seesaw.util/Children found for class: nil
    at clojure.core$_cache_protocol_fn.invokeStatic(core_deftype.clj:568)
    at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:560)
    at seesaw.util$eval118$fn__119$G__109__124.invoke(util.clj:151)
    at clojure.zip$children.invokeStatic(zip.clj:80)
    at clojure.zip$down.invokeStatic(zip.clj:115)
    at clojure.zip$down.invoke(zip.clj:109)
    at seesaw.selector$children_locs.invokeStatic(selector.clj:313)
    at seesaw.selector$children_locs.invoke(selector.clj:312)
    at seesaw.selector$zip_select_nodes_STAR_$select1__1431.invoke(selector.clj:318)
    at seesaw.selector$zip_select_nodes_STAR_$fn__1436.invoke(selector.clj:320)
    at clojure.core$map$fn__4785.invoke(core.clj:2644)
    at clojure.lang.LazySeq.sval(LazySeq.java:40)
    at clojure.lang.LazySeq.seq(LazySeq.java:49)
    at clojure.lang.RT.seq(RT.java:521)
    at clojure.core$seq__4357.invokeStatic(core.clj:137)
    at clojure.core$apply.invokeStatic(core.clj:641)
    at clojure.core$mapcat.invokeStatic(core.clj:2674)
    at clojure.core$mapcat.doInvoke(core.clj:2674)
    at clojure.lang.RestFn.invoke(RestFn.java:423)
    at seesaw.selector$zip_select_nodes_STAR_.invokeStatic(selector.clj:320)
    at seesaw.selector$zip_select_nodes_STAR_.invoke(selector.clj:315)
    at seesaw.selector$select_nodes_STAR_.invokeStatic(selector.clj:324)
    at seesaw.selector$select_nodes_STAR_.invoke(selector.clj:322)
    at seesaw.selector$select.invokeStatic(selector.clj:364)
    at seesaw.selector$select.invoke(selector.clj:358)
    at seesaw.core$select.invokeStatic(core.clj:3587)
    at seesaw.core$select.invoke(core.clj:3517)
    at my.core$identify.invokeStatic(core.clj:8)
    at my.core$identify.invoke(core.clj:6)
    at my.core$my_form.invokeStatic(core.clj:16)
    at my.core$my_form.invoke(core.clj:14)
    at my.core$_main$fn__5440.invoke(core.clj:25)
    at clojure.lang.AFn.applyToHelper(AFn.java:152)
    at clojure.lang.AFn.applyTo(AFn.java:144)
    at clojure.core$apply.invokeStatic(core.clj:646)
    at clojure.core$apply.invoke(core.clj:641)
    at seesaw.invoke$invoke_later_STAR_$fn__744.invoke(invoke.clj:14)
    at clojure.lang.AFn.run(AFn.java:22)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

无法意识到我哪里做错了,使用 Eclipse 的 Window Builder 插件创建后,Java 代码没有修改,并且 clojure 脚本是通过文档制作的。 Eclipse 的 Window Builder 插件可能已过时,或者不再用于创建 GUI 应用程序。

如有任何意见和建议,我们将不胜感激。

最佳答案

你的线索在这里:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No implementation of method: :children of protocol: #'seesaw.util/Children found for class: nil
    at clojure.core$_cache_protocol_fn.invokeStatic(core_deftype.clj:568)

您正在获取一个 nil 值 (java null),并尝试通过调用函数 :children 将其视为对象。更多线索:

 at my.core$identify.invokeStatic(core.clj:8)
    at my.core$identify.invoke(core.clj:6)
    at my.core$my_form.invokeStatic(core.clj:16)
    at my.core$my_form.invoke(core.clj:14)
    at my.core$_main$fn__5440.invoke(core.clj:25)

问题从第 8 行开始:

(ns my.core
  (:gen-class)
  (:use [seesaw.core])
  (:require [seesaw.selector :as selector]))

(defn identify
  [root]
  (doseq [w (select root [:*])]   ;  <=  ***** PROBLEM IS NEAR HERE *****
    (if-let [n (.getName w)]
      (selector/id-of! w (keyword n))))
  root)

我猜你的构造函数(my.Gui.)失败并返回了nil。我将使用 (println :101 some-val) 等行进一步调试,其中 :101 提供了一种向每行添加 ID 的便捷方法。

关于java - Clojure Swing 异常 "No implementation of method: :children",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616947/

相关文章:

java - 检查用户是否订阅的正确方法?

java - 为什么 Eclipse 说 "errors exist"但在控制台中什么也没显示?

python - 如何使 Python Tkinter 中的按钮保持按下状态直到按下另一个按钮

c++ - 如何向 QListWidgetItem 插入子布局?

ios - 应用程序中的所有元素都是像素化的

java - 用于Scala开发的java8中的gradle REPL

java - Soot - 如何防止 loadClassAndSupport 创建空的 SootClass?

java - 对于 facelets 什么相当于 fmt :formatDate?

java - selenium-server-standalone-2.37.0.jar 损坏了?

android - AuthException 未解析为类型和多个 dex 文件 - 构建路径