결과
프로젝트 다운로드
프로젝트 구조
소스 코드
ColorChangeCallback.java
package com.ozxexe.javafxnavigationdrawer;
public interface ColorChangeCallback {
void updateColor(String newColor);
}
ContentsFXML.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXHamburger?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" fx:id="root" prefHeight="360.0" prefWidth="504.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ozxexe.javafxnavigationdrawer.ContentsFXMLController">
<children>
<JFXDrawer fx:id="drawer" defaultDrawerSize="200.0" layoutY="-6.0" prefHeight="368.0" prefWidth="108.0" />
<JFXHamburger fx:id="hamburger" layoutX="460.0" layoutY="14.0" />
</children>
</AnchorPane>
ContentsFXMLController.java
package com.ozxexe.javafxnavigationdrawer;
import com.jfoenix.controls.JFXDrawer;
import com.jfoenix.controls.JFXHamburger;
import com.jfoenix.transitions.hamburger.HamburgerBackArrowBasicTransition;
import com.ozxexe.javafxnavigationdrawer.panel.SidePanelController;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.FadeTransition;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
public class ContentsFXMLController implements Initializable, ColorChangeCallback {
@FXML
private JFXDrawer drawer;
@FXML
private JFXHamburger hamburger;
@FXML
private AnchorPane root;
@Override
public void initialize(URL url, ResourceBundle rb) {
if (!Launcher.isSplashLoaded) {
loadSplashScreen();
}
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/ozxexe/javafxnavigationdrawer/panel/sidepanel.fxml"));
VBox box = loader.load();
SidePanelController controller = loader.getController();
controller.setCallback(this);
drawer.setSidePane(box);
} catch (IOException ex) {
Logger.getLogger(ContentsFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(hamburger);
transition.setRate(-1);
hamburger.addEventHandler(MouseEvent.MOUSE_PRESSED, (e) -> {
transition.setRate(transition.getRate() * -1);
transition.play();
if (drawer.isOpened()) {
drawer.close();
} else {
drawer.open();
}
});
}
private void loadSplashScreen() {
try {
Launcher.isSplashLoaded = true;
StackPane pane = FXMLLoader.load(getClass().getResource(("/com/ozxexe/javafxnavigationdrawer/splash/splash.fxml")));
root.getChildren().setAll(pane);
FadeTransition fadeIn = new FadeTransition(Duration.seconds(3), pane);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeIn.setCycleCount(1);
FadeTransition fadeOut = new FadeTransition(Duration.seconds(3), pane);
fadeOut.setFromValue(1);
fadeOut.setToValue(0);
fadeOut.setCycleCount(1);
fadeIn.play();
fadeIn.setOnFinished((e) -> {
fadeOut.play();
});
fadeOut.setOnFinished((e) -> {
try {
AnchorPane parentContent = FXMLLoader.load(getClass().getResource(("/com/ozxexe/javafxnavigationdrawer/ContentsFXML.fxml")));
root.getChildren().setAll(parentContent);
} catch (IOException ex) {
Logger.getLogger(ContentsFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
});
} catch (IOException ex) {
Logger.getLogger(ContentsFXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void updateColor(String newColor) {
root.setStyle("-fx-background-color:" + newColor);
}
}
Launcher.java
package com.ozxexe.javafxnavigationdrawer;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Launcher extends Application {
public static Boolean isSplashLoaded = false;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/com/ozxexe/javafxnavigationdrawer/ContentsFXML.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Genuine Coder");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
SidePanelController.java
package com.ozxexe.javafxnavigationdrawer.panel;
import com.jfoenix.controls.JFXButton;
import com.ozxexe.javafxnavigationdrawer.ColorChangeCallback;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
public class SidePanelController implements Initializable {
@FXML
private JFXButton b1;
@FXML
private JFXButton b2;
@FXML
private JFXButton b3;
@FXML
private JFXButton exit;
private ColorChangeCallback callback;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void setCallback(ColorChangeCallback callback) {
this.callback = callback;
}
@FXML
private void changeColor(ActionEvent event) {
JFXButton btn = (JFXButton) event.getSource();
System.out.println(btn.getText());
switch (btn.getText()) {
case "Color 1":
callback.updateColor("#00FF00");
break;
case "Color 2":
callback.updateColor("#0000FF");
break;
case "Color 3":
callback.updateColor("#FF0000");
break;
}
}
@FXML
private void exit(ActionEvent event) {
System.exit(0);
}
}
sidepanel.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="400.0" prefWidth="178.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ozxexe.javafxnavigationdrawer.panel.SidePanelController">
<children>
<ImageView fitHeight="127.0" fitWidth="500.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/back.jpg" />
</image>
</ImageView>
<JFXButton fx:id="b1" buttonType="RAISED" onAction="#changeColor" prefHeight="100.0" prefWidth="500.0" ripplerFill="#fc0000" style="-fx-background-color: #f1f1f1;" text="Color 1" />
<JFXButton fx:id="b2" buttonType="RAISED" layoutX="10.0" layoutY="123.0" onAction="#changeColor" prefHeight="100.0" prefWidth="500.0" ripplerFill="#fc0000" style="-fx-background-color: #f1f1f1;" text="Color 2" />
<JFXButton fx:id="b3" buttonType="RAISED" layoutX="10.0" layoutY="197.0" onAction="#changeColor" prefHeight="100.0" prefWidth="500.0" ripplerFill="#fc0000" style="-fx-background-color: #f1f1f1;" text="Color 3" />
<JFXButton fx:id="exit" buttonType="RAISED" layoutX="10.0" layoutY="271.0" onAction="#exit" prefHeight="100.0" prefWidth="500.0" ripplerFill="#fc0000" style="-fx-background-color: #f1f1f1;" text="EXIT" />
</children>
</VBox>
SplashController.java
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<StackPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="360.0" prefWidth="508.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ozxexe.javafxnavigationdrawer.splash.SplashController">
<children>
<ImageView fitHeight="370.0" fitWidth="508.0" pickOnBounds="true">
<image>
<Image url="@../res/back.jpg" />
</image>
</ImageView>
<Pane opacity="0.45" prefHeight="352.0" prefWidth="522.0" style="-fx-background-color: #000000;" />
<Text fill="WHITE" strokeType="OUTSIDE" strokeWidth="0.0" text="WELCOME">
<font>
<Font name="Roboto Medium" size="56.0" />
</font>
</Text>
</children>
</StackPane>
참고 사이트
'Java | JavaFX' 카테고리의 다른 글
JavaFX 폰트 적용하기 (0) | 2019.12.25 |
---|---|
JavaFX 애플리케이션(프로그램) 아이콘 설정하기 (0) | 2019.12.25 |
JavaFX 로그인, 회원가입 줌아웃, 줌인 화면효과주기 예제 (0) | 2019.12.25 |
넷빈즈 프로젝트 외부 라이브러리(jar) 등록하는 방법 (0) | 2019.12.25 |
JavaFX 대쉬보드 디자인 예제 (0) | 2019.12.23 |