我正在尝试制作一个允许您向组中添加或删除成员的GUI。我是如何做到这一点的成员视图我创建了一个VBox布局来容纳所有成员。每个成员都有自己的网格布局。当用户按下添加成员时,它会将成员布局添加到VBox(该区域以查看所有成员)。问题是,当我按下删除成员时,它将删除我要删除的成员下面的成员,并且在最后一个成员上GUI会给出错误说"无法删除成员:索引2,大小2"。
主类
/**
* Main.java * *第7节经理的主要课程 * /
package sba.s7.Main;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import sba.s7.Main.AlertBox.ConfirmBox;
import sba.s7.Main.Panel.Menu;
import sba.s7.Main.Panel.Member.MemberInfo;
import sba.s7.Main.Panel.Member.MemberMenu;
public class Main extends Application {
/** Variables */
/* Imports */
private MemberInfo mem; // Member Info
/* Int */
public final int WIDTH = 715;
public final int HEIGHT = 700;
/* String */
private final String TITLE = "If there is no road, we make one";
/* Scenes */
private Stage window; // Window
private Scene mainScene; // Main Scene
private static VBox infoVbox; // VBox Layout
/** Start */
/*
* This is the first method to run when the GUI starts
*/
@Override
public void start(Stage stage) throws Exception {
window = stage;
/* Instantiates */
mem = new MemberInfo(0); // Member Info
/* Layouts */
// Main Layout
VBox vbox = new VBox(30);
vbox.setPadding(new Insets(5, 0, 0, 30));
// Info Layout
infoVbox = new VBox();
/*
* Scenes
*/
/* Menu */
vbox.getChildren().add(Menu.displayMenu());
/* Members Scene */
vbox.getChildren().add(MemberMenu.displayMenu()); // Member Menu
infoVbox.getChildren().add(mem.displayDir()); // Member Info
/* Info Panel */
vbox.getChildren().add(infoVbox);
/* Window */
mainScene = new Scene(vbox, WIDTH, HEIGHT); // Main Scene
window.setTitle(TITLE); // Title
window.setScene(mainScene); // Scene
window.setMinHeight(200); // Min Height
window.setMinWidth(350); // Min Width
window.setOnCloseRequest(e -> { // Close
e.consume();
close();
});
window.show(); // Show
}
/** Main Method */
/*
* The main method for the class
*/
public static void main(String[] args) {
launch(args);
}
/** Close */
/*
* Performs a proper app close when called
*/
private void close() {
boolean answer = ConfirmBox.display("Exit", "Are you sure you want to exit.");
if(answer) {
Platform.exit();
}
}
/** Getters */
/* infoBox */
public static VBox getLayout() {
return infoVbox;
}
}
菜单类(添加成员部分)
/**
* Menu.java
*
* This is the menu that is displayed at the top of the screen
*/
package sba.s7.Main.Panel;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import sba.s7.Main.Main;
import sba.s7.Main.Panel.Member.MemberInfo;
public class Menu {
/** Variables */
/* Imports */
private static MemberInfo mem;
/* Int */
private static int id = 1;
private static int memSize;
/** Display Menu */
/*
* Displays the menu when called
*/
public static FlowPane displayMenu() {
/* Window */
FlowPane lay = new FlowPane(); // GridPane
/*
* Components
*/
/* Button */
//------------------------------
// Add Member
//------------------------------
Button addMem = new Button();
addMem = new Button("Add a member");
// Action Event
addMem.setOnAction(e -> {
addMember();
});
lay.getChildren().add(addMem); // Adds addMem
return lay;
}
/** Add Member */
/*
* This will set the id & add a member
*/
private static void addMember() {
//----------------------------------------------
// Makes the id less than the layout size
//----------------------------------------------
if(id > Main.getLayout().getChildren().size()) {
for(int i = id; i > Main.getLayout().getChildren().size(); i--) {
id--;
}
}
//-----------------------------
// Adds a member
//-----------------------------
mem = new MemberInfo(id);
Main.getLayout().getChildren().add(id, mem.displayInfo());
memSize++; // Member size
//---------------------------------------------------------------
// This will set id to be less than the size of Main layout
//---------------------------------------------------------------
if(id < Main.getLayout().getChildren().size())
id++; // Id
}
/** Getters */
/* memSize */
public static int getMemSize() {
return memSize;
}
}
会员信息类(会员)
/**
* MemberInfo.java * *这将显示会员信息,如姓名,等级等。 * /
package sba.s7.Main.Panel.Member;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import sba.s7.Main.Main;
import sba.s7.Main.AlertBox.AlertBox;
import sba.s7.Main.File.SaveFile;
public class MemberInfo {
/** Variables */
/* Int */
private int id; // Id
private int row = 2; // Row
/** Constructor */
/*
* Constructor for the class
*/
public MemberInfo(int id) {
this.id = id; // Id
}
/** Display Info */
/*
* This will display member info to the scene when called
*/
public GridPane displayInfo() {
System.out.println("START: " + id);
/* Window */
GridPane gp = new GridPane(); // Grid Pane
gp.setHgap(69); // HGap
gp.setVgap(5);
/*
* Components
*/
/* Label */
//----------------------------
// Rank
//----------------------------
ChoiceBox<String> rank = new ChoiceBox<String>();
rank.getItems().addAll("XO", "SOC1", "SOC", "SO", "OP", "JP"); // Choices
rank.setValue("JP"); // Default Value
rank.setPrefWidth(100); // Width
GridPane.setConstraints(rank, 0, row); // Constraints
gp.getChildren().add(rank); // Adds Rank TextField
/* TextField */
//---------------------------
// Name
//---------------------------
TextField name = new TextField();
name.setPromptText("name"); // Prompt Text
GridPane.setConstraints(name, 2, row); // Constraints
gp.getChildren().add(name); // Adds Rank Label
/* Button */
//---------------------------
// Save Button
//---------------------------
Button save = new Button("Save");
save.setOnAction(e -> SaveFile.save(rank.getValue(), name.getText())); // Sets action event
GridPane.setConstraints(save, 3, row); // Constraints
gp.getChildren().add(save); // Adds the save button
//----------------------------
// Delete Member
//----------------------------
Button delMem = new Button("Remove Member");
delMem.setOnAction(e -> {
removeMember();
});
GridPane.setConstraints(delMem, 4, row); // Constraints
gp.getChildren().add(delMem); // Adds the delMem button
/* House Keeping */
return gp; // Returns the layout
}
/** Display Dir */
/*
* Displays the Director
*/
public GridPane displayDir() {
/* Window */
GridPane gp = new GridPane(); // Grid Pane
gp.setHgap(69); // HGap
gp.setVgap(5);
/*
* Components
*/
/* Label */
//----------------------------
// Rank
//----------------------------
ChoiceBox<String> rank = new ChoiceBox<String>();
rank.getItems().addAll("Dir"); // Choices
rank.setValue("Dir");
rank.setPrefWidth(100); // Width
GridPane.setConstraints(rank, 0, row); // Constraints
gp.getChildren().add(rank); // Adds Rank TextField
/* TextField */
//---------------------------
// Name
//---------------------------
TextField name = new TextField();
name.setPromptText("name"); // Prompt Text
GridPane.setConstraints(name, 2, row); // Constraints
gp.getChildren().add(name); // Adds Rank Label
/* Button */
//---------------------------
// Save Button
//---------------------------
Button save = new Button("Save");
save.setOnAction(e -> SaveFile.save(rank.getValue(), name.getText())); // Sets action event
GridPane.setConstraints(save, 3, row); // Constraints
gp.getChildren().add(save); // Adds the save button
/* House Keeping */
return gp; // Returns the layout
}
/** Remove Member */
/*
* This will remove a member when called
*/
private void removeMember() {
try {
//-------------------------------
// Removes the member
//-------------------------------
Main.getLayout().getChildren().remove(id);
}catch(Exception ex) {
AlertBox.display("ERROR", "Could not remove member: " + ex.getMessage());
}
}
/** Getter */
/* id */
public int getId() {
return id;
}
}