我有一个 JavaFX Text
,Scene
并且Group
Text waitingForKey
Scene scene
Group root
Run Code Online (Sandbox Code Playgroud)
我有一个waitingForKeyString
要添加的字符串 StringwaitingForKey
并且我想要居中对齐。
问题是有时字符串有两个句子,我无法对齐
String waitingForKeyString= " Level 2 \n\n" + "Press ENTER to start a new game";
Run Code Online (Sandbox Code Playgroud)
或者
String waitingForKeyString=" 按ENTER \n"+ "开始游戏", messageString="";
然后
Scene scene = new Scene(root,SCREEN_WIDTH, SCREEN_HEIGHT, Color.BLACK);
waitingForKey.setText(waitingForKeyString);
root.getChildren().add(waitingForKey);
Run Code Online (Sandbox Code Playgroud)
那么我怎么能把它对齐到中心呢?当我尝试首先对齐时,waitingForKeyString
我破坏了第二个 `waitingForKeyString,反之亦然。
我有一个带有GUI的游戏,你可以按下几个按钮来完成游戏.
我有以下布尔值来测试:
public boolean complete(){
if (initvalue==finalvaue)){
return true;
}
else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个监听器来检查游戏是否完成并向用户打印消息.我想我需要为此创建一个SimpleBooleanProperty
工作.但是我需要将它绑定到上面的布尔值吗?
最好的方法是什么?
我正在尝试创建一个ComboBox
将显示 selected 的预览Image
,但ComboBox
显示字符串值。
唯一有效的方法似乎是创建ComboBox
of Node
,但这会导致一旦选定的选项从下拉菜单中消失,如果有人有任何建议,我将不胜感激。
我的代码如下:
String notOnLine = "file:Java1.png";
String onLine = "file:Java2.png";
ObservableList<String> options = FXCollections.observableArrayList();
options.addAll(notOnLine, onLine);
final ComboBox<String> comboBox = new ComboBox(options);
comboBox.setCellFactory(c -> new StatusListCell());
Run Code Online (Sandbox Code Playgroud)
和ListCell
:
public class StatusListCell extends ListCell<String> {
protected void updateItem(String item, boolean empty){
super.updateItem(item, empty);
setGraphic(null);
setText(null);
if(item!=null){
ImageView imageView = new ImageView(new Image(item));
imageView.setFitWidth(40);
imageView.setFitHeight(40);
setGraphic(imageView);
setText("a");
}
}
}
Run Code Online (Sandbox Code Playgroud)
ComboBox
我希望在列表关闭后图像本身显示出来。现在它只显示 URL(例如file:Java1.png
)。
在这个例子中,我希望我的一个列TableView
从LocalDateTime
(在源数据模型中)格式化为格式为"yyyy/MM/dd kk:mm" TableColumn
.(注意:对于渲染,而不是编辑)好吧,在表视图中显示时,我需要在一些数据模型中使用Local/ZonedDateTime类型.做这个的最好方式是什么?(我没有注意到Oracle Table View教程中此类功能的示例).
编辑添加:或者,可能将数据模型中的值保持为String
(格式化),并LocalDateTime
在处理记录时使用转换为最佳?
import java.time.LocalDateTime;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) {
final ObservableList<Foo> data = FXCollections.observableArrayList();
LocalDateTime ldt = LocalDateTime.now();
data.add(new Foo(ldt));
data.add(new Foo(ldt.plusDays(1)));
data.add(new Foo(ldt.plusDays(2)));
TableView<Foo> table = new TableView<Foo>();
table.setItems(data);
TableColumn<Foo, LocalDateTime> ldtCol = new TableColumn<Foo, LocalDateTime>("LDT");
// --- Set cell factory …
Run Code Online (Sandbox Code Playgroud) 例子:
我们的名单包含 5 个名字:Kevin、Hans、Fritz、Han Solo、Peter
我现在想要所有顶部包含“Han”的名字。
所以排序后的列表看起来像这样:
汉斯、汉·索罗、凯文、弗里茨、彼得
到目前为止我已经尝试过:
没什么,因为我不知道,但我已经用谷歌搜索了,但没有找到任何东西。
从列表中删除/添加项目不是一个选项,因为我在CheckListView
(ControlsFX 组件)中使用列表,其中每个项目都有一个会丢失的选中状态。
我想以编程方式添加和删除BorderPane
. 问题是当我添加 a 时Node
,它不可见。
BorderPane
并StackPane
在 FXML 文件中定义。
我想做这样的事情:
StackPane temp = (StackPane) borderPane.getChildren().get(1);
borderPane.getChildren().remove(1);
borderPane.getChildren().add(0, temp);
Run Code Online (Sandbox Code Playgroud)
我试过了,borderPane.requestLayout()
但它不起作用。
我有以下代码:
public class JavaFxApplication2 extends Application {
@Override
public void start(Stage stage) throws Exception {
SplitPane splitPane=new SplitPane();
VBox vBox1=new VBox();
vBox1.setStyle("-fx-background-color: red");
VBox vBox2=new VBox();
vBox2.setStyle("-fx-background-color: blue");
splitPane.getItems().add(vBox1);
splitPane.getItems().add(vBox2);
splitPane.getDividers().get(0).setPosition(0);
Scene scene=new Scene(splitPane, 200, 400);
stage.setScene(scene);
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我运行应用程序时的输出:
现在,我调整大小并扩大舞台:
如您所见,分隔线位置现在不为0。如何在调整大小时禁用分隔线位置更改?
如何在css或代码中访问此列表以进行样式设置.我无法从modena.css中弄明白.
通过使用
.choice-box > * {
-fx-background-color: black;
}
Run Code Online (Sandbox Code Playgroud)
列表不受影响,因此它是某种单独的控件.
当我想要洗牌时,FilteredList
我得到一个java.lang.UnsupportedOperationException
.
怎么办呢?
码:
FilteredList<Card> filteredData =
new FilteredList(ob, filterByOption(option.get("selectedCard"), option.get("chapter")));
if (option.get("cardOrder") == "shuffle") {
filterCards=filteredData;
FXCollections.shuffle(filterCards);
}
Run Code Online (Sandbox Code Playgroud) 我做了一个TextField
在FXML
。我想添加Button
这个TextField
像清除按钮。我该如何实施?