Aiuto java

di il
31 risposte

Aiuto java

Buonasera !
Ho scritto il seguente codice in fxml utilizzando SceneBuilder:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
  <children>
    <MenuBar VBox.vgrow="NEVER">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="New" />
            <MenuItem mnemonicParsing="false" text="Open…" />
            <Menu mnemonicParsing="false" text="Open Recent" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Close" />
            <MenuItem mnemonicParsing="false" text="Save" />
            <MenuItem mnemonicParsing="false" text="Save As…" />
            <MenuItem mnemonicParsing="false" text="Revert" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Preferences…" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Quit" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Edit">
          <items>
            <MenuItem mnemonicParsing="false" text="Undo" />
            <MenuItem mnemonicParsing="false" text="Redo" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Cut" />
            <MenuItem mnemonicParsing="false" text="Copy" />
            <MenuItem mnemonicParsing="false" text="Paste" />
            <MenuItem mnemonicParsing="false" text="Delete" />
            <SeparatorMenuItem mnemonicParsing="false" />
            <MenuItem mnemonicParsing="false" text="Select All" />
            <MenuItem mnemonicParsing="false" text="Unselect All" />
          </items>
        </Menu>
        <Menu mnemonicParsing="false" text="Help">
          <items>
            <MenuItem mnemonicParsing="false" text="About MyHelloApp" />
          </items>
        </Menu>
      </menus>
    </MenuBar>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
         <children>
            <GridPane layoutX="80.0" layoutY="186.0" prefHeight="371.5" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Button mnemonicParsing="false" text="Button" />
                  <TextField fx:id="bottone1" onAction="#Button" prefHeight="27.0" prefWidth="281.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <TextField fx:id="bottone" onAction="#Button" GridPane.columnIndex="1" />
               </children>
            </GridPane>
         </children>
    </AnchorPane>
  </children>
</VBox>
Lo scopo è quello di avviare l'interfaccia. Ho realizzato il codice in java:

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.fxml.*;
import java.time.LocalDate;

public class Avvio extends Application{
  public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage stage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("newinterfaccia.fxml"));
    Scene scene = new Scene(root);
    stage.setTitle("Programma");
    stage.setScene(scene);
    stage.show();
  }
}
Il problema è che quando vado ad eseguire da terminale ottengo questo errore:

Exception in Application start method
java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
	at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException: No controller specified.
/Users/giorgiomartesi/Desktop/1/interfaccia/newinterfaccia.fxml:69

	at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
	at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
	at javafx.fxml/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:565)
	at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:607)
	at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
	at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
	at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
	at Avvio.start(Avvio.java:13)
	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
	at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application Avvio
Qualcuno può aiutarmi a risolvere questo problema? Grazie in anticipo!!

