使用javafxml

时间:2015-11-01 22:59:47

标签: java javafx

我有一个问题,我已经坚持了几天,而且我没有找到任何答案。我会尝试尽可能地描述它。

我有一个显示文本和按钮的fxml文件,我想在按下按钮时更改文本文本。我希望它显示的文本是属于地图中某个键的值。

Fxml文件:

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

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="768" prefWidth="1366" styleClass="root" stylesheets="/PagTest/style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="PagTest.SessionController">
   <children>
      <Text fx:id="text1" layoutX="750.0" layoutY="455.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" AnchorPane.bottomAnchor="309.0" AnchorPane.leftAnchor="750.0" AnchorPane.rightAnchor="593.86328125" AnchorPane.topAnchor="442.0" />
      <Button fx:id="button" onAction="#button" layoutX="649.0" layoutY="438.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

关联的控制器

package PagTest;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.control.*;

import java.io.*;
import java.net.URL;
import java.util.*;


public class SessionController implements Initializable, ControlledScreen{

    public Text text1;
    public Button button;
    private String mFileName;
    ScreensController myController;
    private static Map <String, String> mPatient = new HashMap<>();

    public void setScreenParent (ScreensController screenParent) {myController = screenParent;}

    @FXML
    private void goToWelcomeScreen (ActionEvent event) {myController.setScreen(ScreensFramework.screen1ID);}

    @Override
    public void initialize(URL location, ResourceBundle resources) {}
    public void exit (ActionEvent event) {Platform.exit();}

    public void setSession(String fileName) throws IOException {
        mFileName = fileName;
        BufferedReader br = new BufferedReader(new FileReader(mFileName));
        String line = "";
        while ((line = br.readLine()) != null){
            String parts[] = line.split("\t");
            mPatient.put(parts[0], parts[1]);
        }
        br.close();
        System.out.println(mPatient.toString());
        System.out.println(mPatient.get("firstName"));
    }

    public void button(ActionEvent event) {
        System.out.println(mPatient.toString());
        text1.setText(mPatient.get("firstName"));
    }
}

setSession方法末尾的两个System.out.println完美地工作

如果要求代码用&#34; Hello&#34;替换文本。它可以工作,但当我要求它用地图中firstName键关联的值替换它时它不起作用,我收到错误。

最终我希望Text对象的文本在显示场景时更改,而不必按任何按钮。

我希望它足够清楚,我对java很新。

感谢您的帮助。

编辑:当我重新打开Intellij IDEA以粘贴错误时,我再次尝试,当我点击按钮时文本被正确更改....我不知道为什么。 但是当我在setSession方法中放置text1.setText(mPatient.get(&#34; firstName&#34;);时,我收到以下错误。

新代码:

package PagTest;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.control.*;

import javax.swing.text.JTextComponent;
import java.io.*;
import java.net.URL;
import java.util.*;


public class SessionController implements Initializable, ControlledScreen{

    public Text text1;
    public Button button;
    private String mFileName;
    ScreensController myController;
    private static Map <String, String> mPatient = new HashMap<>();

    public void setScreenParent (ScreensController screenParent) {myController = screenParent;}

    @FXML
    private void goToWelcomeScreen (ActionEvent event) {myController.setScreen(ScreensFramework.screen1ID);}

    @Override
    public void initialize(URL location, ResourceBundle resources) {}
    public void exit (ActionEvent event) {Platform.exit();}

    public void setSession(String fileName) throws IOException {
        mFileName = fileName;
        BufferedReader br = new BufferedReader(new FileReader(mFileName));
        String line = "";
        while ((line = br.readLine()) != null){
            String parts[] = line.split("\t");
            mPatient.put(parts[0], parts[1]);
        }
        br.close();
        System.out.println(mPatient.toString());
        System.out.println(mPatient.get("firstName"));
        text1.setText(mPatient.get("firstName"));
    }

    public void button(ActionEvent event) {
        System.out.println(mPatient.toString());
    }
}

从另一个名为WelcomeController的类调用setSession()方法,该类传递fileName并设置Session.fxml的场景

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 52 more
Caused by: java.lang.NullPointerException
    at PagTest.SessionController.setSession(SessionController.java:44)
    at PagTest.WelcomeController.goToPatientList(WelcomeController.java:30)
    ... 62 more

EDIT2: 这是WelcomeController,当点击一个按钮时,在goToSession中调用setSession(...)

package PagTest;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class WelcomeController implements Initializable, ControlledScreen {


    ScreensController myController;

    public void setScreenParent (ScreensController screenParent) {
        myController = screenParent;
    }

    @FXML
    private void goToWelcomeScreen (ActionEvent event) {myController.setScreen(ScreensFramework.screen1ID);}

    @FXML
    private void goToNewPatientForm (ActionEvent event) {myController.setScreen(ScreensFramework.screen2ID);}

    @FXML
    private void goToSession (ActionEvent event) throws IOException {
        SessionController session = new SessionController();
        session.setSession("resources\\PatientForms\\Pierre-Alexandre Gross.txt");
        myController.setScreen(ScreensFramework.screen4ID);
    }

    @FXML
    private void exit (ActionEvent event) {
        Platform.exit();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }
}

0 个答案:

没有答案