有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java javafx应用程序生成但不会运行,没有显示错误

我构建了一个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);
    }
}

下面是person类的来源:

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();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    在Intellij Idea 2018.2.3中测试了您的代码,并得出下一个结果: enter image description here