这背后的想法是拥有一个"搜索"文本域。当用户键入某些数据并单击按钮时。另一个窗口打开,其数据根据输入"搜索"文本域。
我创建了像TextField search = new TextField();
点击按钮我这样做:
searchButton.setOnAction(e -> {
String st = search.getText().toString();
System.out.println(search.getText());
setSt(st);
InfoWindow.display("Detailed information on " + st);
});
setSt()方法如下所示:
public void setSt(String st)
{
this.st = st;
}
this.st
的声明如下:private static String st;
在信息窗口类中,我尝试评估用户输入的内容:
public static void display(String title){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
String st = MainClass.getSt();
if(st == "M16"){
textLabel = new Label("The M16 rifle, officially designated Rifle, Caliber 5.56 mm.\n" +
" M16, is a United States military adaptation of the Armalite AR-15 rifle.\n" +
"The original M16 was a select-fire, 5.56×45mm rifle with a 20-round magazine.");
}
VBox layout = new VBox(10);
layout.getChildren().addAll(textLabel);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout);
window.setScene(scene);
window.showAndWait();
}
getSt()方法如下所示:
public static String getSt()
{
return st;
}
我已经在调试器的帮助下完成了程序流程。它已经到了评估点if(st == "M16")
,我可以在调试器中看到st =" M16"(因为我已经在"搜索&#34中输入了它; textfield),但它没有进入' if'它只是继续,好像条件是假的。这可能是什么问题?你能帮忙解决一下吗?这绝对不是我所知的。