Search

Scene

Scene

Stage ์œ„์— ํ‘œ์‹œ๋˜๋Š” ์ปจํ…์ธ  ์˜์—ญ(ํ™”๋ฉด)

๊ตฌ์„ฑ์š”์†Œ

โ€ข
์ปจํŠธ๋กค
โ€ข
๋ ˆ์ด์•„์›ƒ
โ€ข
๊ทธ๋ž˜ํ”ฝ
UI ์š”์†Œ๋“ค์ด ๋ฐฐ์น˜๋  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์ž…๋‹ˆ๋‹ค.

Scene ์ƒ์„ฑํ•˜๊ธฐ

โ€ข
๊ธฐ๋ณธ ์ƒ์„ฑ : ๋ ˆ์ด์•„์›ƒ์„ ์ง€์ •ํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.
Scene scene = new Scene(๋ ˆ์ด์•„์›ƒ);
Java
๋ณต์‚ฌ
โ€ข
ํฌ๊ธฐ ์ง€์ • : ๊ฐ€๋กœํฌ๊ธฐ, ์„ธ๋กœํฌ๊ธฐ๋ฅผ px ๋‹จ์œ„๋กœ ์ง€์ •ํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.
Scene scene = new Scene(๋ ˆ์ด์•„์›ƒ, ๊ฐ€๋กœํฌ๊ธฐ, ์„ธ๋กœํฌ๊ธฐ);
Java
๋ณต์‚ฌ
โ€ข
๋ฐฐ๊ฒฝ์ƒ‰ ์ง€์ • : ์ƒ‰์ƒ ์ง€์ •ํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.
Scene scene = new Scene(๋ ˆ์ด์•„์›ƒ, ์ปฌ๋Ÿฌ);
Java
๋ณต์‚ฌ

ํ™”๋ฉด ์ด๋™ํ•˜๊ธฐ

ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด์€ Scene ๋‹จ์œ„๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. ํ™”๋ฉด ์ด๋™์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด, Stage์— ๋“ฑ๋ก๋œ SceneA SceneB ๋กœ ์žฌ์ง€์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class SceneSwitchingExample extends Application { private Stage primaryStage; // ์ฃผ ์ฐฝ์„ ๋‚˜ํƒ€๋‚ด๋Š” Stage ๊ฐ์ฒด private Scene sceneA, sceneB; // SceneA์™€ SceneB๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Scene ๊ฐ์ฒด public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; // SceneA ์ƒ์„ฑ Button switchButton = new Button("Go to Scene B"); switchButton.setOnAction(e -> primaryStage.setScene(sceneB)); // SceneB๋กœ ์ „ํ™˜ํ•˜๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์„ค์ • StackPane layoutA = new StackPane(switchButton); // ๋ฒ„ํŠผ์„ ํฌํ•จํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ ์ƒ์„ฑ sceneA = new Scene(layoutA, 300, 200); // SceneA ์ƒ์„ฑ // SceneB ์ƒ์„ฑ Button backButton = new Button("Go back to Scene A"); backButton.setOnAction(e -> primaryStage.setScene(sceneA)); // SceneA๋กœ ์ „ํ™˜ํ•˜๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์„ค์ • StackPane layoutB = new StackPane(backButton); // ๋ฒ„ํŠผ์„ ํฌํ•จํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ ์ƒ์„ฑ sceneB = new Scene(layoutB, 300, 200); // SceneB ์ƒ์„ฑ // ์ดˆ๊ธฐ Scene ์„ค์ • (์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋  ๋•Œ๋Š” SceneA๋ฅผ ๋ณด์—ฌ์คŒ) primaryStage.setScene(sceneA); primaryStage.setTitle("Scene Switching Example"); // ์œˆ๋„์šฐ ํƒ€์ดํ‹€ ์„ค์ • primaryStage.show(); // ์ฃผ ์ฐฝ์„ ํ™”๋ฉด์— ํ‘œ์‹œ } }
Java
๋ณต์‚ฌ

FXML ๋กœ Scene (ํ™”๋ฉด) ์ง€์ •ํ•˜๊ธฐ

1.
Main.fxml ํŒŒ์ผ ์ƒ์„ฑ
2.
Java ์ฝ”๋“œ์—์„œ โ€œMain.fxmlโ€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
3.
๋ถˆ๋Ÿฌ์˜จ fxml ํŒŒ์ผ ๋ ˆ์ด์•„์›ƒ์— Scene ๋“ฑ๋ก

Main.fxml ํŒŒ์ผ ์ƒ์„ฑ

Main.fxml

SceneBuilder ๋กœ UI ๋””์ž์ธ
<?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.text.Font?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SceneController"> <children> <Button layoutX="156.0" layoutY="176.0" mnemonicParsing="false" text="๋ฒ„ํŠผ"> <font> <Font name="Consolas" size="27.0" /> </font> </Button> <Label layoutX="213.0" layoutY="35.0" text="๋ฉ”์ธ ํ™”๋ฉด"> <font> <Font name="Consolas" size="45.0" /> </font> </Label> </children> </AnchorPane>
XML
๋ณต์‚ฌ

Java ์ฝ”๋“œ์—์„œ โ€œMain.fxmlโ€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

Main.java

Parent root = FXMLLoader.load(getClass().getResource("Main.fxml");
Java
๋ณต์‚ฌ

๋ถˆ๋Ÿฌ์˜จ fxml ํŒŒ์ผ ๋ ˆ์ด์•„์›ƒ์— Scene ๋“ฑ๋ก

Scene scene = new Scene(root);
Java
๋ณต์‚ฌ