如何在JavaFXGridPane中最大化多个网格中的一个指定网格
我使用一个网格窗格,其中有四个网格。但有时每个网格对于用户来说不够大,所以我打算通过双击最大化其中一个网格如何在JavaFXGridPane中最大化多个网格中的一个指定网格,javafx,javafx-8,Javafx,Javafx 8,我使用一个网格窗格,其中有四个网格。但有时每个网格对于用户来说不够大,所以我打算通过双击最大化其中一个网格 但是因为网格的内容是一种SwingNode,没有像setWidth()或setHeight()这样的方法,所以我如何实现这样的函数。双击可以操作GridPane的列约束和行约束的hgrow属性。下面是一个非常简单的例子: import java.awt.Color; import java.util.Random; import javafx.application.Applicatio
但是因为网格的内容是一种SwingNode,没有像setWidth()或setHeight()这样的方法,所以我如何实现这样的函数。双击可以操作
GridPane
的列约束
和行约束
的hgrow
属性。下面是一个非常简单的例子:
import java.awt.Color;
import java.util.Random;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ExpandingSwingNodesInGridPane extends Application {
private static final int NUM_ROWS = 3 ;
private static final int NUM_COLS = 3 ;
private static final double PREF_WIDTH = 0 ;
private static final double MAX_WIDTH = Double.MAX_VALUE ;
private static final double PREF_HEIGHT = 0 ;
private static final double MAX_HEIGHT = Double.MAX_VALUE ;
private static final Random RNG = new Random();
// coordinates in grid pane of currently expanded node:
private IntegerProperty expandedRowProperty = new SimpleIntegerProperty(-1);
private IntegerProperty expandedColumnProperty = new SimpleIntegerProperty(-1);
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
for (int x = 0 ; x < NUM_COLS; x++) {
ColumnConstraints colConstraints = new ColumnConstraints(PREF_WIDTH, PREF_WIDTH, MAX_WIDTH);
root.getColumnConstraints().add(colConstraints);
// change hgrow property according to expanded column:
colConstraints.hgrowProperty().bind(
Bindings.when(expandedColumnProperty.isEqualTo(x))
.then(Priority.ALWAYS)
.otherwise(Priority.SOMETIMES));
}
for (int y = 0 ; y < NUM_ROWS; y++) {
RowConstraints rowConstraints = new RowConstraints(PREF_HEIGHT, PREF_HEIGHT, MAX_HEIGHT);
root.getRowConstraints().add(rowConstraints);
// change vgrow property according to expanded row:
rowConstraints.vgrowProperty().bind(Bindings.when(expandedRowProperty.isEqualTo(y))
.then(Priority.ALWAYS)
.otherwise(Priority.SOMETIMES));
}
for (int x = 0 ; x < NUM_COLS; x++) {
for (int y = 0 ; y < NUM_ROWS; y++) {
addSwingNode(root, x, y);
}
}
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addSwingNode(GridPane root, int x, int y) {
SwingNode swingNode = new SwingNode();
// expand or collapse on double click:
swingNode.setOnMouseClicked(e -> {
if (e.getClickCount() == 2) {
if (expandedColumnProperty.get() == x && expandedRowProperty.get() == y) {
// currently expanded, unexpand:
expandedColumnProperty.set(-1);
expandedRowProperty.set(-1);
} else {
expandedColumnProperty.set(x);
expandedRowProperty.set(y);
}
}
});
root.add(swingNode, x, y);
SwingUtilities.invokeLater(() -> {
JComponent comp = new JPanel();
comp.setBackground(new Color(RNG.nextInt(0xFFFFFF)));
swingNode.setContent(comp);
});
}
public static void main(String[] args) {
launch(args);
}
}
导入java.awt.Color;
导入java.util.Random;
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.embed.swing.SwingNode;
导入javafx.scene.scene;
导入javafx.scene.layout.ColumnConstraints;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.Priority;
导入javafx.scene.layout.RowConstraints;
导入javafx.stage.stage;
导入javax.swing.JComponent;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类ExpandingSwingNodesInGridPane扩展应用程序{
私有静态final int NUM_ROWS=3;
私有静态最终整数=3;
专用静态最终双优先级宽度=0;
专用静态最终双最大宽度=double.MAX\u值;
专用静态最终双优先高度=0;
专用静态最终双最大高度=双最大值;
私有静态最终随机RNG=新随机();
//当前展开节点的网格窗格中的坐标:
私有IntegerProperty expandedRowProperty=新的SimpleIntegerProperty(-1);
私有IntegerProperty expandedColumnProperty=新的SimpleIntegerProperty(-1);
@凌驾
公共无效开始(阶段primaryStage){
GridPane root=新的GridPane();
对于(int x=0;x{
如果(如getClickCount()==2){
if(expandedColumnProperty.get()==x&&expandedRowProperty.get()==y){
//当前已扩展,未扩展:
expandedColumnProperty.set(-1);
expandedRowProperty.set(-1);
}否则{
expandedColumnProperty.set(x);
expandedRowProperty.set(y);
}
}
});
添加(swingNode,x,y);
SwingUtilities.invokeLater(()->{
JComponent comp=新的JPanel();
公司背景(新颜色(RNG.nextInt(0xFFFFFF));
swingNode.setContent(comp);
});
}
公共静态void main(字符串[]args){
发射(args);
}
}
非常感谢您的建议!詹姆斯先生,这对我很有帮助!