我正在使用JavaFX和Scene Builder编写音乐播放器。 我的问题是,如果我调整程序窗口的大小,UI就不会随之增长。如何使UI响应?
在我添加SplashScreen之前,它运行良好。 从那时起,我试图让它发挥作用,但我无法在网上找到任何解决方案!
请提前帮助我!
主要:
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 {
public static boolean isSplashLoaded = false;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Juggle v0.1");
primaryStage.setScene(new Scene(root, 1024, 640));
primaryStage.setResizable(true);
primaryStage.show();
}
}
控制器:
package sample;
import javafx.animation.FadeTransition;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
public MenuItem openFile;
public BorderPane parent;
public void openFile(){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File...");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Audio Files", "*.wav", "*.mp3"));
File f = fileChooser.showOpenDialog(parent.getScene().getWindow());
if ( f != null) {
Media pick = new Media(f.toURI().toString());
MediaPlayer player = new MediaPlayer(pick);
MediaView view = new MediaView(player);
parent.getChildren().add(view);
player.play();
}
else{
}
}
public void loadSplashScreen() throws IOException {
Main.isSplashLoaded = true;
BorderPane pane = FXMLLoader.load(getClass().getResource("Splash.fxml"));
parent.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 {
BorderPane parentContent = FXMLLoader.load(getClass().getResource("sample.fxml"));
parent.getChildren().setAll(parentContent);
} catch (IOException e1) {
e1.printStackTrace();
}
});
}
@Override
public void initialize(URL location, ResourceBundle resources) {
if(!Main.isSplashLoaded){
try {
if(!Main.isSplashLoaded){
loadSplashScreen();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane BorderPane.alignment="CENTER" fx:id="parent"
maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="640.0" prefWidth="1024.0"
style="-fx-background-color: #93F979;"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<bottom>
<BorderPane prefHeight="93.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<top>
<HBox alignment="TOP_CENTER" prefHeight="32.0" prefWidth="800.0" BorderPane.alignment="TOP_CENTER">
<children>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="50.0" text="00:00" textAlignment="CENTER" />
<ProgressBar prefHeight="10.0" prefWidth="900.0" progress="0.0">
<HBox.margin>
<Insets top="4.0" />
</HBox.margin></ProgressBar>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="50.0" text="00:00" textAlignment="CENTER" />
</children>
</HBox>
</top>
</BorderPane>
</bottom>
<center>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true"
BorderPane.alignment="CENTER">
<image>
<Image url="@../Music.png" />
</image>
</ImageView>
</center>
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="openFile" mnemonicParsing="false" onAction="#openFile" text="Open..." />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
Splash FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="1024.0" style="-fx-background-color: #93F979;" BorderPane.alignment="CENTER" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Main">
<center>
<ImageView fitHeight="150.0" fitWidth="200.0" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
<image>
<Image url="@../Splash.png" />
</image>
</ImageView>
</center>
</BorderPane>
答案 0 :(得分:-1)
您是否尝试使用VBox?