繁体   English   中英

Clojure swing异常“未实现方法::children”

[英]Clojure swing exception “No implementation of method: :children”

通过使用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 ),并通过调用function :children尝试将其视为objct。 更多线索:

 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的便捷方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM