문제

나는 이것에 대한 많은 사례가 있다는 것을 알고 있지만, 그들은 모두 약간 날짜가 약간 (Sun Docs)를 느낍니다. Java 애플릿 내부에서 JavaScript와 대화하는 방법은 무엇입니까? Alert ()를 호출하는 방법과 같은 간단한 것은 내가 찾고있는 전부입니다. 브라우저에 JavaScript가 활성화되어 있는지 확인하는 방법을위한 보너스 포인트 (이 애플릿은 JavaScript를 끄는 것이 실제 가능성이있는 학교 환경에서 사용할 수 있습니다). 모든 도움이 미리 감사드립니다 ... 미리 감사드립니다 ...

암호:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import netscape.javascript.JSObject;

public class TeamProject extends Applet implements ActionListener, MouseListener {
    char[][] charValues = new char[10][10];
    Table aTable;
    boolean allowUserInput = false;
    Button BtnStart;
    Button randomChangeBtn;
    boolean guessMode;
    JSObject jso;

    public void init() {
        //setup buttons
        BtnStart = new Button("add row/column");
        BtnStart.addActionListener((ActionListener)this);   //cast
        randomChangeBtn = new Button("change one value");
        randomChangeBtn.addActionListener((ActionListener)this);
        //add button
        this.add(BtnStart);
        //add image to Image objects
        Image imgO = getImage(getCodeBase(), "images/not.gif");
        Image imgX= getImage(getCodeBase(), "images/cross.gif");
        //setup table
        aTable = new Table(100, 100, 75, 55, 5, 5, imgX, imgO);
        //setBackground(Color.LIGHT_GRAY);
        super.resize(700, 700);
        //add mouse listener
        addMouseListener(this);
        //initially guessMode will be false
        guessMode = false;
        //to talk to javascript
        jso = JSObject.getWindow(this);
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        aTable.draw(g);
    }

    //Mouse listener methods
    public void mousePressed (MouseEvent e) {
        if(!guessMode){
            if ((allowUserInput)) { //&&(aTable.isDrawable(e.getX(), e.getY())))
                aTable.swapSquareValue(e.getX(), e.getY());
                repaint();
            }
        } else {
            System.out.println("guessed row = " + e.getY() + " guessed col = " + e.getX());
            aTable.checkGuess(e.getX(), e.getY());
            //repaint();
        }
    }

    public void mouseClicked (MouseEvent e) {}
    public void mouseEntered (MouseEvent e) {}
    public void mouseReleased (MouseEvent e) {}
    public void mouseExited (MouseEvent e) {}

    //Button action listener
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == BtnStart) {
            aTable.addRow();
            aTable.addColumn();
            this.remove(BtnStart);
            this.add(randomChangeBtn);
            repaint();
        } else if (e.getSource() == randomChangeBtn) {
            //aTable.addRow();
            aTable.randomChangeFunc();
            repaint();
            guessMode = true;
        }
        allowUserInput = true;
        System.out.println(aTable.toString());
    }
}

그리고 내 테이블 클래스 :

import java.awt.*;
import java.util.Random;

public class Table {
    private char[][]values = new char[10][10];  //probably better to use array of integer values(0 or 1)
    Image imgO;
    Image imgX;
    private int Rows;
    private int Columns;
    private int BoxWidth ;
    private int BoxHeight;
    public Point Pos = new Point();

    private int tableHeight;
    private int tableWidth;

    private int changedRow;
    private int changedCol;

    //constructor
    public Table(int x, int y, int width, int height, int col, int rows, Image X, Image O) {
        Rows = rows;
        Columns = col;
        BoxWidth = width;
        BoxHeight = height;
        Pos.x = x;
        Pos.y = y;

        imgX = X;
        imgO = O;

        tableHeight = Rows*BoxHeight;
        tableWidth = Columns*BoxWidth;
        this.setValues();
    }

    //draw table
    public void draw(Graphics g) {
        //draw vertical table lines
        for (int i = 0 ; i <= Columns ; i++)
            g.drawLine(i*BoxWidth + Pos.x, Pos.y, i*BoxWidth + Pos.x, tableHeight+Pos.y);

        //draw horizontal table line
        for(int i = 0 ; i <= Rows ; i++)
            g.drawLine(Pos.x, i*BoxHeight + Pos.y, tableWidth+Pos.x, i*BoxHeight + Pos.y);

        //draw values
        drawValues(g);
    }

