按钮不响应javaFx
按钮不提供控制台输出。边框窗格底部的Vbox中有两个按钮,按下时应打印“新建”或“继续” 我遵循一个教程,并试图将其扩展到一个按钮按钮不响应javaFx,java,button,javafx,Java,Button,Javafx,按钮不提供控制台输出。边框窗格底部的Vbox中有两个按钮,按下时应打印“新建”或“继续” 我遵循一个教程,并试图将其扩展到一个按钮 import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application implements EventHandler<ActionEvent> {
Button btn1, btn2;
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
Button btn1 = new Button("new");
Button btn2 = new Button ("continue");
btn1.setOnAction(this);
btn2.setOnAction(this);
VBox vb = new VBox (btn1, btn2);
vb.setSpacing(10);
vb.setPadding(new Insets(20));
BorderPane root = new BorderPane();
root.setBottom(vb);
Scene scene = new Scene (root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
if (event.getSource()==btn1) {
System.out.println("new!");
} else if (event.getSource()==btn2) {
System.out.println("continue!");
}
}
}
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类主扩展应用程序实现EventHandler{
按钮btn1、btn2;
公共静态void main(字符串[]args){
发射(args);
}
public void start(Stage primaryStage)引发异常{
初级阶段。设置标题(“标题”);
按钮btn1=新按钮(“新”);
按钮btn2=新按钮(“继续”);
btn1.设定动作(本);
btn2.设定动作(本);
VBox vb=新的VBox(btn1,btn2);
vb.setspace(10);
vb.设置填充(新插图(20));
BorderPane根=新的BorderPane();
root.setBottom(vb);
场景=新场景(根,300,250);
初级阶段。场景(场景);
primaryStage.show();
}
@凌驾
公共无效句柄(ActionEvent事件){
如果(event.getSource()==btn1){
System.out.println(“新的!”);
}如果(event.getSource()==btn2){
System.out.println(“继续!”);
}
}
}
不会发生任何事情,但应该有一个“新建或继续”的输出。从按钮中删除类 这样,代码将使用字段中的按钮,而不是创建新按钮
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
btn1 = new Button("new");
btn2 = new Button ("continue");
// the rest of the code will be the same
}
从按钮中删除类 这样,代码将使用字段中的按钮,而不是创建新按钮
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("title");
btn1 = new Button("new");
btn2 = new Button ("continue");
// the rest of the code will be the same
}
你“似乎”在隐藏你的变量。您将
btn1
和btn2
都定义为start
的局部变量,这意味着,除非它们已在类的实例级别声明,否则尝试在处理程序中引用它们(event.getSource()==btn1
)将导致编译时错误我编辑了该问题。我希望现在更清楚了。没有编译错误,只是单击按钮时没有响应。是的,正如我所说的,您正在隐藏您的变量,您已经声明了btn1
和btn2
-我“假设”情况是这样的,否则您实际上会得到一个编译器错误,如上文所述,您应该删除在启动
中进行的重新减速,更换按钮btn1=新按钮(“新”)代码>带有btn1=新按钮(“新”)
要正确初始化类级别btn1
您“似乎”隐藏了变量。您将btn1
和btn2
都定义为start
的局部变量,这意味着,除非它们已在类的实例级别声明,否则尝试在处理程序中引用它们(event.getSource()==btn1
)将导致编译时错误我编辑了该问题。我希望现在更清楚了。没有编译错误,只是单击按钮时没有响应。是的,正如我所说的,您正在隐藏您的变量,您已经声明了btn1
和btn2
-我“假设”情况是这样的,否则您实际上会得到一个编译器错误,如上文所述,您应该删除在启动
中进行的重新减速,更换按钮btn1=新按钮(“新”)代码>带有btn1=新按钮(“新”)代码>正确初始化类级别btn1