-2

如何使此代码作为 jApplet 运行?

我的小程序现在看起来像这样:

public class TickTackToeApplet extends JApplet implements ActionListener {
    private static final long serialVersionUID = 1L;
    private ArrayList<JButton> buttons = new ArrayList<JButton>();

    private JPanel panel = new JPanel();
    private Game game = new Game();
    private Player player1;
    private Player player2;
    private int numberOfPlacedOutChars = 0;
    private String winner = "None";

    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    game = new Game();
                    game.setSize(4);

                    try {
                        PlayerManager pm = new PlayerManager();
                        player1 = pm.getPlayer("Henrik", "temp123");
                        player2 = pm.getPlayer("test", "test");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    createGUI(game.getSize());
                }
            });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void CreateButtons(int gameBoardSize)
    {
        for(int x=0; x<gameBoardSize; x++)
        {
            for(int y=0; y<gameBoardSize; y++)
            {
                JButton btn = new JButton("");
                btn.setFont(new Font("Tahoma", Font.PLAIN, 32));
                btn.setName(x + ";" + y);
                btn.addActionListener(this);
                buttons.add(btn);
            }
        }
    }

    public void PlaceOutButtons()
    {
        for(JButton btn : buttons)
            panel.add(btn);
    }

    public void createGUI(int gameBoardSize) {

        panel.setSize(gameBoardSize*25, gameBoardSize*25);
        panel.setBackground(Color.BLACK);
        getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(gameBoardSize, gameBoardSize));

        CreateButtons(gameBoardSize);
        PlaceOutButtons();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton buttonClicked = (JButton)e.getSource();
        String coordinates = buttonClicked.getName();
        String[] strArr = coordinates.split(";");
        int x = Integer.parseInt(strArr[0]);
        int y = Integer.parseInt(strArr[1]);

        if (this.numberOfPlacedOutChars % 2 == 0) {
            this.player1.setGameChar('X');
            this.game.placeChar('X', x, y);
            buttonClicked.setText("X");
            buttonClicked.setEnabled(false);
        }
        else {
            this.player2.setGameChar('O');
            this.game.placeChar('O', x, y);
            buttonClicked.setText("O");
            buttonClicked.setEnabled(false);
        }

        numberOfPlacedOutChars++;

        if(this.game.checkWin() == true) {

            updatePlayersInfo(); 

            for(JButton btn : buttons) {
                btn.setEnabled(false);
            }

            int choice = JOptionPane.showConfirmDialog(this, "Winner is: " + this.winner + 
                    ", play again?", "WINNER", JOptionPane.YES_NO_OPTION);

            if(choice == 0) {
                clearGameBoard();
                return;
            }
            //stop the applet
            stop();
        }

        if(this.game.IsDonePlaying() && this.game.checkWin() == false) {

            int choice = JOptionPane.showConfirmDialog(this, "Winner is: " + this.winner + 
                    ", play again?", "WINNER", JOptionPane.YES_NO_OPTION);

            if(choice == 0) {
                clearGameBoard();
                return;
            }
            //stop the applet
            stop();
        }
    }

    public void startGame(int gameSize, Player player1, Player player2) {
        this.game.setSize(gameSize);
        this.player1 = player1;
        this.player2 = player2;
    }

    private void updatePlayersInfo() {
        if(this.game.getWinningChar() == this.player1.getGameChar()) {
            this.player1.incrementWins();
            this.player2.incrementLosses();
            this.winner = this.player1.getUserName();
        }

        if(this.game.getWinningChar() == this.player2.getGameChar()) {
            this.player2.incrementWins();
            this.player1.incrementLosses();
            this.winner = this.player2.getUserName();
        }

        try {
            PlayerManager playerManager = new PlayerManager();
            playerManager.savePlayer(this.player1);
            playerManager.savePlayer(this.player2);
        } catch (IllegalArgumentException | IOException | InterruptedException e1) {
            JOptionPane.showMessageDialog(this, "Couldn't update player info!");
        }
    }

    private void clearGameBoard() {
        for(JButton btn : this.buttons) {
            btn.setEnabled(true);
            btn.setText("");
            this.winner = "None";
            this.game.setWinningChar('\0');
        }
    }

}

但是我的小程序没有显示在浏览器中。

我可以在 html 文件中使用这个小程序标签吗?

<applet codebase="bin" code="gui/TickTackToeApplet.class" width=500 height=500>
<p>Testing my applet...</p>
</applet>

或者我应该在小程序标签中写什么?

4

1 回答 1

1

您的代码太长,无法深入研究。

我不能确保它会工作甚至编译,但我将提供一些基本指南。

1 使用增量编程,编写一些东西,确保它正常工作,添加更多功能。

2 不要用大写字母调用方法,改成placeOutButtons

public void PlaceOutButtons()

3SwingUtilities.invokeLater创建 GUI 时使用

 SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        createAndShowGUI();
    }
});

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

4 init()是您的小程序的入口点,方法浏览器将调用。因为它是空的,所以什么也没有发生。

http://docs.oracle.com/javase/tutorial/deployment/applet/appletMethods.html

5 你永远不会在类上调用构造函数TickTackToeApplet。试着在里面做init

6 如果您使用 Eclipse,您可以使用其内置的 Applet Viewer 启动小程序。

于 2013-01-04T16:24:57.183 回答