    public void swapSquareValue(int x, int y) {
        if (this.isDrawable(x, y)) {
            int col = this.getColumn(x)-1;
            int row = this.getRow(y)-1;

            if(values[row][col] == 'X')
                values[row][col] = 'O';
            else if(values[row][col] == 'O')
                values[row][col] = 'X';
            else
                System.out.println("ERROR SWAPPING SQUARE VALUE");
        } else
            System.out.println("says not drawable");
    }

    public char getValue(int col, int row) {
        return values[row][col];
    }

    //return true if (x,y) is a point in the table
    public boolean isDrawable(int x, int y) {
        if((this.getRow(y)!=-1)||(this.getColumn(x)!=-1))
            return true;
        else
            return false;
    }

    public void addRow() {
        Rows++;
        tableHeight = (Rows*BoxHeight);
        int numOfXs = 0;
        for (int c=0; c < Columns; c++) {
            numOfXs = 0;
            for(int r = 0; r < Rows - 1; r++) {
                if(values[r][c] == 'X'){
                    numOfXs++;
                    System.out.println("in column " + c + "new x found at " + r + " " + c + ", numOfXs = " + numOfXs);
                }
                if(numOfXs % 2 == 0) {
                    values[Rows - 1][c] = 'O';
                } else{
                    values[Rows - 1][c] = 'X';
                }
            }//end inner for
            System.out.println("end of column " + c);
        }//end outer for
    }// end function

    public void addColumn() {
        Columns++;
        tableWidth = (Columns*BoxWidth);
        int numOfXs = 0;

        for (int r=0; r < Rows; r++) {
            numOfXs = 0;
            for(int c = 0; c < Columns - 1; c++) {
                if(values[r][c] == 'X') {
                    numOfXs++;
                    System.out.println("in row " + r + "new x found at " + r + " " + c + ", numOfXs = " + numOfXs);
                }
                if(numOfXs % 2 == 0) {
                    values[r][Columns - 1] = 'O';
                }
                else {
                    values[r][Columns - 1] = 'X';
                }
            }//end inner for
            System.out.println("end of row " + r);
        }
    }

    //does not add or remove values
    public void setColumn(int col) {
        Columns = col;
        tableWidth = (Columns*BoxWidth);
    }

    //does not add or remove values
    public void setRows(int row) {
        Rows = row;
        tableHeight = (row*BoxHeight);
    }

    public String toString() {
        String ValueString = "Displaying charValues[" + Rows + "][" + Columns + "]\n";
        for (int r=0; r < Rows; r++) {
            for (int c=0; c < Columns; c++) {
                ValueString += (char)values[r][c];
            }
            ValueString += "\n";        //next line
        }
        return ValueString;
    }

    private void drawValues(Graphics g) {
        Point drawPoint = new Point();
        for (int r=0; r < Rows; r++)
            for (int c=0; c < Columns; c++) {
                drawPoint.x = Pos.x+BoxWidth*c;
                drawPoint.y = Pos.y+BoxHeight*r;
                //g.setColor(Color.white);
                //g.fillRect(drawPoint.x+1, drawPoint.y+1, BoxWidth-1, BoxHeight-1);
                if (values[r][c] == 'X') {
                    g.drawImage(imgX,drawPoint.x+1, drawPoint.y+1, BoxWidth-1, BoxHeight-1, null);
                } else {
                    g.drawImage(imgO,drawPoint.x+1, drawPoint.y+1, BoxWidth-1, BoxHeight-1, null);
                }
                //System.out.print((char)values[r][c]);
            }
        g.setColor(Color.black);
    }

    //fills array with random values
    private void setValues() {
        for (int r=0; r < Rows; r++)
            for (int c=0; c < Columns; c++) {
                values[r][c] = this.randomChar();
            }
    }

    //randomly return 'X' or 'O'
    private char randomChar() {
        char randomValue;
        Random RandomGen = new Random();

        if (RandomGen.nextInt(2)==0)
            randomValue = 'O';
        else
            randomValue ='X';
        return randomValue;
    }

    private int getColumn(int x) {
        int offsetx=0;
        for (int i = 0 ; i < Columns ; i++) {
            offsetx = i*BoxWidth;
            if((x>Pos.x+offsetx)&& (x<Pos.x+offsetx+BoxWidth))
                return i+1;
        }
        return -1;
    }

