2014-01-17 54 views
0

在现场建设者我与FX的密码字段:ID passwordBox并在相应的控制器类我有JavaFX的密码字段不工作

@FXML私有静态PasswordField passwordBox =新PasswordField();

我也试过

@FXML私有静态PasswordField passwordBox;

当我运行程序时,密码字段中的字母是纯文本。当我在场景构建器中预览窗口时,会发生同样的情况。密码字段是一个PasswordField,所以我没有把它误认为TextField。

我该怎么办?

编辑: FXML文件

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

<?import java.lang.*?> 
<?import java.net.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.image.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.*?> 

<AnchorPane fx:id="mainAnchor" opacity="1.0" prefHeight="200.0" prefWidth="408.0000999999975" styleClass="back" xmlns:fx="http://javafx.com/fxml" fx:controller="application.MainController"> 
    <!-- TODO Add Nodes --> 
    <children> 
    <Label fx:id="serverIPLbl" layoutX="14.0" layoutY="28.0" prefWidth="175.0" text="Server IP"> 
     <font> 
     <Font name="Segoe UI" size="12.0" fx:id="x1" /> 
     </font> 
    </Label> 
    <TextField fx:id="serverIPBox" layoutX="14.0" layoutY="50.0" prefWidth="175.0" /> 
    <Label fx:id="portLbl" font="$x1" layoutX="215.0" layoutY="28.0" prefWidth="175.0" text="Port" /> 
    <TextField fx:id="portBox" layoutX="215.0" layoutY="50.0" prefWidth="175.0" /> 
    <Label fx:id="passwordLbl" font="$x1" layoutX="14.0" layoutY="81.0" prefWidth="175.0" text="Server Password" /> 
    <ImageView fx:id="settingsButton" fitHeight="23.0" fitWidth="23.0" layoutX="14.0" layoutY="137.0" onMouseClicked="#settingsClicked" pickOnBounds="true" preserveRatio="true"> 
     <image> 
     <Image url="@../images/gear.png" /> 
     </image> 
    </ImageView> 
    <Button id="startServer" fx:id="connectButton" layoutX="14.0" layoutY="167.0" mnemonicParsing="false" onAction="#connectClicked" prefHeight="22.0" prefWidth="376.0" text="Connect" /> 
    <Label fx:id="usernameLbl" font="$x1" layoutX="215.0" layoutY="81.0" prefWidth="175.0" text="Username" /> 
    <TextField id="serverIPBox" fx:id="usernameBox" layoutX="215.0" layoutY="103.0" prefWidth="175.0" promptText="a-z A-Z 0-9 _ - chars allowed" /> 
    <ImageView id="favoriteButton" fx:id="favoritesButton" fitHeight="23.0" fitWidth="23.0" layoutX="55.0" layoutY="137.0" onMouseClicked="#favoritesClicked" pickOnBounds="true" preserveRatio="true"> 
     <image> 
     <Image url="@../images/favorite.png" /> 
     </image> 
    </ImageView> 
    <PasswordField fx:id="passwordBox" layoutX="14.0" layoutY="103.0" prefWidth="175.0" promptText="Optional" /> 
    </children> 
    <stylesheets> 
    <URL value="@style.css" /> 
    </stylesheets> 
</AnchorPane> 
+0

你可以发布你的fxml文件吗? –

+0

@SudipSaha添加了 –

回答

1

问题与您的代码

@FXML是注射注解为控制器的情况下,你不应该结合static成员或成员使用它,你使用new关键字初始化。

而是在你的控制器的定义应该是:

@FXML private PasswordField passwordBox; 

这可能会或可能不会在你的情况下,通过一个新的FXMLLoader申请

  • Controller实例应该总是创建其他注意事项( ).load(...)调用(不通过控制器本身的new关键字,除非您随后使用loader.setController())。
  • fxml文件必须为您的密码字段指定一个名称,该名称与控制器中的名称相匹配(在您的情况下,该名称为passwordBox)。

GatewayApplication.java

package application; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 

import java.io.IOException; 

public class GatewayApplication extends Application { 
    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws IOException { 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("passport.fxml")); 
     AnchorPane layout = loader.load(); 
     stage.setScene(new Scene(layout)); 
     stage.show(); 
    } 
} 

MainController.java

通过SceneBuilderView | Show Sample Skeleton生成。

package application; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.scene.image.ImageView; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.AnchorPane; 


public class MainController { 

    @FXML 
    private ResourceBundle resources; 

    @FXML 
    private URL location; 

    @FXML 
    private Button connectButton; 

    @FXML 
    private ImageView favoritesButton; 

    @FXML 
    private AnchorPane mainAnchor; 

    @FXML 
    private PasswordField passwordBox; 

    @FXML 
    private Label passwordLbl; 

    @FXML 
    private TextField portBox; 

    @FXML 
    private Label portLbl; 

    @FXML 
    private TextField serverIPBox; 

    @FXML 
    private Label serverIPLbl; 

    @FXML 
    private ImageView settingsButton; 

    @FXML 
    private TextField usernameBox; 

    @FXML 
    private Label usernameLbl; 


    @FXML 
    void connectClicked(ActionEvent event) { 
     System.out.println("password = " + passwordBox.getText()); 
    } 

    @FXML 
    void favoritesClicked(MouseEvent event) { 
    } 

    @FXML 
    void settingsClicked(MouseEvent event) { 
    } 

