gpt4 book ai didi

java - 使用下一个和上一个按钮显示 ArrayList Javafx

转载 作者:行者123 更新时间:2023-12-01 19:53:43 25 4
gpt4 key购买 nike

我正在编写一个程序,其中创建一个名为 City 的对象的 Arraylist

    City c1 = new City("Miami", "Florida", 33175, -80.4068, 25.7341);
City c2 = new City("Seattle", "Washington", 98109, -122.3476, 47.6339);
City c3 = new City("Levittown", "Pennsylvania", 19058, -75.1028, 40.3286);
City c4 = new City("Tampa", "Florida", 33625, -82.559, 28.0726);
City c5 = new City("Topeka", "Kansas", 66617, -95.6384, 39.1271);

ArrayList<City> cities = new ArrayList<City>();

cities.add(c1);
cities.add(c2);
cities.add(c3);
cities.add(c4);
cities.add(c5);

我需要使用下一个和上一个按钮将其一一显示。目前我有一个 for 循环遍历 ArrayList 创建标签和文本字段并从增量(i)获取类数据这是代码:

 for (int i = 0; i < cities.size(); i++) {


GridPane pane = new GridPane();
pane.setHgap(5);
pane.setVgap(5);
pane.setPadding(new Insets(25,25,25,25));


Label label1 = new Label("City:");
TextField textField1 = new TextField (cities.get(i).getCity());
pane.add(label1, 0, 1);
pane.add(textField1, 1, 1);

Label label2 = new Label("State:");
TextField textField2 = new TextField (cities.get(i).getState());
pane.add(label2, 0, 2);
pane.add(textField2, 1, 2);

Label label3 = new Label("Zip Code:");
TextField textField3 = new TextField (String.valueOf(cities.get(i).getpostalCode()));
pane.add(label3, 0, 3);
pane.add(textField3, 1, 3);

Label label4 = new Label("Longitude:");
TextField textField4 = new TextField (String.valueOf(cities.get(i).getLongitude()));
pane.add(label4, 0, 4);
pane.add(textField4, 1, 4);

Label label5 = new Label("Longitude:");
TextField textField5 = new TextField (String.valueOf(cities.get(i).getLatitude()));
pane.add(label5, 0, 5);
pane.add(textField5, 1, 5);

Button prev = new Button("Previous");
pane.add(prev, 0, 20);

Button next = new Button("Next");
pane.add(next, 2, 20);

// buttons are work in progress
next.setOnAction((ActionEvent e) -> {


cities.get(i).getCity();
textField1.setText(cities.get(i).getCity());



});

BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);

Scene scene = new Scene(borderPane, 500, 500);
primaryStage.setTitle("Cities");
primaryStage.setScene(scene);
primaryStage.show();

}

我可以在没有 ForLoop 的情况下显示它,但我不知道如何从这里开始。另外,如何在循环内实现上一个/下一个按钮 setOnActions?

谢谢!

最佳答案

最好将用于显示城市的节点移至新类。这样您就可以更轻松地分离关注点。 (实现导航时无需考虑城市显示,反之亦然。)

public class CityView extends GridPane {

public CityView() {
setHgap(5);
setVgap(5);
setPadding(new Insets(25, 25, 25, 25));

Label label1 = new Label("City:");
TextField cityText = new TextField();

Label label2 = new Label("State:");
TextField stateText = new TextField();

Label label3 = new Label("Zip Code:");
TextField zipCodeText = new TextField();

Label label4 = new Label("Longitude:");
TextField longitudeText = new TextField();

Label label5 = new Label("Longitude:");
TextField latitudeText = new TextField();

addColumn(0, label1, label2, label3, label4, label5);
addColumn(1, cityText, stateText, zipCodeText, longitudeText, latitudeText);

this.city.addListener((o, oldValue, newValue) -> {
// fill TextFields with new city values
String name = null;
String state = null;
String zipCode = null;
String longitude = null;
String latitude = null;

if (city != null) {
name = newValue.getCity();
state = newValue.getState();
zipCode = String.valueOf(newValue.getpostalCode());
longitude = String.valueOf(newValue.getLongitude());
latitude = String.valueOf(newValue.getLatitude());
}

cityText.setText(name);
stateText.setText(state);
zipCodeText.setText(zipCode);
longitudeText.setText(longitude);
latitudeText.setText(latitude);
});
}

private final ObjectProperty<City> city = new SimpleObjectProperty<>(this, "city");

public ObjectProperty<City> cityProperty() {
return city;
}

public City getCity() {
return cityProperty().get();
}

public void setCity(City city) {
cityProperty().set(city);
}

}
@Override
public void start(Stage primaryStage) {
ObservableList<City> cities = FXCollections.observableArrayList(
new City("Miami", "Florida", 33175, -80.4068, 25.7341),
new City("Seattle", "Washington", 98109, -122.3476, 47.6339),
new City("Levittown", "Pennsylvania", 19058, -75.1028, 40.3286),
new City("Tampa", "Florida", 33625, -82.559, 28.0726),
new City("Topeka", "Kansas", 66617, -95.6384, 39.1271));

IntegerProperty index = new SimpleIntegerProperty();
IntegerBinding displayIndex = index.add(1);
IntegerBinding size = Bindings.size(cities);
size.addListener((o, oldValue, newValue) -> {
// keep index valid when cities are removed (not necessary for lists that are not modified)
int newSize = newValue.intValue();
int i = index.get();
if (i+1 >= newSize) {
index.set(Math.max(0, newSize - 1));
}
});

CityView cityView = new CityView();
cityView.cityProperty().bind(Bindings.valueAt(cities, index));

Button last = new Button("<");
last.setOnAction(evt -> {
index.set(index.get() - 1);
});
last.disableProperty().bind(index.lessThanOrEqualTo(0));

Button next = new Button(">");
next.setOnAction(evt -> {
index.set(index.get() + 1);
});
next.disableProperty().bind(displayIndex.greaterThanOrEqualTo(size));

Label indexLabel = new Label();
indexLabel.setMaxSize(Region.USE_PREF_SIZE, Double.MAX_VALUE);
indexLabel.setAlignment(Pos.CENTER_RIGHT);
indexLabel.setPrefWidth(20);
indexLabel.textProperty().bind(displayIndex.asString());

HBox navigation = new HBox(10, last, indexLabel, next);
navigation.setFillHeight(true);

VBox root = new VBox(cityView, navigation);

Scene scene = new Scene(root);

primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}

关于java - 使用下一个和上一个按钮显示 ArrayList Javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50361701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com