じゃんけん(JSF) †JavaServer? Facesでじゃんけんをするページを作成する。 import javax.inject.Named; import javax.enterprise.context.RequestScoped; @Named(value = "janken") @RequestScoped public class Janken { private int result; public Janken() { } // getter/setter省略 public void janken() { double rand = Math.random(); result = (int) (rand / 0.3) + 1; } public void reset() { result = 0; } } 続いて、じゃんけんをする画面をFaceletsで作成する。 #ref(): File not found: "jn1.jpg" at page "じゃんけん(JSF)" h:graphicImageは画像を表示するためのタグである。 <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core"> <h:head> <title>じゃんけんポン</title> </h:head> <h:body> じゃんけんポン♪ <h:form> <h:graphicImage id="im" width="240" height="135" value="#{janken.result eq 0 ? 'd.jpg' : janken.result eq 1 ? 'g.jpg' : janken.result eq 2 ? 'c.jpg' : janken.result eq 3 ? 'p.jpg' : 'gcp.jpg'}"/> <br/> <h:commandButton value="グー" action="#{janken.janken()}"> <f:ajax render="im"/> </h:commandButton> <h:commandButton value="チョキ" action="#{janken.janken()}"> <f:ajax render="im"/> </h:commandButton> <h:commandButton value="パー" action="#{janken.janken()}"> <f:ajax render="im"/> </h:commandButton> <h:commandButton value="戻る" action="#{janken.reset()}"> <f:ajax render="im"/> </h:commandButton> </h:form> </h:body> </html>
コメント † |