したがって、私はBorderPaneを別のBorderPane(中央部)の中に配置しようとしていましたが、これまでのところ成功していません。FXML BorderPaneは別のBorderPaneの中央に配置されています
は、私が試してみた(両方FXMLと親シーンからロードペインで):
SetPadding
setMargin
- 使用
AnchorPane
と上、下、左と右のアンカー を設定
Regions
をメインのBorderPaneの左上と左端に追加すると(無効になり、ステージのサイズ変更時には無効になります)
私はのHBox、VBoxの、AnchorPane、またはグループのよう
Pane pane = loader.load();
FXMLをロードするためにペインまたはノード以外のものを使用している場合はFXMLはロードされません。
現在のレイアウト:私が探している
結果(約)
アイデアはFXML、私はウィンドウのサイズを変更してもですレイアウトはMainBorderPaneの中心にとどまります。
レッドボーダー:ステージ - >シーン - > BorderPane ブルーボーダー:メソッドでFXMLを使ってロードBorderPane showNewScene(FXMLPath)
シミュレーションのための(ソースコード:パッケージサンプル )
Main.java
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Main extends Application {
private static BorderPane mainBorderPane;
private static Scene mainScene;
private static MenuBar mainMenuBar;
private static Stage mainStage;
private static Menu mainMenuFile;
private static MenuItem mainItemMenuLock;
private static MenuItem mainItemMenuClose;
private static Menu mainMenuHelp;
private static MenuItem mainItemMenuSupport;
public static BorderPane getMainBorderPane() {
if (mainBorderPane == null) {
mainBorderPane = new BorderPane();
}
return mainBorderPane;
}
public static Scene getMainScene() {
if (mainScene == null) {
mainScene = new Scene(getMainBorderPane(), 800, 600);
}
return mainScene;
}
public static MenuBar getMainMenuBar() {
if (mainMenuBar == null) {
mainMenuBar = new MenuBar();
mainMenuFile = new Menu("File");
mainItemMenuLock = new MenuItem("Lock Screen");
mainItemMenuClose = new MenuItem("Close");
mainMenuFile.getItems().addAll(mainItemMenuLock, mainItemMenuClose);
mainMenuHelp = new Menu("Help");
mainItemMenuSupport = new MenuItem("Support");
mainMenuHelp.getItems().addAll(mainItemMenuSupport);
mainMenuBar.getMenus().addAll(mainMenuFile, mainMenuHelp);
}
return mainMenuBar;
}
public static Stage getMainStage() {
if (mainStage == null) {
getMainBorderPane().setTop(getMainMenuBar());
mainStage = new Stage(StageStyle.DECORATED);
}
return mainStage;
}
public static void main(String[] args) {
launch(args);
}
@Override
public void init() {
//
}
@Override
public void start(final Stage initStage) throws Exception{
UtilMethods.showNewScene("login.fxml");
}
}
UtilMethods.java
package sample;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.layout.Pane;
public class UtilMethods {
public static void showNewScene(String fxmlPath) {
try {
FXMLLoader loader = new FXMLLoader(UtilMethods.class.getResource(fxmlPath));
Pane pane = loader.load();
Main.getMainBorderPane().setCenter(pane);
Main.getMainBorderPane().setAlignment(pane, Pos.CENTER);
Main.getMainStage().setScene(Main.getMainScene());
Main.getMainStage().setAlwaysOnTop(false);
Main.getMainStage().setResizable(true);
Main.getMainStage().show();
}catch (java.io.IOException e){
System.out.println("Error loading screen" + e.getMessage());
}
}
}
LoginController.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class LoginController {
@FXML private TextField userIdField;
@FXML private PasswordField passwordField;
public void initialize(){
//
}
@FXML
private void login(ActionEvent event) {
System.out.println("login");
}
@FXML
private void cancel(){
userIdField.clear();
passwordField.clear();
userIdField.requestFocus();
}
@FXML
private void registerNew(ActionEvent event) throws Exception{
System.out.println("register new");
}
}
login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" hgap="10" vgap="10"
fx:controller="sample.LoginController" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
<children>
<Label text="Username:" GridPane.columnIndex="0"
GridPane.rowIndex="0" GridPane.halignment="RIGHT" />
<Label text="Password:" GridPane.columnIndex="0"
GridPane.rowIndex="1" GridPane.halignment="RIGHT" />
<Label text="Database connection status:" GridPane.columnIndex="0"
GridPane.rowIndex="2" GridPane.halignment="RIGHT" />
<Label fx:id="labelDBStatus" text="..." GridPane.columnIndex="1"
GridPane.rowIndex="2" GridPane.halignment="RIGHT" />
<TextField fx:id="userIdField" GridPane.columnIndex="1" GridPane.rowIndex="0"
promptText="User ID" styleClass="text-field"/>
<PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="1"
promptText="Password" styleClass="text-field"/>
<HBox GridPane.columnIndex="0" GridPane.rowIndex="3"
GridPane.columnSpan="2" alignment="CENTER" spacing="10">
<children>
<Button fx:id="btnLogin" text="Login" onAction="#login" />
<Button fx:id="btnCancel" text="Cancel" onAction="#cancel"/>
<Button fx:id="btnRegister" text="Register" onAction="#registerNew"/>
</children>
</HBox>
</children>
</GridPane>
なぜあなたは 'StackPane'を使用しませんか? –
FXMLLoaderから例外が発生しました: スレッド "JavaFX Application Thread"の例外java.lang.ClassCastException:javafx.scene.layout.GridPaneをjavafx.scene.layout.StackPaneにキャストできません - 私の控えめなことは私のレイアウトですlogin.fxmlはGridPaneであり、GridPaneとStackPaneの両方がjavafx.scene.layout.Pane、 – SouthernGus
から継承されています。「StackPaneを使用」@BoHalimは、 'BorderPane'の代わりに' StackPane'を使用することを意味します。 FXMLの 'GridPane'ルート要素に' alignment = "CENTER"属性を追加すると、あなたが望むことができますか? –