2016-01-31 3 views
0

J'ai fait une simple fenêtre de connexion avec Eclipse (Main.java, application.css, Login.fxml) Comment puis-je ajouter un écouteur sur les 2 boutons?JavaFX Connexion avec FXML

Main.java

package application; 

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

public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      Parent root = FXMLLoader.load(getClass().getResource("Login.fxml")); 
      Scene scene = new Scene(root,350,150); 
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      primaryStage.setTitle("LOGIN"); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

Login.fxml

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

<?import javafx.scene.layout.GridPane?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.control.PasswordField?> 
<?import javafx.scene.layout.HBox?> 

<GridPane xmlns:fx="http://javafx.com/fxml/1" hgap="10" vgap="10"> 
    <padding> 
     <Insets top="10" right="10" bottom="10" left="10"/> 
    </padding> 
    <children> 
     <Label text="Username:" GridPane.columnIndex="0" 
       GridPane.rowIndex="0" GridPane.halignment="RIGHT" /> 
     <Label text="Password:" GridPane.columnIndex="0" 
       GridPane.rowIndex="1" GridPane.halignment="RIGHT" /> 
     <TextField GridPane.columnIndex="1" GridPane.rowIndex="0"/> 
     <PasswordField GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
     <HBox GridPane.columnIndex="0" GridPane.rowIndex="2" 
       GridPane.columnSpan="2" alignment="CENTER" spacing="10"> 
      <children> 
       <Button text="Login" /> 
       <Button text="Annulla" /> 
      </children> 
     </HBox> 
    </children> 
</GridPane> 

application.css

/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */ 
.label { 
    -fx-text-fill: rgb(255,0,0); 
    -fx-font-weight: bold; 
    -fx-font-style: italic; 
} 

Je dois obtenir le mot de passe nom d'utilisateur e et se connecter à une base de données

Répondre

0

Vous devez définir une controller class.

Vous pouvez injecter les commandes dans l'instance du contrôleur en les annotant @FXML, et vous pouvez associer des méthodes dans le contrôleur avec des gestionnaires pour les boutons:

package application ; 

public class LoginController { 

    @FXML 
    private TextField userIdField ; 

    @FXML 
    private PasswordField passwordField ; 

    @FXML 
    private void login() { 
     String userId = userIdField.getText(); 
     String password = passwordField.getText(); 

     // etc... 
    } 
} 

Dans votre FXML, utilisez fx:controller sur l'élément racine spécifiez la classe à utiliser pour créer un contrôleur. Utilisez fx:id pour injecter un élément dans le contrôleur, et utiliser onAction="#methodName" pour spécifier une méthode de gestionnaire pour l'action sur un bouton:

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

<?import javafx.scene.layout.GridPane?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.geometry.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.control.PasswordField?> 
<?import javafx.scene.layout.HBox?> 

<GridPane xmlns:fx="http://javafx.com/fxml/1" hgap="10" vgap="10" 
    fx:controller="application.LoginController"> 

    <padding> 
     <Insets top="10" right="10" bottom="10" left="10"/> 
    </padding> 
    <children> 
     <Label text="Username:" GridPane.columnIndex="0" 
       GridPane.rowIndex="0" GridPane.halignment="RIGHT" /> 
     <Label text="Password:" GridPane.columnIndex="0" 
       GridPane.rowIndex="1" GridPane.halignment="RIGHT" /> 
     <TextField fx:id="userIdField" GridPane.columnIndex="1" GridPane.rowIndex="0"/> 
     <PasswordField fx:id="passwordField" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
     <HBox GridPane.columnIndex="0" GridPane.rowIndex="2" 
       GridPane.columnSpan="2" alignment="CENTER" spacing="10"> 
      <children> 
       <Button text="Login" onAction="#login" /> 
       <Button text="Annulla" /> 
      </children> 
     </HBox> 
    </children> 
</GridPane> 
+0

cela fonctionne merci – xc1469