    private int getRow(int y) {
        int offsety=0;
        for (int i = 0 ; i < Rows ; i++) {
            offsety = i*BoxHeight;
            if((y>Pos.y+offsety)&& (y<Pos.x+offsety+BoxHeight))
                return i+1;
        }
        return -1;
    }

    public void randomChangeFunc() {
        //get random row and column
        Random rand=new Random();

        int randRow = rand.nextInt(Rows);
        int randCol = rand.nextInt(Columns);

        System.out.println("randRow = " + randRow + " randCol = " + randCol);
        /*THIS SHOULD BE HANDLED BY swapSquareValue(randCol,randRow)
        /*BUT GETTING ERRORS (notDrawable). THE FOLLOWING CODE IS A WORK-AROUND
        */
        if(values[randRow][randCol] == 'X')
            values[randRow][randCol] = 'O';
        else if(values[randRow][randCol] == 'O')
            values[randRow][randCol] = 'X';
        else
            System.out.println("ERROR SWAPPING SQUARE VALUE");
        //set globals
        changedRow = randRow;
        changedCol = randCol;
    }

    public void checkGuess(int guessCol, int guessRow){
        int gCol = this.getColumn(guessCol)-1;
        int gRow = this.getRow(guessRow)-1;
        System.out.println("gCol = " + gCol + " gRow = " + gRow);
        if(gCol == changedCol && gRow == changedRow) {
            System.out.println("CORRECT!!!");
        } else
            System.out.println("incorrect :(");
    }
}

내 javac 명령 변경 :

javac -classpath /usr/lib/Java6u1/jre/lib/plugin.jar TeamProject.java

내 "테이블"클래스를 무시하고 언급 한 오류가 발생합니다. 어떤 아이디어?

도움이 되었습니까?

해결책

보다 이 기사. 이 페이지에서 애플릿에서 JS를 호출하려고한다면 Applet에서 업데이트 작업 후 JS 예외가 있기 때문에 확실히 작동합니다 :)

import netscape.javascript.JSObject

public void init() { jso = JSObject.getWindow(this); }

 public void actionPerformed(ActionEvent e) {
     if(jso != null )
         try {
                jso.call("updateWebPage", new String[] {txt.getText()});
             }
             catch (Exception ex) {
                 ex.printStackTrace();
             }
 }

편집하다:클래스 경로 문제의 경우 추가해야합니다 플러그인. 자 %java_home % jre lib plugin.jar에있는 클래스 경로에

edit2 :
나는 당신의 문제가 그 수업이라고 생각합니다 Table 컴파일되지 않았습니다 :

노력하다 javac -classpath /usr/lib/Java6u1/jre/lib/plugin.jar TeamProject.java Table.java 또는 와일드 카드를 사용하십시오 *.java.

어쩌면 당신은 IDE를 사용하는 것을 고려해야합니다 또는 Netbeans 명령 줄 도구로 어려움을 겪지 않고 프로젝트를 컴파일하고 실행합니다.

다른 팁

페이지에서 알림을 호출하는 샘플을 요청한대로 (Opera 10.01, FF 3.5.4, IE6에서 테스트).

참고 mayscript 애플릿 태그에서 이것 해야 하다 Java-JavaScript 커뮤니케이션을 활성화 할 수 있도록 참석하십시오. 기본 액세스에 따라 JSObject 보안상의 이유로 인해 비활성화됩니다.

HTML :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Test</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
  <applet code="HelloWWW.class" width="300px" height="100px"  MAYSCRIPT></applet>
</body>
</html>

Java (컴파일 javac -cp .;[pathtojre]/jre/lib/plugin.jar HelloWWW.java)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import netscape.javascript.*;

public class HelloWWW extends Applet implements ActionListener {
  Button runButton;
  public void init() {
    runButton = new Button("Run: alert(\"Hello WWW\")");
    add(runButton);
    runButton.addActionListener(this);
  }
  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() == runButton) {
      try {
        //get JSOBject
        JSObject jso = JSObject.getWindow(this);
        //call alert with parameter passed as Object array
        jso.call("alert", new Object[]{"Hello WWW"});
      } catch (JSException e) {
        e.printStackTrace();
      }
      runButton.setLabel("Did it!");
      repaint();
    }
  } 
}

또한 확인하십시오 Java-to-JavaScript 커뮤니케이션 그리고 Mozilla Dev : Jsobject 추가 정보.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top