gpt4 book ai didi

javafx-2 - JavaFX 绑定(bind)简单示例

转载 作者:行者123 更新时间:2023-12-04 02:43:14 25 4
gpt4 key购买 nike

我正在编写一个简单的程序,将 ChoiceBox 的值绑定(bind)到 Label 节点的字体值。但是,我无法做到这一点。

应用

package mybinding;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ChoiceBoxBuilder;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.GridPaneBuilder;
import javafx.stage.Stage;

public class SimpleBindingMain extends Application{

Label simpleLabel;
ChoiceBox availableFonts;
GridPane gridPane;
SimpleBindingModel model = new SimpleBindingModel();
Scene scene;

@Override
public void start(Stage stage) throws Exception {
availableFonts = ChoiceBoxBuilder
.create()
.items(model.fonts)
.build();
simpleLabel = LabelBuilder
.create()
.text("Font Preview")
.font(model.theFont)
.build();

gridPane = GridPaneBuilder.create().build();
gridPane.add(simpleLabel,0,0);
gridPane.add(availableFonts,0,1);

model.fontSelectionModel = availableFonts.getSelectionModel();
model.addFontSelectionListener();

scene = SceneBuilder
.create()
.root(gridPane)
.build();

/**
* ADD BINDING CODE
*/

stage.setScene(scene);
stage.sizeToScene();
stage.show();

}

public static void main(String[] args) {
Application.launch("mybinding.SimpleBindingMain");
}
}

模型

package mybinding;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.text.Font;

public class SimpleBindingModel {
public StringProperty string = new SimpleStringProperty(); // the actual text
public IntegerProperty textSize = new SimpleIntegerProperty(); // the size of the text
public ObservableList fonts = FXCollections.observableArrayList( // possible fonts
"DejaVu Sans Mono",
"Nimbus Sans L",
"Ubuntu",
"Ubuntu Condensed"
);
public Font theFont = Font.font("DejaVu Sans Mono",18); // default font
public SingleSelectionModel fontSelectionModel;


public void addFontSelectionListener(){
fontSelectionModel.selectedIndexProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue value, Object oldValue, Object newValue) {
int selectedFontIndex = fontSelectionModel.selectedIndexProperty().getValue();
theFont = Font.font((String)fonts.get(selectedFontIndex),12);
}
});
}
}

请帮我解决绑定(bind)代码。

最佳答案

你快到了:-)。只需进行一些小的修改。

首先,更改SimpleBindingModel.theFont属性类型为 ObjectProperty<Font> , 允许 SimpleBindingMain.simpleLabel.fontProperty受其约束。

在这种情况下,属性声明将是:

public ObjectProperty<Font> theFont = new SimpleObjectProperty<Font>(Font.font("DejaVu Sans Mono",18));

之后,您需要更改theFont 所在的点被读取/设置。例如,在字体选择监听器中:

public void addFontSelectionListener(){
fontSelectionModel.selectedIndexProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue value, Object oldValue, Object newValue) {
int selectedFontIndex = fontSelectionModel.selectedIndexProperty().getValue();
// old code: theFont = Font.font((String)fonts.get(selectedFontIndex),12);
theFont.set(Font.font((String)fonts.get(selectedFontIndex),12));
}
});
}

最后,你可以绑定(bind)simpleLabel.fontPropertytheFont :

simpleLabel.fontProperty().bind(model.theFont);

关于javafx-2 - JavaFX 绑定(bind)简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19433726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com