gpt4 book ai didi

JavaFx 自定义设计按钮在使用的应用程序中单击检测

转载 作者:行者123 更新时间:2023-12-01 19:52:38 25 4
gpt4 key购买 nike

使用 FXML 使用两个文件 CustomToggleSwitch.fxml 和 CustomToggleSwitch.java 创建自定义设计。

CustomToggleSwitch.fxml 有以下代码

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<fx:root type="Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" onAction="#click" text="Button" />
</children>
</fx:root>

CustomToggleSwitch.java 有代码

package com.custom;

import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;

public class CustomToggleSwitch extends Pane{
int tick;

public CustomToggleSwitch() {

FXMLLoader loader=new FXMLLoader(getClass().getResource("CustomToggleSwitch.fxml"));
loader.setRoot(this);
loader.setController(this);

try {
loader.load();
} catch (IOException e) {
e.printStackTrace();
}
}

@FXML
public void click(ActionEvent actionEvent){
System.out.println(tick++);
}
}

从这些创建 jar 文件并在应用程序项目中使用此 jar 文件。应用程序有 test.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import com.custom.*?>
<?import com.custom.CustomToggleSwitch?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<CustomToggleSwitch layoutX="29.0" layoutY="48.0" />
</children>
</AnchorPane>

Controller 类(TestController.java)具有以下内容

public class TestController implements Initializable{

@Override
public void initialize(URL arg0, ResourceBundle arg1) {

}

@FXML
public void click(){
System.out.println("Test");
}
}

按钮在 GUI 上成功显示,并且还检测到按钮单击。按钮在输出控制台上显示 0,1,2,3..等。但测试没有打印在屏幕上。

如何检测应用程序 Controller 类中的按钮按下情况?有人可以帮助我解决此问题。

非常感谢大家。

最佳答案

add listener property with getter and setter in your component controller.

Jai的例子是正确的,但与javafx的其他组件不一致。为了正确实现,最好使用可在 FXML 和手动模式下操作的属性。

这是添加了 onMyAction 属性的用户组件 Controller 。该属性用于事件通知。

public class CustomToggleSwitch extends Pane {

private ObjectProperty<EventHandler<ActionEvent>> onMyAction = new SimpleObjectProperty<EventHandler<ActionEvent>>();

public CustomToggleSwitch() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/sample/CustomToggleSwitch.fxml"));
loader.setRoot(this);
loader.setController(this);

try {
loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
}

@FXML
private void initialize() {

}

@FXML
private void click(ActionEvent event) {
if(onMyAction.get() != null) {
onMyAction.get().handle(event);
}
}

public EventHandler<ActionEvent> getOnMyAction() {
return onMyAction.get();
}

public ObjectProperty<EventHandler<ActionEvent>> onMyActionProperty() {
return onMyAction;
}

public void setOnMyAction(EventHandler<ActionEvent> onMyAction) {
this.onMyAction.set(onMyAction);
}
}

对于这样的结构化组件,例如可以通过 FXML 添加 onMyAction 属性

<AnchorPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" >
<CustomToggleSwitch onMyAction="#testHandler"/>
</AnchorPane>
<小时/>
public class Controller {

@FXML
private void testHandler(ActionEvent event) {

}

}

或手动

<AnchorPane fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml" >
<CustomToggleSwitch fx:id="customToggleSwitch"/>
</AnchorPane>
<小时/>
public class Controller {

@FXML
private CustomToggleSwitch customToggleSwitch;

@FXML
private void initialize() {
customToggleSwitch.setOnMyAction(event -> {

});
}

}
<小时/>

更新

You don't need to use a property. Making the getter and setter available should be sufficient to use it from fxml. (I didn't encountered a case where I would add a listerner to a event handler property yet.)

这是不使用属性的转换

public class CustomToggleSwitch extends Pane {

private EventHandler<ActionEvent> myEventHandler;

public CustomToggleSwitch() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/sample/CustomToggleSwitch.fxml"));
loader.setRoot(this);
loader.setController(this);

try {
loader.load();
}
catch (IOException e) {
e.printStackTrace();
}
}

@FXML
private void initialize() {

}

@FXML
private void click(ActionEvent event) {
if(myEventHandler != null) {
myEventHandler.handle(event);
}
}

public EventHandler<ActionEvent> getOnMyAction() {
return myEventHandler;
}

public void setOnMyAction(EventHandler<ActionEvent> onMyAction) {
myEventHandler = onMyAction;
}
}

关于JavaFx 自定义设计按钮在使用的应用程序中单击检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50790838/

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