# JavaFX không hiển thị form

**URL:** https://daynhauhoc.com/t/javafx-khong-hien-thi-form/125503
**Category:** programming
**Tags:** java, javafx
**Created:** [April 28, 2022, 7:44pm UTC](https://daynhauhoc.com/t/javafx-khong-hien-thi-form/125503 "2022-04-28T19:44:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Nguyen\_Tuan\_Khai\_Kha](https://daynhauhoc.com/user_avatar/daynhauhoc.com/nguyen_tuan_khai_kha/32/47434_2.png) [@Nguyen\_Tuan\_Khai\_Kha](https://daynhauhoc.com/u/Nguyen_Tuan_Khai_Kha)
#### Post date: [April 28, 2022, 7:44pm UTC](https://daynhauhoc.com/t/javafx-khong-hien-thi-form/125503/1 "2022-04-28T19:44:45Z")

</div>

![Untitled](https://daynhauhoc.s3.dualstack.ap-southeast-1.amazonaws.com/original/3X/0/8/0825891a1628495721d315c355d7376eb2dd01ea.png)  
Lúc đầu chạy thử thì được nhưng khi kết nối Controller thì form nó không hiển thị và thông báo lỗi  
` java.lang.ClassNotFoundException:/controllers/LoginController`  
Main

```auto
package controllers;

import java.io.IOException;

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

public class App extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage primaryStage) {
		try {
			Parent root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
			primaryStage.setTitle("Quản lý bán hàng");
			Scene scene = new Scene(root);
			scene.getStylesheets().add(getClass().getResource("/css/Login.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
}

```

Controller

```auto
package controllers;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

public class LoginController {
	@FXML
	private TextField username;
	
	@FXML
	private PasswordField password;
	
	public void action(ActionEvent event) {
		if(username.getText().trim().isEmpty()|| password.getText().trim().isEmpty()) {
			Alert alert = new Alert(AlertType.ERROR);
			alert.setHeaderText("Thông báo lỗi");
			alert.setContentText("Vui lòng điền tài khoản và mật khẩu");
			alert.show();
		}else if(username.getText().trim().equals("admin")&&password.getText().trim().equals("admin123")){
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setContentText("Đăng nhập thành công");
			alert.show();
		}else {
			Alert alert = new Alert(AlertType.ERROR);
			alert.setHeaderText("Thông báo lỗi");
			alert.setContentText("Bạn điền sai tài khoản và mật khẩu");
			alert.show();
		}
			
	}
}

```

FXML

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>

<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="275.0" prefWidth="500.0" type="AnchorPane" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="/controller/LoginController">
   <children>
      <Pane prefHeight="75.0" prefWidth="500.0" style="-fx-background-color: gray;">
         <children>
            <Label id="loginLabel" layoutX="128.0" prefHeight="75.0" stylesheets="@../css/Login.css" text="Đăng nhập" />
         </children>
      </Pane>
      <Pane layoutX="1.0" layoutY="75.0" prefHeight="200.0" prefWidth="500.0">
         <children>
            <Label layoutX="26.0" layoutY="31.0" text="Tài khoản" />
            <Label layoutX="27.0" layoutY="83.0" text="Mật khẩu" />
            <Button layoutX="56.0" layoutY="153.0" mnemonicParsing="false" text="Thoát" />
            <Button layoutX="353.0" layoutY="153.0" mnemonicParsing="false" onAction="#action" text="Đăng nhập" />
            <TextField fx:id="username" layoutX="144.0" layoutY="36.0" prefHeight="25.0" prefWidth="235.0" promptText="Tài khoản" />
            <PasswordField fx:id="password" layoutX="144.0" layoutY="88.0" prefWidth="235.0" promptText="Mật khẩu" />
         </children>
      </Pane>
   </children>
</fx:root>

```

Mình có lên mạng tìm hiểu nhưng nó chỉ hướng dẫn cùng resource  
Mình cũng đã thử đổi  
`fx:controller="/controllers/LoginController"`  
thành  
`fx:controller="./java/controllers/LoginController"`  
vẫn không được

---

<div class="post-metadata">

### Author: ![HieuDC](https://daynhauhoc.com/user_avatar/daynhauhoc.com/hieudc/32/56431_2.png) [@HieuDC](https://daynhauhoc.com/u/HieuDC)
#### Post date: [April 29, 2022, 6:13am UTC](https://daynhauhoc.com/t/javafx-khong-hien-thi-form/125503/2 "2022-04-29T06:13:43Z")

</div>

Thử sửa fx:controller thành “tenPackage.tenFileFXML”

---

<div class="post-metadata">

### Author: ![Nguyen\_Tuan\_Khai\_Kha](https://daynhauhoc.com/user_avatar/daynhauhoc.com/nguyen_tuan_khai_kha/32/47434_2.png) [@Nguyen\_Tuan\_Khai\_Kha](https://daynhauhoc.com/u/Nguyen_Tuan_Khai_Kha)
#### Post date: [April 29, 2022, 7:47am UTC](https://daynhauhoc.com/t/javafx-khong-hien-thi-form/125503/3 "2022-04-29T07:47:31Z")

</div>

Cảm ơn bạn mình đã fix được lỗi
