Clojure +跷跷板:为什么此GUI不起作用?

时间:2018-06-30 18:26:29

标签: clojure seesaw

我正尝试创建一个项目,就像跷跷板示例window-builder一样,我创建了一个名为my-gui-project的项目,并复制了src/window_builder/core.cljsrc/window_builder/MyForm.java中的代码进入我的项目,然后更改第31行的内容:

(let [form (identify (window_builder.MyForm.))]

进入:

(let [form (identify (my_gui_project.MyForm.))]

然后,我运行以下命令:lein depslein compilelein run -m my-gui-project.core,它返回以下错误:

Exception in thread "main" java.lang.ClassNotFoundException: my_gui_project.MyForm, compiling:(my_gui_project/core.clj:31:24)

为什么会这样?代码完全相同,我只是更改了文件夹名称。

这是完整的代码:

core.clj

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

; This is the interesting part. Note that in MyPanel.java, the widgets we're
; interested in have their name set with setName().
(defn identify
  "Given a root widget, find all the named widgets and set their Seesaw :id
   so they can play nicely with select and everything."
  [root]
  (doseq [w (select root [:*])]
    (if-let [n (.getName w)]
      (selector/id-of! w (keyword n))))
  root)

(def states ["CA", "GA", "WA"])

(def defaults
  { :first-name "Laura"
    :last-name "Palmer"
    :street "123 Main St."
    :city "Twin Peaks"
    :zip "12345"
    :state "WA" })

; A helper to create an instance of the form, annotate it for Seesaw and do
; some other initialization.
(defn my-form
  []
  (let [form (identify (my_gui_project.MyForm.))]
    ; initialize the state combobox
    (config! (select form [:#state]) :model states)
    form))

; Now we just create the panel, initialize it to the defaults above with
; seesaw.core/value! and show it in a dialog. Note how, besides setting the
; names of the widgets, the code in MyForm.java is strictly for layout. All
; behavior, etc is done in Clojure.
(defn -main [& args]
  (invoke-later
    (let [form  (value! (my-form) defaults)
          result (-> (dialog :content form :option-type :ok-cancel) pack! show!)]
      (if (= :success result)
        (println "User entered: " (value form))
        (println "User canceled")))))

project.clj

(defproject my-gui-project "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [seesaw "LATEST"]]
  :main ^:skip-aot my-gui-project.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

2 个答案:

答案 0 :(得分:1)

通过测试,我发现MyForm.java内还引用了项目名称。更改MyForm.java中的名称后,它就可以使用。

编辑:正如@Carcigenicate在帖子(现已删除)中所引用的那样,还需要在:java-source-paths ["src"]

中添加project.clj

原始MyForm.java

package window_builder;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

public class MyForm extends JPanel {
  private JTextField firstName;
  private JTextField lastName;
  private JLabel lblStreet;
  private JTextField textField;
  private JLabel lblCity;
  private JTextField textField_1;
  private JLabel lblState;
  private JComboBox comboBox;
  private JLabel lblState_1;
  private JTextField textField_2;

  /**
   * Create the panel.
   */
  public MyForm() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{72, 134, 0, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{28, 28, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JLabel lblFirstName = new JLabel("First Name:");
    GridBagConstraints gbc_lblFirstName = new GridBagConstraints();
    gbc_lblFirstName.anchor = GridBagConstraints.WEST;
    gbc_lblFirstName.insets = new Insets(0, 0, 5, 5);
    gbc_lblFirstName.gridx = 0;
    gbc_lblFirstName.gridy = 0;
    add(lblFirstName, gbc_lblFirstName);

    firstName = new JTextField();
    firstName.setName("first-name");
    firstName.setColumns(10);
    GridBagConstraints gbc_firstName = new GridBagConstraints();
    gbc_firstName.gridwidth = 2;
    gbc_firstName.anchor = GridBagConstraints.NORTH;
    gbc_firstName.fill = GridBagConstraints.HORIZONTAL;
    gbc_firstName.insets = new Insets(0, 0, 5, 5);
    gbc_firstName.gridx = 1;
    gbc_firstName.gridy = 0;
    add(firstName, gbc_firstName);

    JLabel lblLastName = new JLabel("Last Name:");
    GridBagConstraints gbc_lblLastName = new GridBagConstraints();
    gbc_lblLastName.anchor = GridBagConstraints.WEST;
    gbc_lblLastName.insets = new Insets(0, 0, 5, 5);
    gbc_lblLastName.gridx = 3;
    gbc_lblLastName.gridy = 0;
    add(lblLastName, gbc_lblLastName);

    lastName = new JTextField();
    lastName.setName("last-name");
    lastName.setColumns(10);
    GridBagConstraints gbc_lastName = new GridBagConstraints();
    gbc_lastName.gridwidth = 2;
    gbc_lastName.fill = GridBagConstraints.HORIZONTAL;
    gbc_lastName.insets = new Insets(0, 0, 5, 0);
    gbc_lastName.anchor = GridBagConstraints.NORTH;
    gbc_lastName.gridx = 4;
    gbc_lastName.gridy = 0;
    add(lastName, gbc_lastName);

    lblStreet = new JLabel("Street:");
    GridBagConstraints gbc_lblStreet = new GridBagConstraints();
    gbc_lblStreet.anchor = GridBagConstraints.EAST;
    gbc_lblStreet.insets = new Insets(0, 0, 5, 5);
    gbc_lblStreet.gridx = 0;
    gbc_lblStreet.gridy = 1;
    add(lblStreet, gbc_lblStreet);

    textField = new JTextField();
    textField.setName("street");
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.gridwidth = 5;
    gbc_textField.insets = new Insets(0, 0, 5, 0);
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.gridx = 1;
    gbc_textField.gridy = 1;
    add(textField, gbc_textField);
    textField.setColumns(10);

    lblCity = new JLabel("City:");
    GridBagConstraints gbc_lblCity = new GridBagConstraints();
    gbc_lblCity.anchor = GridBagConstraints.EAST;
    gbc_lblCity.insets = new Insets(0, 0, 0, 5);
    gbc_lblCity.gridx = 0;
    gbc_lblCity.gridy = 2;
    add(lblCity, gbc_lblCity);

    textField_1 = new JTextField();
    textField_1.setName("city");
    GridBagConstraints gbc_textField_1 = new GridBagConstraints();
    gbc_textField_1.insets = new Insets(0, 0, 0, 5);
    gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_1.gridx = 1;
    gbc_textField_1.gridy = 2;
    add(textField_1, gbc_textField_1);
    textField_1.setColumns(10);

    lblState_1 = new JLabel("State:");
    GridBagConstraints gbc_lblState_1 = new GridBagConstraints();
    gbc_lblState_1.insets = new Insets(0, 0, 0, 5);
    gbc_lblState_1.gridx = 2;
    gbc_lblState_1.gridy = 2;
    add(lblState_1, gbc_lblState_1);

    comboBox = new JComboBox();
    comboBox.setName("state");
    GridBagConstraints gbc_comboBox = new GridBagConstraints();
    gbc_comboBox.insets = new Insets(0, 0, 0, 5);
    gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
    gbc_comboBox.gridx = 3;
    gbc_comboBox.gridy = 2;
    add(comboBox, gbc_comboBox);

    lblState = new JLabel("Zip:");
    GridBagConstraints gbc_lblState = new GridBagConstraints();
    gbc_lblState.anchor = GridBagConstraints.EAST;
    gbc_lblState.insets = new Insets(0, 0, 0, 5);
    gbc_lblState.gridx = 4;
    gbc_lblState.gridy = 2;
    add(lblState, gbc_lblState);

    textField_2 = new JTextField();
    textField_2.setName("zip");
    GridBagConstraints gbc_textField_2 = new GridBagConstraints();
    gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_2.gridx = 5;
    gbc_textField_2.gridy = 2;
    add(textField_2, gbc_textField_2);
    textField_2.setColumns(10);

  }
}

更正了MyForm.java

package gui_example_application;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

public class MyForm extends JPanel {
  private JTextField firstName;
  private JTextField lastName;
  private JLabel lblStreet;
  private JTextField textField;
  private JLabel lblCity;
  private JTextField textField_1;
  private JLabel lblState;
  private JComboBox comboBox;
  private JLabel lblState_1;
  private JTextField textField_2;

  /**
   * Create the panel.
   */
  public MyForm() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{72, 134, 0, 0, 0, 0, 0};
    gridBagLayout.rowHeights = new int[]{28, 28, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JLabel lblFirstName = new JLabel("First Name:");
    GridBagConstraints gbc_lblFirstName = new GridBagConstraints();
    gbc_lblFirstName.anchor = GridBagConstraints.WEST;
    gbc_lblFirstName.insets = new Insets(0, 0, 5, 5);
    gbc_lblFirstName.gridx = 0;
    gbc_lblFirstName.gridy = 0;
    add(lblFirstName, gbc_lblFirstName);

    firstName = new JTextField();
    firstName.setName("first-name");
    firstName.setColumns(10);
    GridBagConstraints gbc_firstName = new GridBagConstraints();
    gbc_firstName.gridwidth = 2;
    gbc_firstName.anchor = GridBagConstraints.NORTH;
    gbc_firstName.fill = GridBagConstraints.HORIZONTAL;
    gbc_firstName.insets = new Insets(0, 0, 5, 5);
    gbc_firstName.gridx = 1;
    gbc_firstName.gridy = 0;
    add(firstName, gbc_firstName);

    JLabel lblLastName = new JLabel("Last Name:");
    GridBagConstraints gbc_lblLastName = new GridBagConstraints();
    gbc_lblLastName.anchor = GridBagConstraints.WEST;
    gbc_lblLastName.insets = new Insets(0, 0, 5, 5);
    gbc_lblLastName.gridx = 3;
    gbc_lblLastName.gridy = 0;
    add(lblLastName, gbc_lblLastName);

    lastName = new JTextField();
    lastName.setName("last-name");
    lastName.setColumns(10);
    GridBagConstraints gbc_lastName = new GridBagConstraints();
    gbc_lastName.gridwidth = 2;
    gbc_lastName.fill = GridBagConstraints.HORIZONTAL;
    gbc_lastName.insets = new Insets(0, 0, 5, 0);
    gbc_lastName.anchor = GridBagConstraints.NORTH;
    gbc_lastName.gridx = 4;
    gbc_lastName.gridy = 0;
    add(lastName, gbc_lastName);

    lblStreet = new JLabel("Street:");
    GridBagConstraints gbc_lblStreet = new GridBagConstraints();
    gbc_lblStreet.anchor = GridBagConstraints.EAST;
    gbc_lblStreet.insets = new Insets(0, 0, 5, 5);
    gbc_lblStreet.gridx = 0;
    gbc_lblStreet.gridy = 1;
    add(lblStreet, gbc_lblStreet);

    textField = new JTextField();
    textField.setName("street");
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.gridwidth = 5;
    gbc_textField.insets = new Insets(0, 0, 5, 0);
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.gridx = 1;
    gbc_textField.gridy = 1;
    add(textField, gbc_textField);
    textField.setColumns(10);

    lblCity = new JLabel("City:");
    GridBagConstraints gbc_lblCity = new GridBagConstraints();
    gbc_lblCity.anchor = GridBagConstraints.EAST;
    gbc_lblCity.insets = new Insets(0, 0, 0, 5);
    gbc_lblCity.gridx = 0;
    gbc_lblCity.gridy = 2;
    add(lblCity, gbc_lblCity);

    textField_1 = new JTextField();
    textField_1.setName("city");
    GridBagConstraints gbc_textField_1 = new GridBagConstraints();
    gbc_textField_1.insets = new Insets(0, 0, 0, 5);
    gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_1.gridx = 1;
    gbc_textField_1.gridy = 2;
    add(textField_1, gbc_textField_1);
    textField_1.setColumns(10);

    lblState_1 = new JLabel("State:");
    GridBagConstraints gbc_lblState_1 = new GridBagConstraints();
    gbc_lblState_1.insets = new Insets(0, 0, 0, 5);
    gbc_lblState_1.gridx = 2;
    gbc_lblState_1.gridy = 2;
    add(lblState_1, gbc_lblState_1);

    comboBox = new JComboBox();
    comboBox.setName("state");
    GridBagConstraints gbc_comboBox = new GridBagConstraints();
    gbc_comboBox.insets = new Insets(0, 0, 0, 5);
    gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
    gbc_comboBox.gridx = 3;
    gbc_comboBox.gridy = 2;
    add(comboBox, gbc_comboBox);

    lblState = new JLabel("Zip:");
    GridBagConstraints gbc_lblState = new GridBagConstraints();
    gbc_lblState.anchor = GridBagConstraints.EAST;
    gbc_lblState.insets = new Insets(0, 0, 0, 5);
    gbc_lblState.gridx = 4;
    gbc_lblState.gridy = 2;
    add(lblState, gbc_lblState);

    textField_2 = new JTextField();
    textField_2.setName("zip");
    GridBagConstraints gbc_textField_2 = new GridBagConstraints();
    gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField_2.gridx = 5;
    gbc_textField_2.gridy = 2;
    add(textField_2, gbc_textField_2);
    textField_2.setColumns(10);

  }
}

答案 1 :(得分:0)

您可能还需要重命名window_builder目录以匹配名称更改。并更改任何Java文件中的程序包名称。

这与directory_name / namespace.clj的Clojure中的项目命名模式保持一致,并且-切换为_。