如何从对象数组列表中获取项目

时间:2018-12-07 07:56:41

标签: arraylist javafx-8 textfield

那我怎么办才能“打开”我的约会对象数组列表? 我尝试了迭代,但是它得到2个正确的显示,或重复最后一个条目。我需要将文本字段设置为对象数组内的变量。我该怎么办?这是一个将约会保存到数组列表中的日历,该列表是该列表的对象类型,称为约会。我已经快要完成了,以至于无法在文本字段中显示或设置信息。

import java.awt.Color;
import java.util.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;



public class Calendario extends Application {

    private TextField s = new TextField();
    private TextField m = new TextField();
    private TextField t = new TextField();
    private TextField w = new TextField();
    private TextField th = new TextField();
    private TextField f = new TextField();
    private TextField st = new TextField();
    private TextField dom = new TextField();
    private TextField lun = new TextField();
    private TextField mar = new TextField();
    private TextField mie = new TextField();
    private TextField hue = new TextField();
    private TextField vir = new TextField();
    private TextField sab = new TextField();
    private Label firstd = new Label("Sunday");
    private Label secd = new Label("Monday");
    private Label thrd = new Label("Tuesday");
    private Label fortd = new Label("Wednesday");
    private Label fiftd = new Label("Thursday");
    private Label sistd = new Label("Friday");
    private Label svnd = new Label("Saturday");
    private Button submit = new Button();
    private Button open = new Button();
    private Button clear = new Button();
    private ArrayList<Appointment> date = new ArrayList<Appointment>();

public class Appointment{
    DayOfWeek day;
    String tiempo;
    String d;

    Appointment(String tiempo,String d){
        if(Character.isDigit(tiempo.charAt(0)) && tiempo.endsWith("a") || 
                tiempo.endsWith("p")){
        this.tiempo = tiempo;
        }
        else{
            throw new IllegalArgumentException("First input must be numeric and a for am"+
                    "p for pm");
        }
        this.d = d;
    }
}
public void submit(){
    for (DayOfWeek day : DayOfWeek.values()){
        switch (day){
        case SUNDAY: 
           Appointment sunday = new Appointment(s.getText(),dom.getText());
           date.add(sunday);
            break;
        case MONDAY:
            Appointment monday = new Appointment(m.getText(),lun.getText());
            date.add(monday);
            break;
        case TUESDAY:
            Appointment tuesday = new Appointment(t.getText(),mar.getText());
            date.add(tuesday);
            break;
        case WEDNESDAY:
            Appointment wednesday = new Appointment(w.getText(),mie.getText());
            date.add(wednesday);
            break;
        case THURSDAY:
            Appointment thursday = new Appointment(th.getText(),hue.getText());
            date.add(thursday);
            break;
        case FRIDAY:
            Appointment friday = new Appointment(f.getText(),vir.getText());
            date.add(friday);
            break;
        case SATURDAY:
            Appointment saturday = new Appointment(st.getText(),sab.getText());
            date.add(saturday);
            break; 
        default:
            System.out.println("Error oh no help master");
            break;
        }
    }
}

public void open(){


}
public void clear(){
    date.clear();
}

    @Override
    public void start(Stage primaryStage) {
        submit.setText("Submit");
        submit.setStyle("-fx-background-color: #00ff00");
        submit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
               submit();
            }
        });
        open.setText("Open");
        open.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                open();
            }
        });
        clear.setText("Clear");
        clear.setStyle("-fx-background-color: #ff0000");
        clear.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                clear();
            }
        });

        HBox frame = new HBox();
        VBox sun = new VBox(10);
        VBox mon = new VBox(10);
        VBox tues = new VBox(10);
        VBox wed = new VBox(10);
        VBox thur = new VBox(10);
        VBox fri = new VBox(10);
        VBox sat = new VBox(10);

        sun.getChildren().addAll(firstd,s,dom);
        mon.getChildren().addAll(secd,m,lun);
        tues.getChildren().addAll(thrd,t,mar);
        wed.getChildren().addAll(fortd,w,mie);
        thur.getChildren().addAll(fiftd,th,hue,clear);
        fri.getChildren().addAll(sistd,f,vir,open);
        sat.getChildren().addAll(svnd,st, sab,submit);

        firstd.setTranslateX(20);
        secd.setTranslateX(20);
        thrd.setTranslateX(20);
        fortd.setTranslateX(10);
        fiftd.setTranslateX(20);
        sistd.setTranslateX(20);
        svnd.setTranslateX(20);

        frame.getChildren().addAll(sun,mon,tues,wed,thur,fri,sat);

        Scene scene = new Scene(frame, 600, 300);

        primaryStage.setTitle("Appoinment Calendar");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

1 个答案:

答案 0 :(得分:0)

代码写得不好。只需考虑所有天的代码有多相似。为什么要重复自己,而不是想出一个合适的数据结构来让您更方便地访问对象?当您可以创建2个并使用循环重复该过程时,为什么还要创建2个x 7 TextField呢?

您需要做的另一件事实际上是将星期几存储在您的Appointment中。

以下代码中的open实现假定date列表中的内容已经包含正确的值,即使当前代码仅恢复最后提交的值(如果用户编辑了{{1 }}。

TextField