代码应该创建一个好友列表,并允许用户使用相应的按钮添加和删除他们。添加好友时,用户输入自己的信息,删除好友时,选择的好友从列表中移除。
问题是 FXML 文件无法使用 FriendsBook 类中的函数。控制器在 FXML 文件中正确声明,目录结构似乎正确。但是,控制器类中的函数未被识别。此外,IntelliJ 生成的原始代码已被删除,但这似乎不是问题的原因。
如果您需要更多信息,可以发表评论,我会尽快回复!
文件名正是它们在每个部分上方的拼写方式。
我正在使用 JDK 17 和 JavaFX 16,Scene Builder。
FriendsBook.java
package com.example.friendsbook.friendsbook;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FriendsBook extends Application {
private List<Friend> friends = new ArrayList<>();
@Override
public void start(Stage primaryStage) throws Exception {
// load the FXML file
FXMLLoader loader = new FXMLLoader(getClass().getResource("friendsbook.fxml"));
Parent root = loader.load();
// get the controller
FriendsBookController controller = loader.getController();
controller.setFriendsBook(this);
// set the scene
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Friends Book");
// show the stage
primaryStage.show();
}
// add a new friend to the list
public void addFriend(Friend friend) {
friends.add(friend);
}
// remove a friend from the list
public void removeFriend(Friend friend) {
friends.remove(friend);
}
// get the list of friends
public List<Friend> getFriends() {
return friends;
}
public static void main(String[] args) {
launch(args);
}
}
FriendsBookController.java
package com.example.friendsbook.friendsbook;
import java.io.IOException;
import java.util.List;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
public class FriendsBookController {
private FriendsBook friendsBook;
@FXML
private ListView<Friend> friendsList;
@FXML
private Button addButton;
@FXML
private Button deleteButton;
@FXML
private TextField nameField;
@FXML
private TextField ageField;
@FXML
private TextField phoneField;
@FXML
private Label nameLabel;
@FXML
private Label ageLabel;
@FXML
private Label phoneLabel;
public void setFriendsBook(FriendsBook friendsBook) {
this.friendsBook = friendsBook;
// initialize the list view
friendsList.getItems().addAll(friendsBook.getFriends());
// set the listener for the selection change
friendsList.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showFriendDetails(newValue));
}
@FXML
private void handleAddFriend() throws IOException {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String phone = phoneField.getText();
Friend friend = new Friend(name, age, phone);
friendsBook.addFriend(friend);
friendsList.getItems().add(friend);
clearFields();
}
@FXML
private void handleDeleteFriend() throws IOException {
Friend friend = friendsList.getSelectionModel().getSelectedItem();
if (friend != null) {
friendsBook.removeFriend(friend);
friendsList.getItems().remove(friend);
clearFields();
}
}
private void showFriendDetails(Friend friend) {
if (friend != null) {
nameLabel.setText(friend.getName());
ageLabel.setText(Integer.toString(friend.getAge()));
phoneLabel.setText(friend.getPhone());
} else {
clearFields();
}
}
private void clearFields() {
nameField.setText("");
ageField.setText("");
phoneField.setText("");
}
}
friendsbook.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.friendsbook.friendsbook.FriendsBookController">
<children>
<VBox layoutX="172.0" layoutY="31.0" prefHeight="200.0" prefWidth="256.0">
<children>
<Label text="Add Friend" />
<HBox spacing="10.0">
<children>
<Label text="Name" />
<TextField fx:id="nameField" />
</children>
</HBox>
<HBox spacing="10.0">
<children>
<Label text="Age" />
<TextField fx:id="ageField" />
</children>
</HBox>
<HBox spacing="10.0">
<children>
<Label text="Phone" />
<TextField fx:id="phoneField" />
</children>
</HBox>
<Button fx:id="addButton" mnemonicParsing="false" onAction="#handleAddFriend" text="Add" />
</children>
</VBox>
<VBox layoutX="172.0" layoutY="242.0" prefHeight="141.0" prefWidth="256.0">
<children>
<Label text="Selected Friend" />
<HBox spacing="10.0">
<children>
<Label text="Name" />
<Label fx:id="nameLabel" />
</children>
</HBox>
<HBox spacing="10.0">
<children>
<Label text="Age" />
<Label fx:id="ageLabel" />
</children>
</HBox>
<HBox spacing="10.0">
<children>
<Label text="Phone" />
<Label fx:id="phoneLabel" />
</children>
</HBox>
</children>
</VBox>
<ListView fx:id="friendsList" layoutX="14.0" layoutY="14.0" prefHeight="371.0" prefWidth="146.0" />
<Button fx:id="deleteButton" layoutX="492.0" layoutY="356.0" mnemonicParsing="false" onAction="#handleDeleteFriend" text="Delete" />
</children>
</AnchorPane>
我使用的解决方案:
检查项目中的包名和文件结构是否与FXML文件中使用的包名匹配。 确保 FXML 文件中的 fx:id 属性与控制器类中相应的 @FXML 注释字段的名称相匹配。 已验证控制器类是否在正确的包中定义。 注释掉 IntelliJ 放置的原始代码并使用代码示例中的名称创建新文件。
预期结果:
FXML文件要配合FriendsBook类中的函数,程序要能创建好友列表,添加删除,添加好友时带用户到输入好友信息的版块。