我希望我的课程能够像按钮一样触发动作(但我在课堂上包含了按钮)。我想知道如何做到这一点,如果这违反了Java规则呢?
我需要做些什么才能使ValueButton
成为可以从(单击按钮时)接收动作事件的类型,并接收对发起事件的ValueButton
对象的引用?
import javafx.scene.control.Button;
public class ValueButton {
Button button;
int val;
/** Use string of value for string */
public ValueButton(int val) {
init(val, null);
}
/** Provide value and string explicity */
public ValueButton(int val, String str) {
init(val, str);
}
private void init(int val, String str) {
this.val = val;
if(str == null)
str = Integer.toString(val);
button = new Button(str);
button.setOnAction((ae) -> {
// I would like to pass *this* along to caller/user of ValueButton
});
}
int getValue() { return val; }
String getString() { return button.getText(); }
}