1
votes

JavaFX: déplacer l'élément lors du redimensionnement de la fenêtre (étape)

J'ai une application simple sur JavaFX, elle se compose en fait de plusieurs volets et boutons. Je n'arrive pas à comprendre comment faire bouger un bouton lorsque la scène est redimensionnée. Donc, je veux que le bouton inférieur gauche soit toujours visible même lors du redimensionnement de la fenêtre. entrez la description de l'image ici

voici un aperçu des nœuds:

entrez la description de l'image ici

voici fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.geometry.Point3D?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="750.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <BorderPane layoutX="89.0" layoutY="64.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <left>
            <AnchorPane prefHeight="150.0" prefWidth="150.0" style="-fx-background-color: #4059a9 #4059a9;" BorderPane.alignment="CENTER">
               <children>
                  <AnchorPane prefHeight="750.0" prefWidth="75.0" style="-fx-background-color: #2b4496 #2b4496;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="75.0" AnchorPane.topAnchor="0.0">
                     <children>
                        <VBox alignment="CENTER" prefHeight="750.0" prefWidth="75.0" spacing="35.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                           <children>
                              <JFXButton contentDisplay="GRAPHIC_ONLY" prefWidth="82.0" VBox.vgrow="ALWAYS">
                                 <graphic>
                                    <FontAwesomeIconView fill="WHITE" glyphName="HOME" size="2em" />
                                 </graphic>
                                 <VBox.margin>
                                    <Insets top="25.0" />
                                 </VBox.margin>
                              </JFXButton>
                              <JFXButton contentDisplay="GRAPHIC_ONLY" prefWidth="75.0">
                                 <VBox.margin>
                                    <Insets bottom="10.0" top="600.0" />
                                 </VBox.margin>
                                 <graphic>
                                    <FontAwesomeIconView fill="WHITE" glyphName="SIGN_OUT" size="2em" />
                                 </graphic>
                                 <opaqueInsets>
                                    <Insets />
                                 </opaqueInsets>
                              </JFXButton>
                           </children>
                        </VBox>
                     </children>
                     <rotationAxis>
                        <Point3D />
                     </rotationAxis>
                  </AnchorPane>
               </children>
            </AnchorPane>
         </left>
      </BorderPane>
   </children>
</AnchorPane>


8 commentaires

Solution de force brute: calculez le nouvel emplacement du bouton à l'intérieur de l'événement de redimensionnement de la fenêtre en utilisant la nouvelle taille de la fenêtre.


@nicomp oui, cela peut aider. Je pensais qu'il y avait une solution plus élégante ...


Publiez votre FXML. N'essayez pas l'idée de @nicomp.


Je suppose que vous définissez la hauteur minimale sur un nœud enfant.


@Sedrick a ajouté le fxml


@nicomp Les API de l'interface graphique JavaFX sont de haut niveau et sont conçues pour gérer la plupart des situations si vous utilisez correctement les nœuds de disposition.


Vous allez probablement rencontrer plus de problèmes avec votre mise en page actuelle. Si la largeur de l'écran change (peut-être), si la résolution de l'écran change, et si la taille de la police change (peut-être).


Tous ces AnchorPane sont-ils uniquement là pour générer ces formes rectangulaires? il me semble que vous pourriez le faire avec un BorderPane + VBox (boutons compris) uniquement ...


3 Réponses :


3
votes

Votre problème, ce sont les marges que vous définissez sur vos boutons . Suivez cette mise en page. J'ai utilisé HBox comme nœud racine.

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox alignment="CENTER" prefWidth="75.0" style="-fx-background-color: #2b4496 #2b4496;">
         <children>
            <JFXButton contentDisplay="GRAPHIC_ONLY" prefWidth="82.0" VBox.vgrow="ALWAYS">
               <graphic>
                  <FontAwesomeIconView fill="WHITE" glyphName="HOME" size="2em" />
               </graphic>
               <VBox.margin>
                  <Insets top="25.0" />
               </VBox.margin>
            </JFXButton>
            <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" opacity="0.0" VBox.vgrow="ALWAYS" />
            <JFXButton contentDisplay="GRAPHIC_ONLY" prefWidth="75.0">
               <VBox.margin>
                  <Insets bottom="25.0" />
               </VBox.margin>
               <graphic>
                  <FontAwesomeIconView fill="WHITE" glyphName="SIGN_OUT" size="2em" />
               </graphic>
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
            </JFXButton>
         </children>
      </VBox>
      <VBox prefHeight="200.0" prefWidth="75.0" style="-fx-background-color: #4059a9 #4059a9;" />
      <AnchorPane prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS" />
   </children>
</HBox>

 entrez la description de l'image ici

 entrez la description de l'image ici


0 commentaires

3
votes
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <left>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <Button mnemonicParsing="false" text="Button" />
            <AnchorPane VBox.vgrow="ALWAYS" />
            <Button mnemonicParsing="false" text="Button" />
         </children>
      </VBox>
   </left>
</BorderPane>
Add an empty pane into the middle with VGROW set to ALWAYS.

0 commentaires

3
votes

On dirait que vous pourriez obtenir le même effet en utilisant un BorderPane et une VBox avec des remplissages d'arrière-plan superposés et une Région qui grandit toujours insérée entre vos boutons:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.Region?>

<BorderPane prefHeight="750.0" prefWidth="1000.0"
    xmlns="http://javafx.com/javafx/9.0.4"
    xmlns:fx="http://javafx.com/fxml/1">
    <left>
        <VBox alignment="TOP_LEFT"
            spacing="35.0"
            style="-fx-background-color: #2b4496, #4059a9; -fx-background-insets: 0, 0 0 0 75;"> <!-- overlay 2 backgrounds produce rectangles -->
            <padding>
                <Insets right="75.0" />
            </padding>
            <children>
                <JFXButton contentDisplay="GRAPHIC_ONLY" prefWidth="82.0"  VBox.vgrow="NEVER">
                    <graphic>
                        <FontAwesomeIconView fill="WHITE" glyphName="HOME" size="2em" />
                     </graphic>
                     <VBox.margin>
                        <Insets top="25.0" />
                     </VBox.margin>
                  </JFXButton>
                  <Region VBox.vgrow="ALWAYS" /> <!-- filler -->
                  <JFXButton contentDisplay="GRAPHIC_ONLY" prefWidth="75.0" VBox.vgrow="NEVER">
                     <VBox.margin>
                        <Insets bottom="10.0" />
                     </VBox.margin>
                     <graphic>
                        <FontAwesomeIconView fill="WHITE" glyphName="SIGN_OUT" size="2em" />
                     </graphic>
                 </JFXButton>
            </children>
        </VBox>
    </left>
</BorderPane>


1 commentaires

Merci beaucoup, c'est une approche plus élégante