有没有人知道如何配置JavaFX titledPane,以便扩展始终在前台进行?与扩展ComboBox按钮的情况一样。 当包含titledPane的面板区域小于膨胀区域时,膨胀发生在侵入区域后面。如何在入侵地区面前做到这一点。 示例代码显示了此方案。我希望的功能可以通过使用包含按钮的组合框按钮来实现,但我喜欢在titledPane扩展中创建的效果,并希望交替使用它。 提前谢谢。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
TitledPane titledPane = new TitledPane();
titledPane.setPrefWidth(180);
titledPane.setBorder(new Border(new BorderStroke(Color.MAGENTA, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
Button btnSave = new Button();
btnSave.setPrefWidth(180);
btnSave.setText("Save");
btnSave.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnLeftSend = new Button();
btnLeftSend.setPrefWidth(180);
btnLeftSend.setText("Sent to Left");
btnLeftSend.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnRightSend = new Button();
btnRightSend.setPrefWidth(180);
btnRightSend.setText("Sent to right");
btnRightSend.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
VBox buttons = new VBox(3);
buttons.setPrefWidth(180);
buttons.getChildren().addAll(btnSave, btnLeftSend, btnRightSend);
titledPane.setAnimated(true);
titledPane.setText("Options");
titledPane.setContent(buttons);
titledPane.setExpanded(false);
Button btnLine1 = new Button();
btnLine1.setPrefWidth(180);
btnLine1.setText("Force expand");
btnLine1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(true);
}
});
HBox line1 = new HBox(5);
line1.setPadding(new Insets(5, 0, 0, 0));
line1.setAlignment(Pos.TOP_CENTER);
line1.setMaxSize(380, 40);
line1.setMinSize(380, 40);
line1.setPrefSize(380, 40);
line1.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
line1.getChildren().addAll(btnLine1, titledPane);
Button btnLine2 = new Button();
btnLine2.setPrefWidth(180);
btnLine2.setText("Force shrink");
btnLine2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
HBox line2 = new HBox(5);
line2.setPadding(new Insets(5, 0, 0, 0));
line2.setAlignment(Pos.TOP_CENTER);
line2.setMaxSize(380, 40);
line2.setMinSize(380, 40);
line2.setPrefSize(380, 40);
line2.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
line2.getChildren().add(btnLine2);
VBox layout = new VBox(5);
layout.setPadding(new Insets(5));
layout.getChildren().addAll(line1, line2);
Scene scene = new Scene(layout, 390, 160);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(Color.GHOSTWHITE);
primaryStage.setTitle("Sample code for StackOverFlow");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:1)
节点的z顺序由它们添加到父节点的顺序控制。当然,如果您使用VBox
,此订单也会确定VBox
内节点的位置。例如,考虑使用网格窗格,这样您就可以独立于添加子节点的顺序来控制位置。然后,在添加line1
后添加line2
:
// VBox layout = new VBox(5);
// layout.setPadding(new Insets(5));
// layout.getChildren().addAll(line1, line2);
GridPane layout = new GridPane();
layout.setVgap(5);
layout.setPadding(new Insets(5));
layout.add(line2, 0, 1);
layout.add(line1, 0, 0);
答案 1 :(得分:0)
非常感谢你的回复。我在我的应用程序中用GridPane替换了VBox,它按照我的意愿工作。 为了帮助其他人遇到同样的问题,请遵循James_D建议的修改后的代码。
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
TitledPane titledPane = new TitledPane();
titledPane.setPrefWidth(180);
Button btnSave = new Button();
btnSave.setPrefWidth(180);
btnSave.setText("Save");
btnSave.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnLeftSend = new Button();
btnLeftSend.setPrefWidth(180);
btnLeftSend.setText("Sent to Left");
btnLeftSend.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnRightSend = new Button();
btnRightSend.setPrefWidth(180);
btnRightSend.setText("Sent to right");
btnRightSend.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
VBox buttons = new VBox(3);
buttons.setPrefWidth(180);
buttons.getChildren().addAll(btnSave, btnLeftSend, btnRightSend);
titledPane.setAnimated(true);
titledPane.setText("Options");
titledPane.setContent(buttons);
titledPane.setExpanded(false);
Button btnLine1 = new Button();
btnLine1.setPrefWidth(180);
btnLine1.setText("Force expand");
btnLine1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
titledPane.setExpanded(true);
}
});
HBox line1 = new HBox(5);
line1.setPadding(new Insets(5, 0, 0, 0));
line1.setAlignment(Pos.TOP_CENTER);
line1.setMaxSize(380, 40);
line1.setMinSize(380, 40);
line1.setPrefSize(380, 40);
line1.getChildren().addAll(btnLine1, titledPane);
Button btnDummy = new Button();
btnDummy.setPrefWidth(365);
btnDummy.setText("Anything here. Just an example");
HBox line2 = new HBox(5);
line2.setPadding(new Insets(5, 0, 0, 0));
line2.setAlignment(Pos.TOP_CENTER);
line2.setMaxSize(380, 40);
line2.setMinSize(380, 40);
line2.setPrefSize(380, 40);
line2.getChildren().add(btnDummy);
GridPane layout = new GridPane();
layout.setVgap(5);
layout.setPadding(new Insets(5));
layout.add(line2, 0, 1);
layout.add(line1, 0, 0);
Scene scene = new Scene(layout, 390, 160);
primaryStage.setTitle("Sample code for StackOverFlow");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}