0

Необходимо при нажатии кнопки на форме изменять значение primaryStage.setTitle();

Main.java

package sample;

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("view.fxml"));
        primaryStage.setTitle("Исходная форма");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
        primaryStage.setResizable(false);
    }

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

Controller.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;



public class Controller {
    @FXML
    public Button btnRed = new Button();
    @FXML
    public Button btnYellow = new Button();
    @FXML
    public Button btnGreen = new Button();
    @FXML
    public Pane PaneM = new Pane();

    Tooltip tooltipRed = new Tooltip();
    Tooltip tooltipYellow = new Tooltip();
    Tooltip tooltipGreen = new Tooltip();

    public void tooltipsValues() {
        tooltipRed.setText("RED");
        tooltipGreen.setText("GREEN");
        tooltipYellow.setText("YELLOW");
    }

    public void initialize(){
        btnRed.setTooltip(tooltipRed);
        btnYellow.setTooltip(tooltipYellow);
        btnGreen.setTooltip(tooltipGreen);
    }

    public void onClickRed() {
        PaneM.setStyle("-fx-background-color: #7e1300");
        btnRed.setDisable(true);
        btnGreen.setDisable(true);
        btnYellow.setDisable(false);
    }

    public void onClickYellow(){
        PaneM.setStyle("-fx-background-color: #7e7d10");
        btnYellow.setDisable(true);
        btnRed.setDisable(true);
        btnGreen.setDisable(false);
    }

    public void onClickGreen(){
        PaneM.setStyle("-fx-background-color: #2d8e26");
        btnGreen.setDisable(true);
        btnYellow.setDisable(true);
        btnRed.setDisable(false);
    }
}

1 ответ 1

0

Вы должны передать в контроллер ссылку на stage и потом можно будет поменять заголовок.

public void start(Stage primaryStage) throws Exception{
   FXMLLoader loader = new FXMLLoader(getClass().getResource("/view.fxml"));
   Parent root = loader.load();
   // получить ссылку на контроллер
   Controller controller = loader.getController();
   // передать stage в контроллер
   controller.setStage(primaryStage);

   primaryStage.setTitle("Исходная форма");
   primaryStage.setScene(new Scene(root));
   primaryStage.show();
   primaryStage.setResizable(false);
}

Кусок контроллера

private Stage stage;

public void setStage(Stage stage) {
    this.stage = stage;
}

public void onClickRed() {
    paneM.setStyle("-fx-background-color: #7e1300");
    btnRed.setDisable(true);
    btnGreen.setDisable(true);
    btnYellow.setDisable(false);
    // меняем заголовок окна
    stage.setTitle("RED");
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.