Okna - ramy aplikacji Java FX

0

Hej, ucze się JavyFX i mam taki problem nie potrafie zrobić aby po nacisnieciu guzika pojawilo się drugie okno. ucze się z tuta na youtube ale czlowiek tam uzywa Eclipsa przez co nie co się to różni i średnio potrafię powiedzieć gdzie robię błąd, (chodzi mi dokladnie o to że gdy kilikne "Application", otworzy się nowe okno z jakaś zawartością.) do tworzenia okien i elementów używam SceneBuildera
wrzucam kod:

Main.java:


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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        Scene scene =new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

Controller.java:


import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

import java.io.IOException;

public class Controller {



    public void openApplication(){
        FXMLLoader loader =new FXMLLoader(this.getClass().getResource("sample.menu"));
        Pane pane=new Pane();
        try {
            pane=loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public void Option(){
        System.out.println("mmmmm");
    }
public void exit(){
        System.exit(0);
    }

}```

sample.fxml:
```<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>

<StackPane fx:id="mainStackPane" 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="sample.Controller">

      <Pane fx:id="Pane" prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button fx:id="Application" layoutX="192.0" layoutY="200.0" mnemonicParsing="false" onAction="#openApplication" text="Application">
                     <font>
                        <Font size="31.0" />
                     </font>
                  </Button>
                  <Button fx:id="Settings" layoutX="223.0" layoutY="127.0" mnemonicParsing="false" onAction="#Option" text="Options">
                     <font>
                        <Font size="29.0" />
                     </font>
                  </Button>
                  <Button fx:id="exitApplication" layoutX="252.0" layoutY="56.0" mnemonicParsing="false" onAction="#exit" text="Exit">
                     <font>
                        <Font size="30.0" />
                     </font>
                  </Button>
         </children>
      </Pane>
   
</StackPane>

menuController.java:


import javafx.fxml.FXMLLoader;

public class menuController {
    FXMLLoader loader= new FXMLLoader(this.getClass().getResource("sample.menu"));

}

menu.fxml:


<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>


<Pane 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">
   <children>
      <Button fx:id="Test" layoutX="195.0" layoutY="148.0" mnemonicParsing="false" text="Click">
         <font>
            <Font size="56.0" />
         </font>
      </Button>
   </children>
</Pane>

Po kliknieciu Application wywala mi masę błędów.

Z góry przepraszam że nie wrzucam tego kodu na gita ale uważam że nie potrafię go jeszcze na tyle abym mógł go wam za prezętować tak jak bym tego chciał

0
FXMLLoader loader =new FXMLLoader(this.getClass().getResource("sample.menu"));

Zmień na "menu.fxml"

0
Potat0x napisał(a):
FXMLLoader loader =new FXMLLoader(this.getClass().getResource("sample.menu"));

Zmień na "menu.fxml"

Zmieniłem, wciąż ten sam błąd po nacisnieciu przycisku Application.

	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.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.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:394)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$345(GlassViewEventHandler.java:432)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
	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.gtk.GtkApplication._runLoop(Native Method)
	at com.sun.glass.ui.gtk.GtkApplication.lambda$null$203(GtkApplication.java:139)
	at java.lang.Thread.run(Thread.java:748)
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:498)
	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:498)
	at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
	at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
	... 48 more
Caused by: java.lang.IllegalStateException: Location is not set.
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
	at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
	at sample.Controller.openApplication(Controller.java:20)
	... 58 more
0

Problem w tym, że nie może znaleźć pliku .fxml. Bez znajomości, gdzie on jest trudno pomóc. Druga kwestia to jaki to projekt: Maven czy Gradle?

0
cs napisał(a):

Problem w tym, że nie może znaleźć pliku .fxml. Bez znajomości, gdzie on jest trudno pomóc. Druga kwestia to jaki to projekt: Maven czy Gradle?

jest to projekt mavenowy, nie powinno byc problemu z tym aby go znalazlo specjalnie trzymam pliki w jednym miejscu aby nie bawic sie z podowaniem sciezki.

Zauwazyłem że nie podałem kontrolera w pliku Menu.fxml wiec zrobilem to ale wciaz nic z tego, dalej nie dziala.

screenshot-20190213175440.png

0

No raczej nie wygląda na Maven'a, brak domyślnej struktury, pom.xml itd. Jeśli jednak to Maven to w src\main robisz katalog resources, przenosisz tam pliki fxml a w kodzie odwołujesz się do nich np.:/menu.fxml.

0

Mialeś racje zapomnialem dodac pom ale to i tak nic nie zmienilo. ponieważ teraz stworzyłem całkowicie nowy projekt pusty, dodalem do tego wsparcie dla frameworkow. ppm na katalog glowny i tam support for framework wybralem maven. i co ciekawe do poki nie wybralem maven wszystko sie wyswietlalo zdarzylem go dodac juz nic nie rusza.

screenshot-20190215233046.png
Strasznie mnie to irytuje bo co zasiadam aby co kolwiek napisać zaraz jakieś błędy :/

1

Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));

0

Ok, ok wszystko działa nie ma błędów ale mam problem utknąłem patrzyłem po Necie ale w sumie nic konkretnego, a Ja chcialbym nauczyc się operować tym co mam.
Postawiłem nowy projekt mavenowy. działa. mamy tam od razu ramy dokumentu:

Clasa Main:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/MainScreen.fxml/"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }

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

No i zrobiłem formatke i formatka się wyświetla i w ogóle super:

główne okno(MainScreen.fxml):

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>

<StackPane fx:id="MainStack" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Pane prefHeight="200.0" prefWidth="200.0">
         <children>
            <Button layoutX="147.0" layoutY="63.0" mnemonicParsing="false" onAction="#OpenApp" text="Open">
               <font>
                  <Font size="26.0" />
               </font>
            </Button>
            <Button layoutX="141.0" layoutY="152.0" mnemonicParsing="false" onAction="#Option" text="Option">
               <font>
                  <Font size="25.0" />
               </font>
            </Button>
            <Button layoutX="156.0" layoutY="238.0" mnemonicParsing="false" onAction="#Exit" text="Exit">
               <font>
                  <Font size="27.0" />
               </font>
            </Button>
         </children>
      </Pane>
   </children>
</StackPane>

Przypisuje działania na kliknięcie w MainController:

public class MainController {
    @FXML
    public void OpenApp() {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/MenuScreen.fxml/"));
        Pane pane = null;
        try {
            pane = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        new Scene(pane);
    }
    public void Option(){
        System.out.println("tsebula");
    } 

    public void Exit(){
        System.exit(0);
    }
}

Lipa, przez to że jest tam parent root i z niego zrobiony jest FXMLloader nie potrafie tego ogarnać. próbuje zrobić tak aby pod Open wyświetlało się okno,(Tak wcześniej zrobiłem to ale w zupełnie inny sposób) zależy mi na tym aby nauczyć się posługiwać na tym szablonie, chciałbym to zrozumie.Czy ktoś jest w stanie mi to wytłumaczyć?

1 użytkowników online, w tym zalogowanych: 0, gości: 1