我构建了一个Java应用程序以将名称打印到应用程序中。我相信这些东西很好,但是我使用javafx将其制作为应用程序,并且在运行它时进行了构建,仅此而已,我不确定为什么。
我有两个班级,一个要上课的主要班级,另一个是要升入主要班级以培养人民的人员班级。
这是主要的代码:
//Carmine, Rohit
package project5;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author finst
*/
public class Project5a extends Application {
TextArea textAreaResults;
Button buttonShowNames;
StringBuilder output;
@Override
public void start(Stage primaryStage) {
textAreaResults = new TextArea();
textAreaResults.setPrefSize(380, 375);
textAreaResults.setEditable(false);
buttonShowNames = new Button("Show Names");
buttonShowNames.setStyle("-fx-text-fill: white; -fx-background-color: midnightblue; -fx-font-weight: bold;");
buttonShowNames.setOnAction(e -> NamesList(e));
VBox vBoxResults = new VBox(10, textAreaResults, buttonShowNames);
vBoxResults.setPadding(new Insets(10));
vBoxResults.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBoxResults, 400, 450);
primaryStage.setTitle("Names");
primaryStage.setScene(scene);
primaryStage.show();
}
private void NamesList(ActionEvent event) {
output = new StringBuilder();
List<Person> list = new ArrayList();
list.add(new Person("John", 'H', "Johnson"));
list.add(new Person("Kaitlyn", 'A', "Indigo"));
list.add(new Person("Wesley", 'T', "Collins"));
list.add(new Person("Emma", 'Z', "Watson"));
output.append("Names:\n");
displayList(list);
textAreaResults.setText(output.toString());
}
public void displayList(List list) {
Iterator personIterator = list.iterator();
while (personIterator.hasNext()) {
output.append(personIterator.next());
}
}
public static void main(String[] args) {
launch(args);
}
}
这是个人类的来源:
package project5;
/**
* @author finst
*/
public class Person {
String firstName;
String lastName;
char middleInitial;
public Person() {
this("", '0', "");
}
public Person(String firstName, char middleInitial, String lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public char getMiddleInitial() {
return middleInitial;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
StringBuilder output = new StringBuilder();
output.append("First Name: ");
output.append(getFirstName());
output.append(" Middle Initial: ");
output.append(getMiddleInitial());
output.append(" Last Name: ");
output.append(getLastName());
output.append("\n");
return output.toString();
}
}