英文:
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));
}
}
<details>
<summary>英文:</summary>
public static void printDetails() {
//PRINTING DEFAULT MEMBERS NAMES AS AN ARRAY
int i;
System.out.println (" ");
System.out.printf ("%-20s%-20s%-25s%-25s%-25s%n", "Index", "Name", "membershipDate",
"membershipNumber","member type");
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));
}
}
![](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 ArrayList
s 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<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();
}
}
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论