请对我的代码有问题,我正在尝试在 javaFx 中构建一个媒体播放器应用程序,并正在使用BorderPane
将子节点分别布局到顶部、中心和底部,一切似乎都很好,但是当我设置中心节点时,底部节点消失了。
我也有一个 CSS 文件,但我不认为它是问题的一部分,除非我错了。
请,任何人都可以帮助解决如何将子节点分别布局到顶部、中心和按钮。
注意:如果我注释掉
root.setCenter(new Main().mediaView(view));
行,顶部和底部节点会很好。
谢谢。
这是我的代码:
package application;
import java.io.File;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
new Main().getPane(primaryStage);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public void getPane(Stage stage) {
Media media = new Media(new File("src/The_Nut_Job.mp4").toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView view = new MediaView(mediaPlayer);
mediaPlayer.setOnReady(new Runnable() {
@Override
public void run() {
double width = mediaPlayer.getMedia().getWidth();
double height = mediaPlayer.getMedia().getHeight();
System.out.println(width +", "+height);
BorderPane root = new BorderPane();
root.setTop(new Main().titleBar());
root.setBottom(new Main().toolBar());
//BorderPane.setAlignment(new Main().mediaView(view), Pos.CENTER);
root.setCenter(new Main().mediaView(view));
Scene scene = new Scene(root, width, height);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.show();
mediaPlayer.play();
}
});
}
public StackPane mediaView(MediaView mView) {
StackPane view = new StackPane();
view.getChildren().add(mView);
view.setId("view");
return view;
}
public HBox titleBar() {
HBox titleBar = new HBox();
titleBar.setMinHeight(35);
titleBar.setMaxHeight(35);
titleBar.setId("title");
return titleBar;
}
public HBox toolBar() {
HBox toolBar = new HBox();
toolBar.setMinHeight(35);
toolBar.setMaxHeight(35);
toolBar.setId("tool");
return toolBar;
}
}
这是我的 CSS:
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
.root {
-fx-border-color: #e6e6fa;
-fx-border-radius: 8 8 0 0;
-fx-border-width: 1;
-fx-background-radius: 8 8 0 0;
-fx-background-color: transparent;
}
#title {
-fx-background-color: #222;
-fx-border-color: transparent;
-fx-border-radius: 8 8 0 0;
-fx-background-radius: 8 8 0 0;
}
#tool {
-fx-background-color: #222;
-fx-border-color: white;
-fx-border-radius: 0 0 8 8;
-fx-background-radius: 0 0 8 8;
}
#view {
-fx-background-color: transparent;
-fx-border-radius: 0 0 8 8;
-fx-background-radius: 0 0 8 8;
}