我正在尝试使用JavaFX创建应用程序。我有个问题。当我要更新另一个场景中的标签文本时,我无法执行此操作。每当我使用setText()更新标签文本时,它都会返回nullpointerexception。我试过创建标签实例,但这甚至无济于事。我正在发布代码段和fxml文档。请帮我该怎么办。这是我的代码。
public void screenforward(ActionEvent event) throws IOException {
String a = pin.getText();
System.out.println(a);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mithu?autoReconnect=true&useSSL=false","root","polkadot");
Statement stmt=con.createStatement();
ResultSet rs1=stmt.executeQuery("select * from banking where pin="+a);
int bal = -1;//,id=1;
String first = null,last = null;
if(rs1.next()) {
//System.out.println("Welcome "+rs.getString(5)+" "+rs.getString(6));
first=rs1.getString(5);
last=rs1.getString(6);
bal=rs1.getInt(7);
//id=rs.getInt(1);
}
String tex="Welcome "+first+" "+last+".";
if(bal!=-1)
{
withdraw.setText("-_-");
System.out.println(tex);
}//
} catch (Exception e) {
System.out.println(e);
}
Parent ask = FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene askscene = new Scene(ask);
Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();
window.setScene(askscene);
window.show();
}
每当我更改场景时,都会调用此函数,并且希望此函数更改场景中未发生的标签文本。下面是我的fxml文档。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.Bloom?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="319.0" prefWidth="591.0" style="-fx-background-color: #1B2D6F;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="196.0" layoutY="27.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@banner.png" />
</image>
</ImageView>
<Button layoutX="47.0" layoutY="120.0" mnemonicParsing="false" onAction="#screen2" prefHeight="42.0" prefWidth="93.0" text="Fund Transfer" />
<Button layoutX="47.0" layoutY="182.0" mnemonicParsing="false" onAction="#screen2" prefHeight="42.0" prefWidth="93.0" text="Change Pin" />
<Button layoutX="47.0" layoutY="240.0" mnemonicParsing="false" onAction="#screen2" prefHeight="42.0" prefWidth="93.0" text="Change Phone" />
<Button fx:id="withdraw" layoutX="450.0" layoutY="120.0" mnemonicParsing="false" onAction="#screen2" prefHeight="42.0" prefWidth="100.0" text="Withdraw" />
<Button layoutX="450.0" layoutY="182.0" mnemonicParsing="false" onAction="#screen2" prefHeight="42.0" prefWidth="100.0" text="Mini Statement" />
<Button layoutX="450.0" layoutY="240.0" mnemonicParsing="false" onAction="#screen2" prefHeight="42.0" prefWidth="100.0" text="Check Balance" />
<Label fx:id="name" alignment="CENTER" layoutX="215.0" layoutY="131.0" prefHeight="63.0" prefWidth="161.0" text="..Anything.." textFill="#e4eb0f">
<effect>
<Bloom />
</effect>
</Label>