如何使用GridPane JavaFX使按钮跨越多列/行?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用GridPane JavaFX使按钮跨越多列/行?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2402字,纯文字阅读大概需要4分钟。
内容图文
![如何使用GridPane JavaFX使按钮跨越多列/行?](/upload/InfoBanner/zyjiaocheng/746/a9545c30e98142a78f53b28e47953761.jpg)
我是JavaFX的新手,正在尝试使用GridPane进行简单的按钮设计.
我无法弄清楚如何使按钮跨越多个列/行,而不会将其他按钮推开.我一直在使用HBox和VBox将其他按钮组合在一起.我在按钮上尝试过setRowSpan,但这似乎不起作用.
这是我的代码:
import javafx.stage.*;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
public class SimpleGUI extends Application {
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane();
Scene scene = new Scene(root, 200, 200);
//Buttons
Button b1 = new Button("KNAPP 1");
Button b2 = new Button("KNAPP 2");
Button b3 = new Button("KNAPP 3");
Button b4 = new Button("KNAPP 4");
Button b5 = new Button("KNAPP 5");
Button b6 = new Button("KNAPP 6");
//Horizontal Box
HBox topButtons = new HBox();
topButtons.getChildren().add(b1);
topButtons.getChildren().add(b2);
topButtons.getChildren().add(b3);
//Vertical Box
VBox leftButtons = new VBox();
leftButtons.getChildren().add(b4);
leftButtons.getChildren().add(b5);
//Placement
GridPane.setConstraints(topButtons, 0,0);
GridPane.setConstraints(leftButtons, 0,1);
GridPane.setConstraints(b6, 1,1);
//Length (3 Columns, 2 Rows)
GridPane.setColumnSpan(topButtons, 3);
GridPane.setRowSpan(leftButtons, 2);
//Add them to the stage
root.getChildren().add(topButtons);
root.getChildren().add(leftButtons);
root.getChildren().add(b6);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
解决方法:
由于您使用的是网格窗格,因此您不需要HBox和VBox:
import javafx.stage.*;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
public class SimpleGUI extends Application {
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane();
Scene scene = new Scene(root);
//Buttons
Button b1 = new Button("KNAPP 1");
Button b2 = new Button("KNAPP 2");
Button b3 = new Button("KNAPP 3");
Button b4 = new Button("KNAPP 4");
Button b5 = new Button("KNAPP 5");
Button b6 = new Button("KNAPP 6");
root.add(b1, 0, 0);
root.add(b2, 1, 0);
root.add(b3, 2, 0);
root.add(b4, 0, 1);
root.add(b5, 0, 2);
// node, columnIndex, rowIndex, columnSpan, rowSpan:
root.add(b6, 1, 1, 2, 2);
// allow button to grow:
b6.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
内容总结
以上是互联网集市为您收集整理的如何使用GridPane JavaFX使按钮跨越多列/行?全部内容,希望文章能够帮你解决如何使用GridPane JavaFX使按钮跨越多列/行?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。