目标:让用户输入和输出半随机锻炼。
问题:我已经得到代表锻炼的输出,循环和所有随机化实际锻炼的内容都无法由动作处理程序看到。我可以在控制台行中输出实际锻炼,但不能在GUI中输出。
我已经尝试了几乎所有内容,从重做代码到移动代码。我只需要当用户单击随机按钮时对操作处理程序可见的val变量。
我将代码分离出来,以便于阅读。第一部分是GUI和选择内容。第二组代码是在例如ChestDay中进行随机化的代码。当我移动第二个代码片段时,该代码似乎也变得非常敏感。
我真的可以使用所有建议,还有几天可以解决此问题。
我需要在两个摘要中看到的变量是“ val”变量。
package Final;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.text.Text;
public class FinalGUI extends Application {
private static final int range = 6;
// launch the application
public void start(Stage stage) {
try {
// set title for the stage
stage.setTitle("Your Work Out Friend");
// create a VBox
VBox vbox = new VBox();
Text text = new Text();
ComboBox choiceMuscle = new ComboBox();
ComboBox choiceRep = new ComboBox();
// create a label
Label label = new Label("Choose your muscle group and your workout goal.");
// Creates a list that the vbox can use
ObservableList<String> choicesMuscle = FXCollections.observableArrayList(
"Chest day", "Back day", "Leg day");
choiceMuscle.setItems(choicesMuscle);
ObservableList<String> choicesRep = FXCollections.observableArrayList(
"Lose weight", "Gain muscle mass", "Gain Strength");
choiceRep.setItems(choicesRep);
// add label to vbox
vbox.getChildren().add(label);
// add buttons to VBox
vbox.getChildren().addAll(choiceMuscle, choiceRep, text);
Button btnOK = new Button();
btnOK.setText("Give me a workout!");
vbox.getChildren().add(btnOK);
btnOK.setOnAction(a -> {
String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem() + "";
if (cMuscle.equalsIgnoreCase("Chest Day")) {
printArray(chestDay());
}
else if (cMuscle.equalsIgnoreCase("Leg Day")) {
printArray(legDay());
}
else if (cMuscle.equalsIgnoreCase("Back Day")) {
printArray(backDay());
}
//else {
// System.out.println("Invalid choice made. Choose between Chest, Leg or Back days only.");
//}
//});
String cRep = choiceRep.getSelectionModel().getSelectedItem() + "";
String repChoice = "";
//Move code from WorkOutRep here
if (cRep.equalsIgnoreCase("Lose Weight")) {
repChoice = "5 sets of 20 reps, with 1 minute of cardio in between";
}
else if (cRep.equalsIgnoreCase("Gain muscle mass")) {
repChoice = "4 sets of 10 reps, or 5 sets of 8 reps";
}
else if (cRep.equalsIgnoreCase("Gain strength")) {
repChoice = "5 sets of 3 reps, increasing the weight until you can no longer lift the weight.";
}
//else {
// System.out.println("Invalid choice made. Choose between the listed choices only.");
//}
//});
text.setText("Your workout is: " + repChoice + val );
});
// create a scene
Scene scene = new Scene(vbox, 400, 200);
// set the scene
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static String[] backDay() {
List<String> backList = new ArrayList <>();
backList.add("Deadlift");
backList.add("Pull Up");
backList.add("Back Rows");
backList.add("Bend-Over Barbell Rows");
backList.add("Romanian Deadlift");
backList.add("Front Squat");
return randomizerHelper(backList);
}
public String[] chestDay() {
List <String> chestList = new ArrayList <>();
chestList.add("Bench Press");
chestList.add("Incline Press");
chestList.add("Dip");
chestList.add("Flys");
chestList.add("Reverse Flys");
chestList.add("Supine Press");
return randomizerHelper(chestList);
}
public String[] legDay() {
List<String> legList = new ArrayList <>();
legList.add("Squat");
legList.add("Leg Press");
legList.add("Leg Extension");
legList.add("Dumbbell Step Up");
legList.add("Body Weight Calf Raises");
legList.add("Walking Lunge");
return randomizerHelper(legList);
}
public static String[] randomizerHelper(List<String> arr) {
String[] returnArray = new String[3];
String chosen;
for(int i = 0; i < returnArray.length; i++) {
chosen = arr.get((int) (Math.random() * range));
for(int j = 0; j < returnArray.length; j++) {
if (!(chosen.equalsIgnoreCase(returnArray[j]))) {
returnArray[i]= chosen;
break;
}
}
}
return returnArray;
}
public void printArray(String[] arr) {
for (String val : arr) {
Text text = new Text();
text.setText(val);
}
}
// Main Method
public static void main(String args[]) {
// launch the application
Application.launch(args);
}
}
最佳答案
您的printArray()
方法创建新的Text
节点,但永远不会将其添加到场景图中。只设置了最后一个字符串,没有看到。相反,您可以创建一个字符串,其中包含针对特定肌肉群的随机锻炼列表。然后可以将结果字符串添加到现有的Text
中。在下面的变体中
方法randomExercises()
调用Collections.shuffle()
将列表随机化,然后选择合适的subList()
。然后,按钮处理程序可以调用selectMuscles()
并将相关的muscleChoice
添加到text
。
muscleChoice = selectMuscles(…);
repChoice = …;
text.setText("Your workout: " + "\n" + muscleChoice + "\n" + repChoice);
每个肌肉群的运动清单应该只创建一次;作为类成员,列表对类方法可见。
private final List<String> backList = new ArrayList<>();
private final List<String> chestList = new ArrayList<>();
private final List<String> legList = new ArrayList<>();
…
private String[] backDay() {
if (backList.isEmpty()) {
backList.add("Deadlift");
…
}
return randomExercises(backList);
}
…
在general中,除非特别需要更广泛的访问,否则类方法应为
private
。为了更安全的操作,avoid raw types;请注意,不需要强制转换或隐式转换。
ComboBox<String> choiceMuscle = new ComboBox<>();
String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem();
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.text.Text;
/** @see https://stackoverflow.com/a/59334974/230513 */
public class FinalGUI extends Application {
private final List<String> backList = new ArrayList<>();
private final List<String> chestList = new ArrayList<>();
private final List<String> legList = new ArrayList<>();
@Override
public void start(Stage stage) {
stage.setTitle("Your Work Out Friend");
Label label = new Label("Choose your muscle group and your workout goal.");
ComboBox<String> choiceMuscle = new ComboBox<>();
ObservableList<String> choicesMuscle = FXCollections.observableArrayList(
"Chest day", "Back day", "Leg day");
choiceMuscle.setItems(choicesMuscle);
choiceMuscle.getSelectionModel().select(0);
ComboBox<String> choiceRep = new ComboBox<>();
ObservableList<String> choicesRep = FXCollections.observableArrayList(
"Lose weight", "Gain muscle mass", "Gain Strength");
choiceRep.setItems(choicesRep);
choiceRep.getSelectionModel().select(0);
Button btnOK = new Button("Give me a workout!");
Text text = new Text("\n\n\n");
VBox vbox = new VBox(8);
vbox.setPadding(new Insets(8));
vbox.getChildren().addAll(label, choiceMuscle, choiceRep, btnOK, text);
btnOK.setOnAction(a -> {
String cMuscle = choiceMuscle.getSelectionModel().getSelectedItem();
String muscleChoice = "";
if (cMuscle.equalsIgnoreCase("Chest Day")) {
muscleChoice = selectMuscles(chestDay());
} else if (cMuscle.equalsIgnoreCase("Leg Day")) {
muscleChoice = selectMuscles(legDay());
} else if (cMuscle.equalsIgnoreCase("Back Day")) {
muscleChoice = selectMuscles(backDay());
}
String cRep = choiceRep.getSelectionModel().getSelectedItem();
String repChoice = "";
if (cRep.equalsIgnoreCase("Lose Weight")) {
repChoice = "5 sets of 20 reps, with 1 minute of cardio between";
} else if (cRep.equalsIgnoreCase("Gain muscle mass")) {
repChoice = "4 sets of 10 reps, or 5 sets of 8 reps";
} else if (cRep.equalsIgnoreCase("Gain strength")) {
repChoice = "5 sets of 3 reps, increasing the weight\n"
+ "until you can no longer lift the weight.";
}
text.setText("Your workout: " + "\n" + muscleChoice + "\n" + repChoice);
});
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
private String[] backDay() {
if (backList.isEmpty()) {
backList.add("Deadlift");
backList.add("Pull Up");
backList.add("Back Rows");
backList.add("Bend-Over Barbell Rows");
backList.add("Romanian Deadlift");
backList.add("Front Squat");
}
return randomExercises(backList);
}
private String[] chestDay() {
if (chestList.isEmpty()) {
chestList.add("Bench Press");
chestList.add("Incline Press");
chestList.add("Dip");
chestList.add("Flys");
chestList.add("Reverse Flys");
chestList.add("Supine Press");
}
return randomExercises(chestList);
}
private String[] legDay() {
if (legList.isEmpty()) {
legList.add("Squat");
legList.add("Leg Press");
legList.add("Leg Extension");
legList.add("Dumbbell Step Up");
legList.add("Body Weight Calf Raises");
legList.add("Walking Lunge");
}
return randomExercises(legList);
}
private String[] randomExercises(List<String> arr) {
Collections.shuffle(arr);
return arr.subList(0, 3).toArray(new String[3]);
}
private String selectMuscles(String[] arr) {
return (Arrays.toString(arr));
}
public static void main(String args[]) {
Application.launch(args);
}
}