31 Risposte

  • Re: Aiuto java

    giorgiomarte ha scritto:


    Caused by: javafx.fxml.LoadException: No controller specified.
    Il messaggio è chiaro (e lo si evince anche dal fxml). Non hai specificato (e credo nemmeno creato materialmente) la classe che deve fare da "controller".

    Se non hai idea di che cosa sia la classe "controller", documentati. Ad esempio: https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#controllers
  • Re: Aiuto java

    Il messaggio è chiaro (e lo si evince anche dal fxml). Non hai specificato (e credo nemmeno creato materialmente) la classe che deve fare da "controller".
    Ho creato anche quella:
    
    import javafx.fxml.FXML;
    import javafx.scene.control.TextField;
    
    public class PleaseProvideControllerClassName {
    
        @FXML
        private TextField bottone1;
    
        @FXML
        private TextField bottone;
    
        @FXML
        void Button(ActionEvent event) {
          bottone.setText(bottone1.getText());
    
        }
    
    }
    
    come file.java e salvata nella stessa cartella di avvio.java... come mai crea errore??
  • Re: Aiuto java

    Il nome qualificato della classe controller va specificato nel FXML.
  • Re: Aiuto java

    Il nome qualificato della classe controller va specificato nel FXML.
    Come si fa?? da SceneBuilder??
  • Re: Aiuto java

    giorgiomarte ha scritto:


    Come si fa?? da SceneBuilder??
    https://stackoverflow.com/questions/40993769/how-can-i-set-the-controller-in-the-scene-builder
  • Re: Aiuto java

    Ho specificato il nome della classe controller nel FXML.
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Menu?>
    <?import javafx.scene.control.MenuBar?>
    <?import javafx.scene.control.MenuItem?>
    <?import javafx.scene.control.SeparatorMenuItem?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.ColumnConstraints?>
    <?import javafx.scene.layout.GridPane?>
    <?import javafx.scene.layout.RowConstraints?>
    <?import javafx.scene.layout.VBox?>
    
    <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="interfaccia.Controller">
      <children>
        <MenuBar VBox.vgrow="NEVER">
          <menus>
            <Menu mnemonicParsing="false" text="File">
              <items>
                <MenuItem mnemonicParsing="false" text="New" />
                <MenuItem mnemonicParsing="false" text="Open…" />
                <Menu mnemonicParsing="false" text="Open Recent" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Close" />
                <MenuItem mnemonicParsing="false" text="Save" />
                <MenuItem mnemonicParsing="false" text="Save As…" />
                <MenuItem mnemonicParsing="false" text="Revert" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Preferences…" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Quit" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Edit">
              <items>
                <MenuItem mnemonicParsing="false" text="Undo" />
                <MenuItem mnemonicParsing="false" text="Redo" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Cut" />
                <MenuItem mnemonicParsing="false" text="Copy" />
                <MenuItem mnemonicParsing="false" text="Paste" />
                <MenuItem mnemonicParsing="false" text="Delete" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Select All" />
                <MenuItem mnemonicParsing="false" text="Unselect All" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
              <items>
                <MenuItem mnemonicParsing="false" text="About MyHelloApp" />
              </items>
            </Menu>
          </menus>
        </MenuBar>
        <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
             <children>
                <GridPane layoutX="80.0" layoutY="186.0" prefHeight="371.5" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <columnConstraints>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  </columnConstraints>
                  <rowConstraints>
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  </rowConstraints>
                   <children>
                      <Button mnemonicParsing="false" text="Button" />
                      <TextField fx:id="bottone1" onAction="#Button" prefHeight="27.0" prefWidth="281.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                      <TextField fx:id="bottone" onAction="#Button" GridPane.columnIndex="1" />
                   </children>
                </GridPane>
             </children>
        </AnchorPane>
      </children>
    </VBox>
    
    Creando anche il nuovo file controller.java, pero ottengo sempre errore:
    
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    	at java.base/java.lang.Thread.run(Thread.java:844)
    Caused by: javafx.fxml.LoadException: 
    /Users/giorgiomartesi/Desktop/1/interfaccia/newinterfaccia.fxml:15
    
    	at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    	at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
    	at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:930)
    	at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
    	at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
    	at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
    	at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    	at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    	at Avvio.start(Avvio.java:13)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    	at java.base/java.security.AccessController.doPrivileged(Native Method)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    	at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    Caused by: java.lang.ClassNotFoundException: interfaccia.Controller
    	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    	at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:928)
    	... 19 more
    Exception running application Avvio
    
    Perche??
  • Re: Aiuto java

    giorgiomarte ha scritto:


    Caused by: java.lang.ClassNotFoundException: interfaccia.Controller
    Anche qui il messaggio mi pare più che chiaro. Sicuro che il nome completamente qualificato della classe sia interfaccia.Controller?
  • Re: Aiuto java

    Ho creato una cartella nella quale ho caricato il file Avvio.java :
    
    import javafx.application.*;
    import javafx.stage.*;
    import javafx.scene.*;
    import javafx.fxml.*;
    import java.time.LocalDate;
    
    public class Avvio extends Application{
      public static void main(String[] args) {
        launch(args);
      }
      @Override
      public void start(Stage stage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("interfaccia/newinterfaccia.fxml"));
        Scene scene = new Scene(root);
        stage.setTitle("Programma Gestionale");
        stage.setScene(scene);
        stage.show();
      }
    }
    
    nella stessa cartella ne ho creata un altra (di nome inerfaccia) nella quale ho inserito:
    Controller.java :
    
    package interfaccia;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.TextField;
    
    public class Controller {
    
        @FXML
        private TextField bottone1;
    
        @FXML
        private TextField bottone;
    
        @FXML
        void Button(ActionEvent event) {
    
        }
    
    }
    
    e newinterfaccia.java :
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Menu?>
    <?import javafx.scene.control.MenuBar?>
    <?import javafx.scene.control.MenuItem?>
    <?import javafx.scene.control.SeparatorMenuItem?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.ColumnConstraints?>
    <?import javafx.scene.layout.GridPane?>
    <?import javafx.scene.layout.RowConstraints?>
    <?import javafx.scene.layout.VBox?>
    
    <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="interfaccia.Controller">
      <children>
        <MenuBar VBox.vgrow="NEVER">
          <menus>
            <Menu mnemonicParsing="false" text="File">
              <items>
                <MenuItem mnemonicParsing="false" text="New" />
                <MenuItem mnemonicParsing="false" text="Open…" />
                <Menu mnemonicParsing="false" text="Open Recent" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Close" />
                <MenuItem mnemonicParsing="false" text="Save" />
                <MenuItem mnemonicParsing="false" text="Save As…" />
                <MenuItem mnemonicParsing="false" text="Revert" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Preferences…" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Quit" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Edit">
              <items>
                <MenuItem mnemonicParsing="false" text="Undo" />
                <MenuItem mnemonicParsing="false" text="Redo" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Cut" />
                <MenuItem mnemonicParsing="false" text="Copy" />
                <MenuItem mnemonicParsing="false" text="Paste" />
                <MenuItem mnemonicParsing="false" text="Delete" />
                <SeparatorMenuItem mnemonicParsing="false" />
                <MenuItem mnemonicParsing="false" text="Select All" />
                <MenuItem mnemonicParsing="false" text="Unselect All" />
              </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Help">
              <items>
                <MenuItem mnemonicParsing="false" text="About MyHelloApp" />
              </items>
            </Menu>
          </menus>
        </MenuBar>
        <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
             <children>
                <GridPane layoutX="80.0" layoutY="186.0" prefHeight="371.5" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                  <columnConstraints>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  </columnConstraints>
                  <rowConstraints>
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  </rowConstraints>
                   <children>
                      <Button mnemonicParsing="false" text="Button" />
                      <TextField fx:id="bottone1" onAction="#Button" prefHeight="27.0" prefWidth="281.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                      <TextField fx:id="bottone" onAction="#Button" GridPane.columnIndex="1" />
                   </children>
                </GridPane>
             </children>
        </AnchorPane>
      </children>
    </VBox>
    
    Ho sbagliato ?? il nome della classe è errato??
  • Re: Aiuto java

    giorgiomarte ha scritto:


    Ho sbagliato ?? il nome della classe è errato??
    Il nome risulta giusto.

    Ma ... come compili/avvii? Da prompt comandi con i tools del JDK? Da un IDE?
  • Re: Aiuto java

    Da prompt comandi, nel seguente modo:
    
    MBP-di-giorgio:~ giorgiomartesi$ cd Desktop
    MBP-di-giorgio:Desktop giorgiomartesi$ cd 1
    MBP-di-giorgio:1 giorgiomartesi$ javac Avvio.java
    MBP-di-giorgio:1 giorgiomartesi$ java Avvio
    
    Credo sia giusto?? oppure sbaglio qualcosa??
  • Re: Aiuto java

    giorgiomarte ha scritto:


    Credo sia giusto?? oppure sbaglio qualcosa??
    Devi compilare ANCHE la classe del controller. Siccome non è referenziata staticamente dalla classe Avvio, non si compila in "automatico" quando compili Avvio.java !

    E se la compilassi, vedresti che la classe del controller dà un errore. Non hai importato ActionEvent.

    Quindi, struttura dei file:

    XYZ\Avvio.java
    XYZ\interfaccia\Controller.java
    XYZ\interfaccia\newinterfaccia.fxml

    (la cartella XYZ che cosa/dove sia NON ha importanza).

    Stando come directory corrente in XYZ:

    javac Avvio.java
    javac interfaccia\Controller.java

    quindi

    java Avvio
  • Re: Aiuto java

    Ho importato ActionEvent nel controller nel seguente modo:
    
    package interfaccia;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.TextField;
    import java.awt.event.ActionEvent;
    
    public class Controller {
    
        @FXML
        private TextField bottone1;
    
        @FXML
        private TextField bottone;
    
        @FXML
        void Button(ActionEvent event) {
          bottone.setText(bottone1.getText());
        }
    
    }
    
    poi compilo ed ottengo un altro errore:
    
    MBP-di-giorgio:~ giorgiomartesi$ cd Desktop
    MBP-di-giorgio:Desktop giorgiomartesi$ cd 1
    MBP-di-giorgio:1 giorgiomartesi$ javac Avvio.java
    MBP-di-giorgio:1 giorgiomartesi$ javac interfaccia/Controller.java
    MBP-di-giorgio:1 giorgiomartesi$ java Avvio
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    	at java.base/java.lang.Thread.run(Thread.java:844)
    Caused by: javafx.fxml.LoadException: Error resolving onAction='#Button', either the event handler is not in the Namespace or there is an error in the script.
    /Users/giorgiomartesi/Desktop/1/interfaccia/newinterfaccia.fxml:69
    
    	at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    	at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
    	at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
    	at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
    	at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    	at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    	at Avvio.start(Avvio.java:13)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    	at java.base/java.security.AccessController.doPrivileged(Native Method)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    	at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    Exception running application Avvio
    
    Perche?? è sbagliato qualcosa nel codice??
  • Re: Aiuto java

    giorgiomarte ha scritto:


    import java.awt.event.ActionEvent;
    Hai importato ActionEvent di AWT (che non c'entra niente ... guarda caso ha solo lo stesso nome).

    import javafx.event.ActionEvent;
  • Re: Aiuto java

    OKOK scusa ho sbagliato... comunque sostituendo e eseguendo ottengo questo errore:
    
    MBP-di-giorgio:~ giorgiomartesi$ cd Desktop
    MBP-di-giorgio:Desktop giorgiomartesi$ cd 1
    MBP-di-giorgio:1 giorgiomartesi$ javac Avvio.java
    MBP-di-giorgio:1 giorgiomartesi$ javac interfaccia/Controller.java
    MBP-di-giorgio:1 giorgiomartesi$ java Avvio
    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    	at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
    Caused by: java.lang.RuntimeException: Exception in Application start method
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    	at java.base/java.lang.Thread.run(Thread.java:844)
    Caused by: javafx.fxml.LoadException: Error resolving onAction='#Button', either the event handler is not in the Namespace or there is an error in the script.
    /Users/giorgiomartesi/Desktop/1/interfaccia/newinterfaccia.fxml:69
    
    	at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    	at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
    	at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
    	at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
    	at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    	at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    	at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    	at Avvio.start(Avvio.java:13)
    	at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    	at java.base/java.security.AccessController.doPrivileged(Native Method)
    	at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    	at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    Exception running application Avvio
    
    Non riesco a capire come mai non mi si avvia l'interfaccia ... dove può essere l'errore??
Devi accedere o registrarti per scrivere nel forum
31 risposte