【问题标题】:Clojure swing exception "No implementation of method: :children"Clojure swing异常“没有方法的实现::children”
【发布时间】:2018-10-02 21:53:02
【问题描述】:

学习如何使用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)

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

任何意见和建议将不胜感激。

【问题讨论】:

    标签: java eclipse user-interface clojure windowbuilder


    【解决方案1】:

    你的线索在这里:

    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。

    【讨论】:

    • 很好的stacktrace分析,但猜测肯定是错误的。构造函数在任何情况下都不能返回 nil:它们要么成功并返回一个对象,要么失败并抛出异常/错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多