Нужна помощь для получения доступа к кнопке внутри GridPane. GridPane находится внутри HBox, который находится в центре корня. Этот Hbox является NumberBoy для отображения номеров лото для выбора. они как купоны. знаю, что мне нужно добраться до этой кнопки для обработки события. Я должен выбрать Numbers, и эти цифры должны быть указаны в ярлыке myNumbers. Я сделал, может быть, сложный графический интерфейс для него, но почему-то я не могу получить доступ к кнопкам isde the Grid для обработки событий.
Другая проблема показывает, когда я меняю купоны еще на два купона (в качестве опции в манубаре «Купоны»). После изменения количества купонов возникает много запутанных ошибок. Также после выбора один раз я не могу выбрать другое время. Как я могу решить эту проблему, ребята? Нужна помощь для этого.
package LottoSimulator.LottoView;
import java.util.ArrayList;
import LottoSimulator.LottoMain;
import LottoSimulator.LottoModel.LottoModel;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class LottoView {
private LottoModel model;
private Stage stage;
protected BorderPane root;
protected Scene scene;
private MenuArea menubar;
public Button numbers[];
private Button numberButton;
// private ArrayList<HBox> numberBoxs;
//for the LottoNumbers to choose
private HBox numberBox;
private BorderPane chooseNumberPane;
private GridPane numberField;
private HBox pickedNumbersBox;
private HBox couponsArea;
// private Label myNumbers;
private VBox evaluationBox;
private Label correctNumbers;
private Label odds;
private Label bonus;
private Label[] myNumbers;
//ControlArea elements for Left of Root
private Button playB;
private Button saveB;
private Button showB;
private Button[] bottomButtons;
private VBox controlBox;
// elements for the WinnerLabel
private HBox winnerBox;
private Label winnerLabel;
private Label winNumbers[] = new Label[7];
private ArrayList<Button> buttons;
public LottoView(Stage stage, LottoModel model) {
this.stage = stage;
root = new BorderPane();
menubar = new MenuArea();
root.setTop(menubar);
playB = new Button("Play");
saveB = new Button("Save Highscore");
showB = new Button("Show Highscore");
bottomButtons = new Button[3];
bottomButtons[0] = playB;
bottomButtons[1] = saveB;
bottomButtons[2] = showB;
controlBox = new VBox();
controlBox.getChildren().addAll(bottomButtons);
controlBox.setSpacing(15.0);
controlBox.setAlignment(Pos.BOTTOM_CENTER);
for(Button b : bottomButtons) {
b.getStyleClass().add("controlButtons");
}
root.setLeft(controlBox);
winnerBox = new HBox();
for(int a = 0; a < winNumbers.length;a++) {
Label label = new Label("--");
winNumbers[a] = label;
label.getStyleClass().add("winNumbers");
}
winnerBox.setAlignment(Pos.CENTER);
winnerBox.setSpacing(20.0);
winnerBox.getChildren().addAll(winNumbers);
root.setBottom(winnerBox);
createNumbersArea();
createNumberBox();
root.setCenter(couponsArea);
scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("Lotto.css").toExternalForm());
stage.setScene(scene);
}
public HBox getWinnerBox() {
return winnerBox;
}
public void setWinnerBox(HBox winnerBox) {
this.winnerBox = winnerBox;
}
public Label getWinnerLabel() {
return winnerLabel;
}
public void setWinnerLabel(Label winnerLabel) {
this.winnerLabel = winnerLabel;
}
public Label[] getWinNumbers() {
return winNumbers;
}
public void setWinNumbers(Label[] winNumbers) {
this.winNumbers = winNumbers;
}
public HBox createNumberBox() {
numberBox = new HBox();
chooseNumberPane = new BorderPane();
numberField = new GridPane();
myNumbers = new Label[7];
pickedNumbersBox = new HBox();
// myNumbers = new Label(" -- -- -- -- -- -- -- + -- ");
evaluationBox = new VBox();
evaluationBox.setPadding(new Insets(10, 5, 5, 5));
evaluationBox.setSpacing(3);
correctNumbers = new Label("Correct: ");
odds = new Label("Odd: ");
bonus = new Label("Bonuszahl: ");
evaluationBox.getChildren().addAll(correctNumbers, odds, bonus);
//labels machen
for(int i = 0; i<myNumbers.length; i++){
// label = new Label
myNumbers[i] = new Label();
pickedNumbersBox.getChildren().add(myNumbers[i]);
pickedNumbersBox.setSpacing(20.0);
}
numberBox.getChildren().add(chooseNumberPane);
chooseNumberPane.setCenter(numberField);
chooseNumberPane.setTop(pickedNumbersBox);
chooseNumberPane.setRight(evaluationBox);
//creating Buttons and Adds the Buttons in an Array
numbers = new Button[43];
for(int i = 0; i< numbers.length;i++) {
numbers[i] = new Button(Integer.toString(i));
}
//setting the Buttons in the GridPane
for(int i = 1; i < numbers.length; i++) {
numberField.add(numbers[i], (i-1)%6, (i-1)/6);
}
return numberBox;
}
// Creating the NumbersArea with all elements
public void createNumbersArea() {
HBox[] couponsArray = new HBox[LottoMain.NUM_COUPONS];
couponsArea = new HBox();
for(int i = 0; i < LottoMain.NUM_COUPONS; i++) {
couponsArray[i] = createNumberBox();
}
couponsArea.getChildren().addAll(couponsArray);
}
public void start() {
stage.show();
}
// getters and Setters for alls elements
public LottoModel getModel() {
return model;
}
public void setModel(LottoModel model) {
this.model = model;
}
public Stage getStage() {
return stage;
}
public void setStage(Stage stage) {
this.stage = stage;
}
public BorderPane getRoot() {
return root;
}
public void setRoot(BorderPane root) {
this.root = root;
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
public MenuArea getMenubar() {
return menubar;
}
public void setMenubar(MenuArea menubar) {
this.menubar = menubar;
}
public Button[] getNumbers() {
return numbers;
}
public void setNumbers(Button[] numbers) {
this.numbers = numbers;
}
public HBox getNumberBox() {
return numberBox;
}
public void setNumberBox(HBox numberBox) {
this.numberBox = numberBox;
}
public BorderPane getChooseNumberPane() {
return chooseNumberPane;
}
public void setChooseNumberPane(BorderPane chooseNumberPane) {
this.chooseNumberPane = chooseNumberPane;
}
public GridPane getNumberField() {
return numberField;
}
public void setNumberField(GridPane numberField) {
this.numberField = numberField;
}
public HBox getPickedNumbersBox() {
return pickedNumbersBox;
}
public void setPickedNumbersBox(HBox pickedNumbersBox) {
this.pickedNumbersBox = pickedNumbersBox;
}
public HBox getCouponsArea() {
return couponsArea;
}
public void setCouponsArea(HBox couponsArea) {
this.couponsArea = couponsArea;
}
public VBox getEvaluationBox() {
return evaluationBox;
}
public void setEvaluationBox(VBox evaluationBox) {
this.evaluationBox = evaluationBox;
}
public Label getCorrectNumbers() {
return correctNumbers;
}
public void setCorrectNumbers(Label correctNumbers) {
this.correctNumbers = correctNumbers;
}
public Label getOdds() {
return odds;
}
public void setOdds(Label odds) {
this.odds = odds;
}
public Label getBonus() {
return bonus;
}
public void setBonus(Label bonus) {
this.bonus = bonus;
}
public Label[] getMyNumbers() {
return myNumbers;
}
public void setMyNumbers(Label[] myNumbers) {
this.myNumbers = myNumbers;
}
public Button getPlayB() {
return playB;
}
public void setPlayB(Button playB) {
this.playB = playB;
}
public Button getSaveB() {
return saveB;
}
public void setSaveB(Button saveB) {
this.saveB = saveB;
}
public Button getShowB() {
return showB;
}
public void setShowB(Button showB) {
this.showB = showB;
}
public Button[] getBottomButtons() {
return bottomButtons;
}
public void setBottomButtons(Button[] bottomButtons) {
this.bottomButtons = bottomButtons;
}
public VBox getControlBox() {
return controlBox;
}
public void setControlBox(VBox controlBox) {
this.controlBox = controlBox;
}
}
package LottoSimulator.LottoModel;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
public class LottoModel {
private ArrayList<Integer> winnerNumbers;
private int miNumero[];
private int bonusNumber = 0;
private double odd;
private int wins;
private Random rand = new Random();
private String bonusLblString;
public LottoModel() { } // Contructor empty not to use but generated for any cases.
//generates the Winner numbers + Bonus number
//
public ArrayList<Integer> randWinNumbers() {
//Gewinnerzahlen zwischen 1-42 werden generiert
while(winnerNumbers.size() < 7) {
int random = rand.nextInt(42)+1;
if(!winnerNumbers.contains(random)) {
winnerNumbers.add(random);
}
}
//Bonuszahl zwischen 1-6 wird generiert
bonusNumber = rand.nextInt(6)+1;
// System.out.println("WinnerNumbers: " + winnerNumbers + " Bonus: " + bonusNumber);
return winnerNumbers;
}
public void compareNumbers(int bonus, int[] choosedNumbers) {
for(int i=0; i<winnerNumbers.size(); i++) {
if(winnerNumbers.contains(choosedNumbers[i])) {
wins++;
}
}
//Wahrscheinlichkeitsberechnung zutrifft auf wie viele getroffene Zahlen
switch(wins) {
case 0: odd= 25.0;
break;
case 1: odd= 6.0;
break;
case 2: odd= 1.2;
break;
case 3: odd= 0.2;
break;
case 4: odd= 0.02;
break;
case 5: odd= 0.002;
break;
case 6: odd= 0.00013;
break;
case 7: odd= 0.000004;
break;
}
bonusLblString = " ";
//Bonuszahl trifft zu
if(bonus!=0 && bonus==bonusNumber) {
switch(wins){
case 0: odd= 25.0;
break;
case 1: odd= 1.0;
break;
case 2: odd= 0.2;
break;
case 3: odd= 0.03;
break;
case 4: odd= 0.003;
break;
case 5: odd= 0.0003;
break;
case 6: odd= 0.000021;
break;
case 7: odd= 0.00000067;
break;
}
setBonusLblString("Bonuszahl: Richtig");
} else {
setBonusLblString("Bonuszahl: Falsch");
}
}
public ArrayList<Integer> getWinnerNumbers() {
return winnerNumbers;
}
public void setWinnerNumbers(ArrayList<Integer> winnerNumbers) {
this.winnerNumbers = winnerNumbers;
}
public int[] getMiNumero() {
return miNumero;
}
public void setMiNumero(int[] miNumero) {
this.miNumero = miNumero;
}
public int getBonusNumber() {
return bonusNumber;
}
public void setBonusNumber(int bonusNumber) {
this.bonusNumber = bonusNumber;
}
public double getOdd() {
return odd;
}
public void setOdd(double odd) {
this.odd = odd;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public Random getRand() {
return rand;
}
public void setRand(Random rand) {
this.rand = rand;
}
public String getBonusLblString() {
return bonusLblString;
}
public void setBonusLblString(String bonusLblString) {
this.bonusLblString = bonusLblString;
}
}
package LottoSimulator.LottoController;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Random;
import LottoSimulator.LottoMain;
import LottoSimulator.LottoModel.LottoModel;
import LottoSimulator.LottoView.LottoView;
import LottoSimulator.LottoView.MenuArea;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
import javafx.util.Duration;
public class LottoController {
private LottoModel model;
private LottoView view;
private int turns, counter;
private String[] choosedNumbers = new String [7];
private Label[] labelPickedNo = new Label[7];
private int pickedBonus = 0;
private String chooseString;
public int x = 0;
String userHomeFolder = System.getProperty("user.home");
File textFile = new File(userHomeFolder, "Lotto_Safe_Data.txt");
public LottoController(LottoModel model, LottoView view) {
this.model = model;
this.view = view;
MenuArea.getOneC().setOnAction(e->{
LottoMain.NUM_COUPONS = 1;
});
MenuArea.getTwoC().setOnAction(e->{
LottoMain.NUM_COUPONS = 2;
view.getStage().close();
Platform.runLater( () -> {
try {
new LottoMain().start(new Stage());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
});
MenuArea.getThreeC().setOnAction(e->{
LottoMain.NUM_COUPONS = 3;
view.getStage().close();
Platform.runLater( () -> {
try {
new LottoMain().start(new Stage());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
});
MenuArea.getFourC().setOnAction(e->{
LottoMain.NUM_COUPONS = 4;
view.getStage().close();
Platform.runLater( () -> {
try {
new LottoMain().start(new Stage());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
});
}
}
package LottoSimulator;
import LottoSimulator.LottoController.LottoController;
import LottoSimulator.LottoModel.LottoModel;
import LottoSimulator.LottoView.LottoView;
import javafx.application.Application;
import javafx.stage.Stage;
public class LottoMain extends Application {
public static int NUM_COUPONS = 1;
private LottoView view;
private LottoController controller;
private LottoModel model;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
model = new LottoModel();
view = new LottoView(primaryStage, model);
controller = new LottoController(model, view);
view.start();
}
}