Как передать параметр из commandButton компоненту в JSF? - PullRequest
0 голосов
/ 06 февраля 2020

Я пытаюсь сделать игру ti c -ta c -toe в JSF. У меня есть двумерная матрица (3x3), которая содержит значения 0, 1 или 2. Так что все это в начале 0 и переключается на 1 или 2. В зависимости от игрока.

Я пытаюсь получить изображение для кнопки из метода в компоненте, который считывает значение в матрице и дает ему соответствующее изображение (ничего, X или O);

<h:form id="board">

<!--  id string i+j  -->

<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="00"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="01"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="02"/></h:commandButton>
<br />
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="10"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="11"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="12"/></h:commandButton>
<br />
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="20"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="21"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="22"/></h:commandButton>

</h:form>

и в компоненте, который у меня есть

public String btnImg() {

        String buttonId = getParameter("btn");

        int i = Integer.parseInt(String.valueOf(buttonId.charAt(0)));
        int j = Integer.parseInt(String.valueOf(buttonId.charAt(1)));

        int[][] posi = x.getTabuleiro();

        if (posi[i][j] == 0)
            return "resources/images/clear.png";

        else if (posi[i][j] == 1)
            return "resources/images/black_x.png";

        else
            return "resources/images/black_o.png";

    }

getTabuleiro дает мне доску, как сейчас. Как получить параметры кнопки, чтобы она знала, какая кнопка какая?

1 Ответ

0 голосов
/ 06 февраля 2020

Использовать методики будет проще всего.

<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg(2,2)}" style="width:200px;height:200px"></h:commandButton>

и ваш Java - Метод, подобный

public String btnImg(int i, int j) {

        int[][] posi = x.getTabuleiro();

        if (posi[i][j] == 0)
            return "resources/images/clear.png";

        else if (posi[i][j] == 1)
            return "resources/images/black_x.png";

        else
            return "resources/images/black_o.png";

    }
...