Kółko i krzyzyk

0

Witam, w javieFx umieściłem Buttony w gridPane, teraz chciałbym się do nich odwoływać bo numerach wierszy i kolumn. I tu jest pytanie jak z gridPane pobrać etykiete przycisku który znajduję się w danym miejscu? Czytam szukam nic nie mogę wymyślić.

0

Hmmm chyba tego czego potrzebujesz to wyciągniecie obiektu button z konkretnej kolumny i wiersza. W tym celu powinieneś stworzyć sobie metodę która zwróci Ci ten button. Algorytm działania

  1. Pobierz wszystkie wrzucone przez Ciebie elementy z panelu (zapewne tylko przyciski).
  2. Iterujesz po tych obiektach sprawdzając dla każdego czy jego położenie w gridPane nie jest tym którego szukasz
  3. Zwracasz znaleziony przycisk.

Przydatne metody :

ObservableList<Node> childrens = gridPane.getChildren();

int row = gridPane.getRowIndex(node);

int column = gridPane.getColumnIndex(node);

0

Pierwszy raz spotykam się z tym Node, mógłbyś objaśnić o co tak w tym chodzi?

0

Node jest klasą z której każda kontrolka w javafx dziedziczy. Dodając element do kontenera takiego jak np. GridPane dodajesz zawsze obiekt który rozszerza bezpośrednio lub pośrednio klase Node. Jeśli dodajesz do kontenera tylko Button-y możesz od razu rzutować taki obiekt na Button.

ObservableList<Node> childrens = gridPane.getChildren(); //lista kontrolek zawartych w panelu gridPane
for(Node node : childrens){
    Button button = (Button) node;
}

Specjalnie nie piszę Ci całego rozwiązania abyś mógł sam do tego dojść. Masz tak naprawdę już większą połowę potrzebną do napisania tej funkcji która zwróci obiekt button zawarty w konkretnym wierszu i kolumnie.

0

Za słaby jeszcze jestem w te klocki żeby to pojąć. Próbowałem coś takiego ale wywala masę błędów których nawet odczytać nie potrafię. Zamiarem było aby wyświetlić co siedzi w kolumnie 1 i wersie 1.

 package pl.batia.tictactoe.game;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class Game implements Initializable {

	@FXML
	
    private GridPane gridPane;
	private Button clickedButton;
	public static Boolean cureentlyPlayer = true;

	public void initialize(URL arg0, ResourceBundle arg1) {

	}

	@FXML
	public void ButtonClicked(ActionEvent event) {
		clickedButton = (Button) event.getTarget();
		
			if (clickedButton.getText().equals("")) {
				if (cureentlyPlayer == true) {
					clickedButton.setText("X");
					cureentlyPlayer = false;
					
				
				} else {

					clickedButton.setText("O");
					cureentlyPlayer = true;
					
				}
			RowsAndColumns(1,1,gridPane);
				
				}
			
		}
	
				
	
	 public Node RowsAndColumns(final int row,final int column,GridPane gridPane) {
	        Node result = null;
	        ObservableList<Node> childrens = gridPane.getChildren();
	        for(Node node : childrens) {
	            if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
	                result = node;
	               Button clickedButton = (Button) node;
	                System.out.println(clickedButton.getText());
	                break;
	               
	               }
	            }
	        
	        return result;
	        
	    }
		
				
		}
0

Wygląda dobrze oprócz

if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column)

powinno być

if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column)

Wrzuć co Ci się dokładnie sypie, co masz w konsoli.

0
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
	at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Node.fireEvent(Node.java:8390)
	at javafx.scene.control.Button.fire(Button.java:185)
	at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
	at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
	at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$137/873930576.get(Unknown Source)
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
	at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
	at com.sun.glass.ui.View.notifyMouse(View.java:927)
	at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
	at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
	at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
	at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
	... 50 more
Caused by: java.lang.NullPointerException
	at pl.batia.tictactoe.game.Game.RowsAndColumns(Game.java:55)
	at pl.batia.tictactoe.game.Game.ButtonClicked(Game.java:43)
	... 60 more 
0

gridPane może być nullem ?

ObservableList<Node> childrens = gridPane.getChildren();
0

Poradzilem sobie ze wszystkim. Ale jeszcze 1 rzeczy nue moge ogarnac, Panowie jak w przypadku szybkiej wygranej zablokowac mozliwosc klikniecia w inne przyciski ktore nie posiadaja jeszcze ani x ani o. Probowalem button.setDisable (true); ale tak tego zrobic sie nie da. Pozdro

1 użytkowników online, w tym zalogowanych: 0, gości: 1