javaについての質問[JavaFX]

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
free
記事: 29
登録日時: 9年前

javaについての質問[JavaFX]

#1

投稿記事 by free » 8年前

こんにちは。久しぶりの質問です。
このサイトは一応javaの質問もしているそうなのでトピック立ち上げてみました。

今、「Eclipse4.4ではじめる Javaプログラミング入門 Eclipse4.4 Luna 対応」という
本でJava学習しているのですが、エラーコードが出てもどう直したらよいのかわからないので
質問しました。

SampleApp.java

コード:

package jp.tuyano.eclipsebook;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;

public class SampleApp extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("window.fxml"));	
		Scene scene = new Scene(root,300,150);
		stage.setScene(scene);
		stage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}

}
window.fxml

コード:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?language javascript?>

<BorderPane prefHeight="150.0" prefWidth="300.0"
  xmlns="http://javafx.com/javafx/8"
  xmlns:fx="http://javafx.com/fxml/1">
  <fx:script>function buttobAction(e){
    ver str = text1.getText();
    label1.setText('you typed: "' + str + '"!');
  }</fx:script>
  <top>
    <Label fx:id="label1" text="this is FXML sample!"
    BorderPane.alignment="CENTER" />
  </top>
  <center>
    <TextField fx:id="text1" BorderPane.alignment="CENTER" />
  </center>
  <bottom>
    <Button fx:id="btn1" onAction="buttonAction(event);"
    text="Click" BorderPane.alignment="CENTER" />
  </bottom>
</BorderPane>  
上がjavaのコード、下がxml形式のコードで、テキスト(文章)を書いてボタンをクリックすると、
入力した文字がウィンドウの上に表示されるみたいですが、クリックすると、

(エラー文)
<eval>:2:8 Expected ; but found str
ver str = text1.getText();
^ in <eval> at line number 2 at column number 8

とコンソール(エラーや出力結果を表示するところ)に出力された後、ウィンドウや中のテキストフィールドと
ボタンは出るのですが、それをクリックすると、

(エラー文続き)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: javax.script.ScriptException: ReferenceError: "buttonAction" is not defined in <eval> at line number 1
at javafx.fxml.FXMLLoader$ScriptEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1800(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.script.ScriptException: ReferenceError: "buttonAction" is not defined in <eval> at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:564)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:548)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:528)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:524)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:194)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
... 47 more

・・・とこのようにエラーが出てしまいます。XMLなんてやったことないのでどこを直したらいいのかわかりません。
どなたか分かる方いらっしゃいますか?
(成功すると、自分が入力したテキストがウィンドウ上部に表示されるみたいです。)

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: javaについての質問[JavaFX]

#2

投稿記事 by ISLe » 8年前

window.fxmlの11行目の関数名が間違ってませんか?

エラー文続き の1行目に
"buttonAction" is not defined (buttonActionは定義されていない)
と書いてあります。

free
記事: 29
登録日時: 9年前

Re: javaについての質問[JavaFX]

#3

投稿記事 by free » 8年前

直してみました。確かに11行目のbuttonActionがbuttobActionになってました。
凡ミスですね。
それでもまだエラーがでてたので、window.fxmlの12行目のvarという単語(?)を消してみたらエラーが出なくなり、
普通に起動したので良かったのですが、突然エラーが出なくなった訳って何ですかね?
それについて教えて頂けますか?

修正後のSampleApp.java(上)とwindow.fxml(下)

コード:

package jp.tuyano.eclipsebook;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;

public class SampleApp extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Parent root = FXMLLoader.load(getClass().getResource("window.fxml"));	
		Scene scene = new Scene(root,300,150);
		stage.setScene(scene);
		stage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}

}

コード:

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?language javascript?>

<BorderPane prefHeight="150.0" prefWidth="300.0"
  xmlns="http://javafx.com/javafx/8"
  xmlns:fx="http://javafx.com/fxml/1">
  <fx:script>function buttonAction(e){
    str = text1.getText();
    label1.setText('you typed: "' + str + '"!');
  }</fx:script>
  <top>
    <Label fx:id="label1" text="this is FXML sample!"
    BorderPane.alignment="CENTER" />
  </top>
  <center>
    <TextField fx:id="text1" BorderPane.alignment="CENTER" />
  </center>
  <bottom>
    <Button fx:id="btn1" onAction="buttonAction(event);"
    text="Click" BorderPane.alignment="CENTER" />
  </bottom>
</BorderPane>  

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: javaについての質問[JavaFX]

#4

投稿記事 by みけCAT » 8年前

free さんが書きました:window.fxmlの12行目のvarという単語(?)を消してみたらエラーが出なくなり、
普通に起動したので良かったのですが
本当ですか?
修正前のwindow.fxmlの12行目にはverという単語(トークン?)はあってもvarという単語は見当たらないのですが、本当にvarという単語を消したのですか?
オフトピック
よく見たら、ちゃんとここがおかしいって指摘されていますね…
free さんが書きました:クリックすると、

(エラー文)
<eval>:2:8 Expected ; but found str
ver str = text1.getText();
^ in <eval> at line number 2 at column number 8

とコンソール(エラーや出力結果を表示するところ)に出力された後
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

free
記事: 29
登録日時: 9年前

Re: javaについての質問[JavaFX]

#5

投稿記事 by free » 8年前

みけCAT さんが書きました:
free さんが書きました:window.fxmlの12行目のvarという単語(?)を消してみたらエラーが出なくなり、
普通に起動したので良かったのですが
本当ですか?
修正前のwindow.fxmlの12行目にはverという単語(トークン?)はあってもvarという単語は見当たらないのですが、本当にvarという単語を消したのですか?
オフトピック
よく見たら、ちゃんとここがおかしいって指摘されていますね…
free さんが書きました:クリックすると、

(エラー文)
<eval>:2:8 Expected ; but found str
ver str = text1.getText();
^ in <eval> at line number 2 at column number 8

とコンソール(エラーや出力結果を表示するところ)に出力された後
比較してみました。(問題の<fx:script>文)
(最初の投稿時(1))

コード:

  <fx:script>function buttonAction(e){
    ver str = text1.getText();
    label1.setText('you typed: "' + str + '"!');
  }</fx:script>
(後に投稿した文(2))strのみでverを消した

コード:

  <fx:script>function buttonAction(e){
    str = text1.getText();
    label1.setText('you typed: "' + str + '"!');
  }</fx:script>
(みけCATさんの指摘後にもう一度訂正、実行した文(3))strの前にvarを加えた

コード:

  <fx:script>function buttonAction(e){
    var str = text1.getText();
    label1.setText('you typed: "' + str + '"!');
  }</fx:script>
確かに最初に投稿した文と、後に投稿した文では違ってました。すみません。
Offtopicでのエラー文はそのためのものだったのですね。
(1)ではエラー、(2)(3)では何事もなく動いたのですが、varというのは何でしょうか?
(というよりもvarは記述する必要があるんでしょうか?)

閉鎖

“C言語何でも質問掲示板” へ戻る