    @FXML 
    void initialize() { 
     assert connectButton != null : "fx:id=\"connectButton\" was not injected: check your FXML file 'passport.fxml'."; 
     assert favoritesButton != null : "fx:id=\"favoritesButton\" was not injected: check your FXML file 'passport.fxml'."; 
     assert mainAnchor != null : "fx:id=\"mainAnchor\" was not injected: check your FXML file 'passport.fxml'."; 
     assert passwordBox != null : "fx:id=\"passwordBox\" was not injected: check your FXML file 'passport.fxml'."; 
     assert passwordLbl != null : "fx:id=\"passwordLbl\" was not injected: check your FXML file 'passport.fxml'."; 
     assert portBox != null : "fx:id=\"portBox\" was not injected: check your FXML file 'passport.fxml'."; 
     assert portLbl != null : "fx:id=\"portLbl\" was not injected: check your FXML file 'passport.fxml'."; 
     assert serverIPBox != null : "fx:id=\"serverIPBox\" was not injected: check your FXML file 'passport.fxml'."; 
     assert serverIPLbl != null : "fx:id=\"serverIPLbl\" was not injected: check your FXML file 'passport.fxml'."; 
     assert settingsButton != null : "fx:id=\"settingsButton\" was not injected: check your FXML file 'passport.fxml'."; 
     assert usernameBox != null : "fx:id=\"usernameBox\" was not injected: check your FXML file 'passport.fxml'."; 
     assert usernameLbl != null : "fx:id=\"usernameLbl\" was not injected: check your FXML file 'passport.fxml'."; 
    } 

} 

的style.css

.root { 
    -fx-background-color: cornsilk; 
} 

护照。FXML

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

<?import javafx.scene.control.*?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 
<?import java.net.URL?> 
<AnchorPane fx:id="mainAnchor" opacity="1.0" prefHeight="200.0" prefWidth="408.0000999999975" styleClass="back" xmlns:fx="http://javafx.com/fxml" fx:controller="application.MainController"> 
    <children> 
     <Label fx:id="serverIPLbl" layoutX="14.0" layoutY="28.0" prefWidth="175.0" text="Server IP"> 
      <font> 
       <Font name="Segoe UI" size="12.0" fx:id="x1" /> 
      </font> 
     </Label> 
     <TextField fx:id="serverIPBox" layoutX="14.0" layoutY="50.0" prefWidth="175.0" /> 
     <Label fx:id="portLbl" font="$x1" layoutX="215.0" layoutY="28.0" prefWidth="175.0" text="Port" /> 
     <TextField fx:id="portBox" layoutX="215.0" layoutY="50.0" prefWidth="175.0" /> 
     <Label fx:id="passwordLbl" font="$x1" layoutX="14.0" layoutY="81.0" prefWidth="175.0" text="Server Password" /> 
     <ImageView fx:id="settingsButton" fitHeight="23.0" fitWidth="23.0" layoutX="14.0" layoutY="137.0" onMouseClicked="#settingsClicked" pickOnBounds="true" preserveRatio="true"> 
      <image> 
       <Image url="http://icons.iconarchive.com/icons/hopstarter/soft-scraps/24/Gear-icon.png" /> 
      </image> 
     </ImageView> 
     <Button id="startServer" fx:id="connectButton" layoutX="14.0" layoutY="167.0" mnemonicParsing="false" onAction="#connectClicked" prefHeight="22.0" prefWidth="376.0" text="Connect" /> 
     <Label fx:id="usernameLbl" font="$x1" layoutX="215.0" layoutY="81.0" prefWidth="175.0" text="Username" /> 
     <TextField id="serverIPBox" fx:id="usernameBox" layoutX="215.0" layoutY="103.0" prefWidth="175.0" promptText="a-z A-Z 0-9 _ - chars allowed" /> 
     <ImageView id="favoriteButton" fx:id="favoritesButton" fitHeight="23.0" fitWidth="23.0" layoutX="55.0" layoutY="137.0" onMouseClicked="#favoritesClicked" pickOnBounds="true" preserveRatio="true"> 
      <image> 
       <Image url="http://icons.iconarchive.com/icons/hopstarter/soft-scraps/24/Button-Favorite-icon.png"/> 
      </image> 
     </ImageView> 
     <PasswordField fx:id="passwordBox" layoutX="14.0" layoutY="103.0" prefWidth="175.0" promptText="Optional" /> 
    </children> 
    <stylesheets> 
     <URL value="@style.css" /> 
    </stylesheets> 
</AnchorPane> 

样本程序UI

sampleui

示例程序输出

在密码字段中输入魔术作品 “XYZZY” 并按下连接,程序从现场提取密码并将其输出到控制台:

使用
password = xyzzy 

测试系统OS X 10.9上运行Java 8b121

在这样的未来问题中,您可能需要提供minimal, complete, tested and readable example。所有问题都不需要这样的样本,但肯定会对这个问题有所帮助。

+0

我做了你的建议,但它没有改变任何东西 –

+0

添加了示例代码,它不会复制问题中提到的问题。 – jewelsea

+0

我有一个控制器类中的方法,它调整组件的高DPI屏幕。所以我的组件必须是静态的,因为方法是静态的。在我的拥有FXMLLoader的Main类中,我运行该方法。我如何去除所有东西的静电并仍然允许它工作?任何想法为什么密码字段在静态时不会运行? –