JavaFX在盒子问题中创建另一个盒子

时间:2018-12-09 13:08:00

标签: javafx

在框内创建框时,我需要帮助。如果运行我的代码,则会弹出“ JavaFX课程注册”框。

我想在自己的方框中同时包含“您当前正在注册”和“当前总学时”两个文本区域。从下拉菜单中选择课程后,这还将包括输出。

预先感谢您的帮助。

 import javafx.application.Application;
 import javafx.event.ActionEvent;
 import javafx.scene.layout.Region;
 import javafx.event.EventHandler;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.layout.StackPane;
 import javafx.stage.Stage;
 import javafx.scene.layout.GridPane;
 import javafx.geometry.Insets;
 import javafx.scene.control.Label;
 import javafx.scene.text.Font;
 import javafx.scene.text.FontPosture;
 import javafx.scene.text.FontWeight;
 import javafx.scene.image.ImageView;
 import javafx.scene.paint.Color;
 import javafx.geometry.HPos;
 import javafx.geometry.VPos;
 import javafx.scene.layout.RowConstraints;
 import javafx.scene.control.ComboBox;
 import javafx.scene.control.TextField;


 public class JavaFXRegister extends Application {

 GridPane grid = new GridPane();
 Label selectPromptLabel = new Label("Please select a course for which you 
 want to register");
 ComboBox<Course> coursesComboBox = new ComboBox<>();   
 Label confirmPromptLabel = new Label("");
Label registeredCoursesPromptLabel = new Label("You are currently registered 
 for");
Label creditHourPromptLabel = new Label("Current total Credit Hours");            
Label registeredCoursesLabel = new Label("");
Label creditHoursLabel = new Label("0");

Course choice;
int totalCredit = 0;


@Override
public void start(Stage primaryStage) {

  /*
    grid.setHgap(0);
    grid.setVgap(500);

    grid.setGridLinesVisible(true);
  */
    RowConstraints row0 = new RowConstraints();
    RowConstraints row1 = new RowConstraints();
    RowConstraints row2 = new RowConstraints();
    RowConstraints row3 = new RowConstraints();
    RowConstraints row4 = new RowConstraints();


    row0.setPercentHeight(5);
    row1.setPercentHeight(50);
    row2.setPercentHeight(10);
    row3.setPercentHeight(5);
    row4.setPercentHeight(15);

    grid.getRowConstraints().addAll(row0, row1,row2, row3, row4);

    grid.setAlignment(Pos.CENTER);

    grid.setHgap(5);
    grid.setVgap(5);

    //Ron Added
    grid.setPadding(new Insets(10, 10, 10, 10));  

    grid.add(selectPromptLabel, 0, 0);
    grid.setHalignment(selectPromptLabel, HPos.LEFT);

    //Ron Added
    selectPromptLabel.setMinWidth(Region.USE_PREF_SIZE);


    coursesComboBox.getItems().addAll(
            new Course("IT4782"), 
            new Course("IT4784"),
            new Course("IT4786"),
            new Course("IT4789"),
            new Course("IT2230"),
            new Course("IT3345"),
            new Course("IT3349") );
    coursesComboBox.setMaxWidth(Double.MAX_VALUE);

    grid.add(coursesComboBox, 0, 1);
    grid.setValignment(coursesComboBox, VPos.TOP);

    confirmPromptLabel.setTextFill(Color.RED);

    grid.add(confirmPromptLabel, 0, 2);  
    grid.setHalignment(confirmPromptLabel, HPos.LEFT);
    grid.setValignment(confirmPromptLabel, VPos.TOP);

    grid.add(registeredCoursesPromptLabel, 0, 3);  
    grid.setHalignment(registeredCoursesPromptLabel, HPos.LEFT);
    grid.setValignment(registeredCoursesPromptLabel, VPos.TOP);

    grid.add(creditHourPromptLabel, 1, 3);  
    grid.setHalignment(creditHourPromptLabel, HPos.LEFT);   
    grid.setValignment(creditHourPromptLabel, VPos.TOP);

    grid.add(registeredCoursesLabel, 0, 4);
    grid.setHalignment(registeredCoursesLabel, HPos.LEFT);   
    grid.setValignment(registeredCoursesLabel, VPos.TOP);
    registeredCoursesLabel.setStyle("-fx-background-color: #fff600;");

    grid.add(creditHoursLabel, 1, 4); 
    grid.setHalignment(creditHoursLabel, HPos.LEFT);   
    grid.setValignment(creditHoursLabel, VPos.TOP);
    creditHoursLabel.setStyle("-fx-background-color: #fff600;");

    Scene scene = new Scene(grid, 500, 500, Color.RED);

    primaryStage.setTitle("JavaFX Register for Course");
    primaryStage.setScene(scene);
    primaryStage.show();


    coursesComboBox.setOnAction(e -> {

        //Ron Added 
        coursesComboBox.getValue();
        choice = coursesComboBox.getValue();

        //Ron added
        String setCode = choice.toString();
        // Update display of registered courses 
        if(choice.getIsRegisteredFor()){
            confirmPromptLabel.setText("**Invalid** \n" + "You are already 
   registered for " + setCode);
            return;
        }
        // Update display of the total credit hours 
        if(totalCredit >= 9){
            confirmPromptLabel.setText("**Invalid** \n" + "You can't 
    register for anymore courses.");
            return;
        }
        //display message
        confirmPromptLabel.setText("You have registered for " + setCode );
        registeredCoursesLabel.setText(registeredCoursesLabel.getText() + 
    setCode + "\n");
        totalCredit += 3;
        creditHoursLabel.setText(totalCredit + "");
        choice.setIsRegisteredFor(true);
    });
   }


   public static void main(String[] args) {
    launch(args);
  }

 }
  Course Class

 package javafxregisterforcourse;

 public class Course {

private String code = "";
private boolean isRegisterdFor = false;

public Course(String code){
    this.code = code;
}

public void setCode(String code){
    this.code = code;
}

public String getCode() {
    return this.code;
}

public void setIsRegisteredFor(boolean trueOrFalse){
    this.isRegisterdFor = trueOrFalse;
}

public boolean getIsRegisteredFor() {
    return this.isRegisterdFor;
} 

@Override
public String toString(){
    return this.getCode();
 }
 }

0 个答案:

没有答案