Napisałem taką oto apkę w FX i właściwie nawet fajna ta Java, nie trzeba się martwić o te wszystkie wskaźniki, adresy czy headery jak w C++.
//habit
package sample;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextAlignment;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
public class Habit {
String name, notes;
int currentScore, maxScore;
///gui
Button statusBtn;
Label namelbl;
HBox lay;
///
Habit(String name, String notes) {
this.name = name;
this.notes = notes;
namelbl = new Label(name);
namelbl.setTextAlignment(TextAlignment.CENTER);
statusBtn = new Button("...");
statusBtn.setPrefSize(32, 32);
statusBtn.setVisible(true);
statusBtn.setOnAction(e -> System.out.println(notes));
lay = new HBox();
lay.setSpacing(10);
lay.setAlignment(Pos.BASELINE_LEFT);
lay.getChildren().addAll(statusBtn, namelbl);
Calendar cal = Calendar.getInstance();
Date dat = cal.getTime();
cal.setTime(dat);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int day = cal.get(Calendar.DAY_OF_MONTH);
int monday = 0;
if (dayOfWeek == 1) monday = day -6;
else if (dayOfWeek == 2) monday = day;
else if (dayOfWeek == 3) monday = day - 1;
else if (dayOfWeek == 4) monday = day - 2;
else if (dayOfWeek == 5) monday = day - 3;
else if (dayOfWeek == 6) monday = day - 4;
else if (dayOfWeek == 7) monday = day - 5;
Vector<Button> buttons = new Vector<>();
for (int i = 0; i < 7; i++) {
Button btnxd = new Button();
btnxd.setText(String.valueOf(monday + i));
btnxd.setPadding(new Insets(5, 5, 5, 5));
btnxd.setPrefSize(32, 32);
if (i <= dayOfWeek-2) setStatus(status.UNKNOWN, btnxd); /// tu dodałem -2
buttons.add(btnxd);
lay.getChildren().add(btnxd);
}
}
public enum status {
UNKNOWN, COMPLETED, SKIPPED, FAILED
}
public void setStatus(status stat, Button but) {
if (stat == status.UNKNOWN) {
but.setStyle("-fx-background-color: gray;");
but.setText("?");
but.setOnAction(e -> setStatus(status.COMPLETED, but));
} else if (stat == status.COMPLETED) {
but.setStyle("-fx-background-color: green;");
but.setText("✔");
but.setOnAction(e -> setStatus(status.SKIPPED, but));
currentScore+=1;
if(currentScore>maxScore) maxScore = currentScore;
} else if (stat == status.SKIPPED) {
but.setStyle("-fx-background-color: blue;");
but.setText("▬");
but.setOnAction(e -> setStatus(status.FAILED, but));
} else if (stat == status.FAILED) {
but.setStyle("-fx-background-color: red;");
but.setText("✖");
but.setOnAction(e -> setStatus(status.UNKNOWN, but));
currentScore=0;
}
}
public String getName() {
return name;
}
public HBox getLay() {
return lay;
}
}
//userInput
package sample;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class UserInput {
TextField nameInput;
Label namelbl;
TextField notesInput;
Label noteslbl;
GridPane layout;
Button okBtn;
private String name,notes;
public UserInput() {
nameInput = new TextField();
namelbl = new Label("Habit name:");
notesInput = new TextField();
noteslbl = new Label("Habit notes:");
okBtn = new Button(); okBtn.setText("OK!"); okBtn.setOnAction(e -> {
if(!nameInput.getText().isEmpty() || !notesInput.getText().isEmpty()){
name = nameInput.getText();
notes = notesInput.getText();
Main.addHabit(name,notes);
}
});
layout = new GridPane(); layout.setHgap(10); layout.setVgap(15);
layout.add(namelbl,0,0);
layout.add(nameInput,1,0);
layout.add(noteslbl,0,1);
layout.add(notesInput,1,1);
layout.add(okBtn,1,2);
}
GridPane getInput(){return layout;}
}
//main
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
Stage Mainwindow;
static VBox layout= new VBox();
@Override
public void start(Stage primaryStage) {
///HERE'S YOUR CODE :-)///
Mainwindow = primaryStage;
Mainwindow.setTitle("Habits Tracker 2019");
Habit habit = new Habit("Workout", "Every day hard pump");
Habit habit2 = new Habit("Reading", "Brain improvement");
Habit habit3 = new Habit("Meditating", "Clean mind");
layout.setSpacing(15);
Scene ourWindow = new Scene(layout, 500, 400);
UserInput input = new UserInput();
layout.getChildren().add(input.getInput());
layout.getChildren().add(habit.getLay());
layout.getChildren().add(habit2.getLay());
layout.getChildren().add(habit3.getLay());
Mainwindow.setScene(ourWindow);
Mainwindow.show();
}
public static void addHabit(String name, String notes) {
Habit hab = new Habit(name,notes);
layout.getChildren().add(hab.getLay());
}
public static void main(String[] args) {
launch(args);
}
}
Jakieś rady, wskazówki, coś w nim powinienem zmienić, usprawnić? (taki prototyp na szybko)