我在场景上有一个按钮,只要按下空格键或回车键,这个按钮就会自动触发。我希望用户能够在不触发此按钮的情况下键入这些键。我已经尝试root.requestFocus()在我的场景中的其他节点上执行和调用请求焦点方法。当按下这些键时,如何防止此按钮触发。谢谢你的帮助。
编辑:到目前为止,我刚刚完成了使 Javafx 应用程序工作的样板代码,添加了该按钮和一些标签。我在我的应用程序的几个节点中尝试了 requestFocus() 方法,但没有一个有什么不同。我还有一个scene.setOnKeyPressed用于按下按键的动作事件侦听器。
我有以下代码:
<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">
<stylesheets>
<URL value="@menu.css"/>
</stylesheets>
<Button text="Customers" onAction="#handleCustomers" />
<Button text="Suppliers" onAction="#handleSuppliers" />
<Separator/>
<Button text="Families" onAction="#handleFamilies" />
<Button text="Products" onAction="#handleProducts" />
<Separator/>
</VBox>
Run Code Online (Sandbox Code Playgroud)
这会生成一组堆叠的按钮(根据需要),但它们不会占据容器的整个宽度。
如果我试试这个:
我有以下代码:
<VBox id="menu" styleClass="menu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.app.menu.MenuController">
<stylesheets>
<URL value="@menu.css"/>
</stylesheets>
<AnchorPane>
<Button text="Customers" onAction="#handleCustomers" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Button text="Suppliers" onAction="#handleSuppliers" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Separator/>
<Button text="Families" onAction="#handleFamilies" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Button text="Products" onAction="#handleProducts" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"/>
<Separator/>
</AnchorPane>
</VBox>
Run Code Online (Sandbox Code Playgroud)
然后我将按钮水平调整大小,但它们不再堆叠。我无法使用 FXML 在 Java FX 中实现这一点。
所需的输出是这样的:
我正在尝试了解 JavaFX 的坐标系。
对于某些节点(形状?),例如Line或Rectangle我可以(或应该)在坐标系中指定 ax 和 y 值。
这究竟是什么?这是稍后附加到节点或其他东西的平移和拉伸吗?其他节点只有一个setLayoutX()方法,而例如Line具有setLayoutX()和setStartX()。
谢谢!
我正在开发一个 JavaFX 应用程序。我需要TreeView使用波斯语以编程方式创建一个节点名称。
问题是我在运行应用程序时看到奇怪的字符。我在网上搜索过同样的问题。我编写了一个函数来根据同一问题的答案进行编码:
public static String getUTF(String encodeString) {
return new String(encodeString.getBytes(StandardCharsets.ISO_8859_1),
StandardCharsets.UTF_8);
}
Run Code Online (Sandbox Code Playgroud)
我用它来转换我的字符串来构建TreeView:
CheckBoxTreeItem<String> userManagement =
new CheckBoxTreeItem<>(GlobalItems.getUTF("???????"));
Run Code Online (Sandbox Code Playgroud)
对于某些字符,此答案无法正常工作:
我仍然得到奇怪的结果。如果我不使用编码,我会得到:
我已经用列制作了我的表格视图,一切正常。
我注意到的一件事是,当我过滤行时,一些数据保留在我定义为 cellFactory 的列中。
显示问题的图像:
列的代码:
@FXML
private TableColumn<Log, Integer> colQuanJour;
colQuanJour.setCellValueFactory(new PropertyValueFactory<>("quantite"));
Run Code Online (Sandbox Code Playgroud)
CellFactory 添加颜色和字符(+ 或 -)
colQuanJour.setCellFactory(
new Callback<TableColumn<Log,Integer>,TableCell<Log,Integer>>(){
@Override
public TableCell<Log, Integer> call(TableColumn<Log, Integer> param) {
return new TableCell<Log, Integer>(){
@Override
protected void updateItem(Integer item, boolean empty) {
if( ! empty){
int currIndex = indexProperty().getValue() < 0 ? 0
: indexProperty().getValue();
int operIndex = param.getTableView().getItems().get(currIndex).getOperation().getId_operation();
setStyle("-fx-font-weight: bold");
String Char ;
if(operIndex == 2){
setStyle("-fx-font-size : 16px");
setStyle("-fx-text-fill : red");
Char = "-";
}else {
setStyle("-fx-font-size : 16px");
setStyle("-fx-text-fill …Run Code Online (Sandbox Code Playgroud) 我想在一个标签中显示来自用户的输入信息,但我在运行时无法在标签中显示性别选项
button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event) {
if (male.isSelected()) {
info.setText(male.getText());
}
if (female.isSelected()) {
info.setText(female.getText());
}
}
});
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
info.setText(nameField.getText()+ "\n"+ noField.getText() +"\n"+ emaileField.getText() );
}
});
Run Code Online (Sandbox Code Playgroud) 我想让用户使用 ColorPicker 选择颜色,然后使用该颜色更改按钮的颜色。JavaFX
ColorPicker cp = new ColorPicker();
cp.setOnAction(e -> {
Color c = cp.getValue();
System.out.println(c);
});
Run Code Online (Sandbox Code Playgroud)
在 println 中,它会给出像 0xe6e64dff,0xccffccff... 这样的答案。
如果我想将按钮涂成蓝色,我需要使用这个:
Button button = new Button();
button.setStyle("-fx-background-color: #ff0000; ");
Run Code Online (Sandbox Code Playgroud)
所以我假设我必须先将颜色值转换为字符串才能使用它?或者我该怎么做?我如何让选择的颜色在 setStyle 行中可用?
我自己正在学习 Java,并且正在尝试像应用程序一样登录。if 语句不起作用,但 else 语句始终有效。
这是代码:
package Lessons;
public class Main extends Application {
Stage window;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Sign in");
TextField nameInput = new TextField();
Button button = new Button("Sign In");
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20,20));
layout.getChildren().addAll(nameInput, button);
Scene scene = new Scene(layout, 600, 600);
window.setScene(scene);
window.show();
//Checks the username
String username = "thomas";
if (nameInput.getText().equals(username)) {
button.setOnAction(e -> CorrectUsernameReplier.display());}
else …Run Code Online (Sandbox Code Playgroud) 我正在制作一个从 API 获取实时价格信息的程序。然后我想在实时更新的 JavaFX 图表上显示价格信息。当我尝试将信息传递给 JavaFX 线程时,它并不总是正确传递,并且 JavaFX 线程无法获取价格信息。
API 调用是在一个单独的线程上完成的,当它有信息时,它就会调用 updateScene 方法。这是我遇到问题的地方,API 线程包含我尝试设置 JavaFX 线程使用的变量的所有信息,但它没有任何信息。
private CandleStickFeed candleStickFeed;
public void updateScene(CandleStickFeed candleStickFeed){
this.candleStickFeed = candleStickFeed;
System.out.println("Feed Size This Thread = " + candleStickFeed.getSize());
Platform.runLater(new Runnable(){
@Override
public void run(){
System.out.println("Feed Size JavaFX Thread = " + candleStickFeed.getSize());
updateChart();
}
});
}
Run Code Online (Sandbox Code Playgroud)
程序有时会输出
进给尺寸 此线程 = 5
馈送大小 JavaFX 线程 = 5
这是我所期望的。但它有时也会输出
进给尺寸 此线程 = 5
馈送大小 JavaFX 线程 = 0
任何帮助将不胜感激。我刚开始使用多线程,所以不确定我在做什么。我一直在寻找不同的答案,但找不到任何答案。谢谢
如果我的控制器类是
public class FXMLDocumentController implements Initializable {
@FXML
private TextArea msgArea;
public void initialize(URL url, ResourceBundle rb) {
someThread.start();
}
}
Run Code Online (Sandbox Code Playgroud)
如何从线程更改 TextArea 的值?
编辑:我使用任务来解决这个问题。感谢所有试图提供帮助的人。