在JavaFX标签中显示格式化的字符串表格是否可能?

huangapple go评论119阅读模式
英文:

Is it possible to display a string formatted table in a javaFx label?

问题

public static void printDetails() {
    // 打印默认成员名单作为数组

    int i;
    System.out.println("  ");
    System.out.printf("%-20s%-20s%-25s%-25s%-25s%n", "索引", "姓名", "会员日期",
            "会员编号", "会员类型");
    System.out.println("------------------------------------------------------------");

    for (i = 0; i < defaultNames.size(); i++) {
        System.out.format("%-20s%-20s%-25s%-25s%-25s%n", i + 1, defaultNames.get(i),
                defaultMembershipDate.get(i), defaultMembershipNumber.get(i), defaultMemberType.get(i));
    }
}

在JavaFX标签中显示格式化的字符串表格是否可能?


<details>
<summary>英文:</summary>

    public static void printDetails() {
        //PRINTING DEFAULT MEMBERS NAMES AS AN ARRAY

        int i;
        System.out.println (&quot;  &quot;);
        System.out.printf (&quot;%-20s%-20s%-25s%-25s%-25s%n&quot;, &quot;Index&quot;, &quot;Name&quot;, &quot;membershipDate&quot;,
                &quot;membershipNumber&quot;,&quot;member type&quot;);
        System.out.println(&quot;------------------------------------------------------------&quot;)

        for (i = 0; i &lt; defaultNames.size (); i++) {
            System.out.format (&quot;%-20s%-20s%-25s%-25s%-25s%n&quot;, i + 1, defaultNames.get (I),
                    defaultMembershipDate.get (i), defaultMembershipNumber.get (i),defaultMemberType.get (i));
        }


    }


  ![](https://i.stack.imgur.com/nIDIW.jpg)

</details>


# 答案1
**得分**: 1

是的,可以在JavaFX标签中显示格式化的字符串表格,但如James_D所说,它可能不会看起来很好。你应该使用一个`TableView`,它会更干净,也会使你的代码更整洁,而不是使用4个不同的`ArrayList`,你最终会有一个包含对象的`ArrayList`,这些对象包含了它们自己的数据,然后你可以将这些对象传递给`TableView`,让它为你处理工作。

在将来/当你为任何项目编写代码时,你应该将`MembershipData`更新为正确的数据类型,例如,你可以将`startDate`更改为`LocalDate`,并显然将Number更改为`Integer`。

它最终会看起来像这样:

```java
public class Main extends Application {

    @Override
    public void start(Stage stage) {
        ArrayList<MembershipData> membershipDataArrayList = new ArrayList<>();
        for (int i = 1; i < 5; i++) {
            String letter = String.valueOf((char) (i + 64));
            membershipDataArrayList.add(new MembershipData(letter, ""+i+i+i, ""+i+i+i, letter));
        }

        showDetails(membershipDataArrayList);
    }

    public void showDetails(ArrayList<MembershipData> membershipDataArrayList) {

        System.out.println ("  ");
        System.out.printf ("%-20s%-20s%-25s%-25s%-25s%n", "Index", "Name", "membershipDate",
                "membershipNumber","member type");
        System.out.println("------------------------------------------------------------");

        int i = 0;
        for (MembershipData membershipData : membershipDataArrayList) {
            System.out.format ("%-20s%-20s%-25s%-25s%-25s%n", i++, membershipData.getName(),
                    membershipData.getStartDate(), membershipData.getNumber() ,membershipData.getType());
        }

        TableColumn<MembershipData, String> column1 = new TableColumn<>("Name");
        column1.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<MembershipData, String> column2 = new TableColumn<>("MemberShip Date");
        column2.setCellValueFactory(new PropertyValueFactory<>("startDate"));

        TableColumn<MembershipData, String> column3 = new TableColumn<>("MemberShip Number");
        column3.setCellValueFactory(new PropertyValueFactory<>("number"));

        TableColumn<MembershipData, String> column4 = new TableColumn<>("MemberShip Type");
        column4.setCellValueFactory(new PropertyValueFactory<>("type"));

        TableView<MembershipData> tableView = new TableView<>();
        tableView.getColumns().addAll(column1, column2, column3, column4);

        tableView.getItems().addAll(membershipDataArrayList);

        VBox vBox = new VBox(tableView);
        vBox.setAlignment(Pos.CENTER);

        Stage stage  = new Stage();
        stage.setWidth(475);
        stage.setScene(new Scene(vBox));
        stage.show();
    }
}

这是你的会员类的样子

```java
public class MembershipData{
    private final String name;
    private final String startDate;
    private final String number;
    private final String type;

    public MembershipData(String name, String startDate, String number, String type){
        this.name = name;
        this.startDate = startDate;
        this.number = number;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public String getStartDate() {
        return startDate;
    }

    public String getNumber() {
        return number;
    }

    public String getType() {
        return type;
    }
}
英文:

Yes it is possible to display a string formatted table in a javaFx label but it will not look very nice as James_D said you should use a TableView it will look much cleaner and it will help keep you code cleaner as well instead of using 4 different ArrayLists you will end up have 1 ArrayList with objects containing their own data from there you can pass those objects to the TableView and let it handle the work for you

In the future/when your coding this for whatever you are working on you should update the MembershipData to the correct data types for example you could change startDate to be LocalDate and obviously change the Number to be an Integer

It would end up looking something like this

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        ArrayList&lt;MembershipData&gt; membershipDataArrayList = new ArrayList&lt;&gt;();
        for (int i = 1; i &lt; 5; i++) {
            String letter = String.valueOf((char) (i + 64));
            membershipDataArrayList.add(new MembershipData(letter, &quot;&quot;+i+i+i, &quot;&quot;+i+i+i, letter));
        }

        showDetails(membershipDataArrayList);
    }

    public void showDetails(ArrayList&lt;MembershipData&gt; membershipDataArrayList) {

        System.out.println (&quot;  &quot;);
        System.out.printf (&quot;%-20s%-20s%-25s%-25s%-25s%n&quot;, &quot;Index&quot;, &quot;Name&quot;, &quot;membershipDate&quot;,
                &quot;membershipNumber&quot;,&quot;member type&quot;);
        System.out.println(&quot;------------------------------------------------------------&quot;);

        int i = 0;
        for (MembershipData membershipData : membershipDataArrayList) {
            System.out.format (&quot;%-20s%-20s%-25s%-25s%-25s%n&quot;, i++, membershipData.getName(),
                    membershipData.getStartDate(), membershipData.getNumber() ,membershipData.getType());
        }

        TableColumn&lt;MembershipData, String&gt; column1 = new TableColumn&lt;&gt;(&quot;Name&quot;);
        column1.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;name&quot;));

        TableColumn&lt;MembershipData, String&gt; column2 = new TableColumn&lt;&gt;(&quot;MemberShip Date&quot;);
        column2.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;startDate&quot;));

        TableColumn&lt;MembershipData, String&gt; column3 = new TableColumn&lt;&gt;(&quot;MemberShip Number&quot;);
        column3.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;number&quot;));

        TableColumn&lt;MembershipData, String&gt; column4 = new TableColumn&lt;&gt;(&quot;MemberShip Type&quot;);
        column4.setCellValueFactory(new PropertyValueFactory&lt;&gt;(&quot;type&quot;));

        TableView&lt;MembershipData&gt; tableView = new TableView&lt;&gt;();
        tableView.getColumns().addAll(column1, column2, column3, column4);

        tableView.getItems().addAll(membershipDataArrayList);

        VBox vBox = new VBox(tableView);
        vBox.setAlignment(Pos.CENTER);

        Stage stage  = new Stage();
        stage.setWidth(475);
        stage.setScene(new Scene(vBox));
        stage.show();
    }
}

This is what your membership class would look like

public class MembershipData{
    private final String name;
    private final String startDate;
    private final String number;
    private final String type;

    public MembershipData(String name, String startDate, String number, String type){
        this.name = name;
        this.startDate = startDate;
        this.number = number;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public String getStartDate() {
        return startDate;
    }

    public String getNumber() {
        return number;
    }

    public String getType() {
        return type;
    }
}

huangapple
  • 本文由 发表于 2020年7月29日 21:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63154832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定