1

JSP、HTML、JavaScript を JavaFX に置き換えようとしているので、FXML ファイルとビジネス ロジックを Web サーバーに保持しました。サーバーから FXML を読み込んで、単純な JavaFX(java) コードでクライアント側に表示することはできますが、イベント ハンドラー (コントローラー) を動的に読み込むことはできません。クライアントアプリを軽量アプリにしたい。

誰かがこれを行うためのより良い方法を提案できますか?

編集: fxml ファイルでイベント ハンドラー クラス名を指定する必要があります。イベントハンドラのオブジェクトは、FXMLLoader による fxml のロード時にインスタンス化されます。fxml とイベント ハンドラー クラスを tomcat サーバーに保持しました。URLConnection を使用してサーバーから fxml をロードするアプリケーションを 1 つ作成しました。これで fxml がロードされましたが、fxml ファイルで定義されたコントロールのイベントを処理できません。FXMLLoader によって fxml をロードしている間、イベント ハンドラ クラスも FXMLLoader によってインスタンス化されるためです。クライアント アプリケーションのイベント ハンドラ クラスで使用できません。ただし、イベント ハンドラーは tomcat サーバーで使用できます。サーバーからクラス ファイルをロードし、クライアント側でクラス ファイル (イベント ハンドラ) を動的にインスタンス化する方法はありますか。

4

2 に答える 2

4

JavaScript などのスクリプト言語を使用することは、クライアント マシンでコンパイルが不要になるように、関連する制御ロジックを含む fxml ページをサーバーからロードする 1 つの方法です。確立された html+JavaScript モデルと非常によく似ています。

このアプローチの例として、WebFXを試すことができます。

メトロノーム.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?language javascript?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>

<AnchorPane id="AnchorPane" prefHeight="370.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <HBox id="HBox" alignment="CENTER" layoutX="36.0" layoutY="328.0" spacing="5.0">
      <children>
        <Button fx:id="startButton" mnemonicParsing="false" onAction="handleStartButtonAction(event);" text="%start" />
        <Button fx:id="pauseButton" mnemonicParsing="false" onAction="handlePauseButtonAction(event);" text="%pause" />
        <Button fx:id="resumeButton" mnemonicParsing="false" onAction="handleResumeButtonAction(event);" text="%resume" />
        <Button fx:id="stopButton" mnemonicParsing="false" onAction="handleStopButtonAction(event);" text="%stop" />
      </children>
    </HBox>
    <Circle fx:id="circle" fill="RED" layoutX="64.0" layoutY="58.0" radius="7.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" />
  </children>

  <fx:script source="metronome.js" />
</AnchorPane>

メトロノーム.js

var webfx = {title: "Metronome WebFX Sample"};

var java = Packages.java;
var javafx = Packages.javafx;

var URL = java.net.URL;
var ResourceBundle = java.util.ResourceBundle;

var Animation = javafx.animation.Animation;
var Interpolator = javafx.animation.Interpolator;
var Timeline = javafx.animation.Timeline;
var TranslateTransitionBuilder = javafx.animation.TranslateTransitionBuilder;
var Duration = javafx.util.Duration;

var anim = TranslateTransitionBuilder.create()
        .duration(new Duration(1000.0))
        .node(circle)
        .fromX(0)
        .toX(200)
        .interpolator(Interpolator.LINEAR)
        .autoReverse(true)
        .cycleCount(Timeline.INDEFINITE)
        .build();

function handleStartButtonAction()  { anim.playFromStart(); }    
function handlePauseButtonAction()  { anim.pause(); }    
function handleResumeButtonAction() { anim.play();  }
function handleStopButtonAction()   { anim.stop();  }    
startButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.STOPPED));
pauseButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.RUNNING));
resumeButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.PAUSED));
stopButton.disableProperty().bind(anim.statusProperty().isEqualTo(Animation.Status.STOPPED));

JavaScript のようなスクリプト言語の代わりに、Java のような静的言語をコントローラに使用したい場合は、コンパイルされたクラス ファイルをクライアントで取得する方法を見つける必要があります。たとえば、サーバー上でコンパイルしてサーバーからロードできるクラスローダーを用意するか、クライアント アプリに Java コンパイラを同梱してソース コードをコンパイルします。

于 2013-10-28T05:41:03.870 に答える
1

サーバーからロードできるクラスローダーを持つ

の Java ReStart テクノロジ( https://github.com/pjBooms/ Java-ReStart )。

于 2014-11-22T06:38:36.793 に答える