A
A
Alexander2017-04-06 09:15:01
Java
Alexander, 2017-04-06 09:15:01

Button initialization in JavaFX?

Good afternoon. Tell me why the button does not work for me. When pressed, it should change the Scene, but for some reason this does not happen. I watched a whole bunch of training videos and read as many articles. Here is the code.
Main scene FXML:

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

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

<Pane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
   <children>
      <Button fx:id="btngsc" layoutX="14.0" layoutY="60.0" mnemonicParsing="false" prefHeight="65.0" prefWidth="223.0" style="-fx-background-radius: 40; -fx-background-color: #778899;" text="Получить сим-карту">
         <font>
            <Font name="Arial Italic" size="19.0" />
         </font>
      </Button>
      <Button fx:id="help" layoutX="14.0" layoutY="139.0" mnemonicParsing="false" prefHeight="65.0" prefWidth="223.0" style="-fx-background-radius: 40; -fx-background-color: #778899;" text="Помогите!!!">
         <font>
            <Font name="Arial Italic" size="19.0" />
         </font>
      </Button>
      <Label alignment="CENTER" layoutX="28.0" layoutY="14.0" prefHeight="32.0" prefWidth="745.0" text="Добрый день! Если вы хотите получить сим-карту, нажмите на кнопку &quot;Получить сим-карту&quot;.">
         <font>
            <Font name="Arial Italic" size="16.0" />
         </font>
      </Label>
      <Rectangle arcHeight="5.0" arcWidth="5.0" fill="#ffffff00" height="543.0" layoutX="249.0" layoutY="43.0" stroke="BLACK" strokeType="INSIDE" width="518.0" />
   </children>
</Pane>

FXML Scenes to go to:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.media.MediaView?>
<?import javafx.scene.text.Font?>


<Pane fx:id="gsc" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
   <children>
      <AnchorPane fx:id="root1" maxHeight="600.0" maxWidth="800.0" minHeight="600.0" minWidth="800.0" prefHeight="600.0" prefWidth="800.0">
         <children>
            <Label layoutX="386.0" layoutY="14.0" text="Для продолжения посмотрите обучающий видеоролик. Если вы и так все знаете можно пропустить этот пункт и перейти к получению сим-карты, для этого нажмите кнопку: &quot;Далее&quot;." textAlignment="CENTER" wrapText="true" AnchorPane.leftAnchor="16.0" AnchorPane.rightAnchor="16.0" AnchorPane.topAnchor="14.0">
               <font>
                  <Font name="Arial Italic" size="16.0" />
               </font>
            </Label>
            <MediaView fitHeight="450.0" fitWidth="450.0" layoutX="175.0" layoutY="75.0" AnchorPane.topAnchor="75.0" />
            <Button fx:id="nextbtngsc" layoutX="514.0" layoutY="525.0" maxHeight="65.0" maxWidth="223.0" minHeight="65.0" minWidth="223.0" mnemonicParsing="false" prefHeight="65.0" prefWidth="223.0" style="-fx-background-color: #778899; -fx-background-radius: 40;" text="Далее">
               <font>
                  <Font name="Arial Italic" size="19.0" />
               </font>
            </Button>
            <Button fx:id="backngsc" layoutX="64.0" layoutY="525.0" maxHeight="65.0" maxWidth="223.0" minHeight="65.0" minWidth="223.0" mnemonicParsing="false" prefHeight="65.0" prefWidth="223.0" style="-fx-background-color: #778899; -fx-background-radius: 40;" text="Назад">
               <font>
                  <Font size="19.0" />
               </font>
            </Button>
         </children>
      </AnchorPane>
   </children>
</Pane>

Main program class:
package sample;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;


public class Main extends Application { // главный класс.

    private Stage stage = null;
    private BorderPane sukapane;
    private Scene scene, gscScene;

    @FXML
    private Button btngsc;
    @FXML
    private Pane root;
    @FXML
    private Pane gsc;

    public void start(Stage stage) throws Exception{ //Создаем Stage
        this.stage = stage;
        stage.setTitle("PromskyAppTerminal");
        //Вызываем методы инициализации Scene
        initSample();
        showPersonOverview();
    }

    private void initSample() {//Инициализация пустой сцены(как-бы оболочка)

        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("suka.fxml"));
            sukapane = (BorderPane) loader.load();
            scene = new Scene(sukapane, 800, 600);
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void showPersonOverview() {// Инициализация главного окна

        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("sample.fxml"));
            root = (Pane) loader.load();
            sukapane.setCenter(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    public void initialize(){//работаем с кнопкой(на данный момент с одной)

        btngsc.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                FXMLLoader loader1 = new FXMLLoader();
                loader1.setLocation(getClass().getResource("getsimcard.fxml"));
                try {
                    gsc = (Pane) loader1.load();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                gscScene = new Scene(gsc, 800, 600);
                stage = null;
                stage.setScene(gscScene);

            }
        });

    }

    public Stage getStage() {
        return stage;
    }

    public static void main(String[] args) { // Здесь пишется вся логика.

        launch(args); // Запуск программы.

    }

}

Guys, I really hope for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2017-04-06
@jamakasi666

What a shame.
1) For starters, you do not need to combine controllers with each other, and even with the class in which main ().
2) For each fxml, make your own separate controller, at least there will be no porridge.
3) In the Main class, leave only the main and start functions. Make the stage variable public and static.
4) The event of pressing the button for simplicity, you can not write with pens and also declare directly in fxml the name of the function that it will call.
5) in creation you call initSample() followed by showPersonOverview(); , you can not do it this way. Change the scene only through stage.setScene(scene);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question