使用ActionListener在我的GUI中出错

时间:2014-11-17 16:38:05

标签: java user-interface syntax-error actionlistener

所以,我正在研究一个项目,我的GUI似乎提出了4个错误。这些错误指向我添加动作侦听器的位置。我不确定如何解决这个问题。这是我的代码:

import javax.swing.*;
import java.awt.*;
public class GUIDisplay extends JFrame implements Display {
    //declearing global variables and J items.
    protected ReversiBoard board;
    protected JLabel rowL, colL, gameOverLabel1,gameOverLabel2,message1Label,
            message2Label;
    protected JTextField rowField, columnField;
    protected JButton moveButton, saveButton, startButton, quitButton;
    protected Canvas myCanvas;
    protected GUIDisplay myDisplay = new GUIDisplay(board);
    public GUIDisplay(ReversiBoard inBoard){
        board = inBoard;
    }
    //Simple message
    public void displayMessage(String message){
        message1Label = new JLabel(message);
    }
    //player status such as your turn
    public void displayStatus(Player inPlayer, String message){
        message2Label = new JLabel(inPlayer.getName() + ", " + message);
    }
    //end game messages
    public void displayWinner(Player winner){
        if (winner == null)
        {
            gameOverLabel1 = new JLabel("Tie Game!");
        }
        else
        {
            gameOverLabel2 = new JLabel("Winner: " + winner);
        }
    }
    public void drawBoard(Graphics g){
        //set controls the scale of the board
        int set = 60;
        int size = board.getSize();
        //creating umbered lines
        for (int c = 1; c <= size + 1; c++){
            g.drawString("" + (c - 1), set/2, c*set + set/2);
            g.drawString("" + (c - 1), c * set + set/2, set/2);
        }
        //places pieces
        for(int c = 0; c < size; c++) {
            for(int i = 0; i < size; i++){
                int p = board.getPiece(i, j).getType();
                if (p != Piece.BLANK){
                    if (p == Piece.BLACKPIECE){
                        g.setColor(Color.BLACK);
                    }
                    else{
                        g.setColor(Color.WHITE);
                    }
                    g.fillOval(set + set * i  + 10, set + set * c + 10,
                               set - 20, set - 20);
                }
            }
        }
    }
    //gets the move
    public Move getMove(Player p){
        int row, col;
        row = Integer.parseInt(rowField.getText());
        col = Integer.parseInt(columnField.getText());
        Move m = new Move(p, row, col);
        return m;
    }
    //Setting for graphics
    public void setGraphics(){
        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        myCanvas = new Canvas(){
            public void boardStart(Graphis g){
                super.paint(g);
                drawBoard(g);
            }
        
        };
        //Setting buttons
        moveButton = new JButton("Move!");
        saveButton = new JButton("Save!");
        startButton = new JButton("Start!");
        quitButton = new JButton("Quit!");
        //Seting labels
        rowL = new JLabel("Row");
        colL = new JLabel("Column");
        gameOverLabel1 = new JLabel("");
        gameOverLabel2 = new JLabel("");
        message1Label = new JLabel("");
        message2Label = new JLabel("");
        //Setting text fields
        rowTF = new JTextField();
        columnTF = new JTextField();
        //Adding canvas
        pane.add(myCanvas, BoarderLayout.CENTER);
        //Modifyibg Canvas
        myCanvas.setBackground(Color.GREEN);
        //Adding JPanel
        JPanel paneComp = new JPanel();
        paneComp.setLayout(new GridLayout(0, 2));
        paneComp.add(gameOverLabel1);
        paneComp.add(gameOverLabel2);
        paneComp.add(message1Label);
        paneComp.add(message2Label);
        paneComp.add(rowL);
        paneComp.add(rowTF);
        paneComp.add(colL);
        paneComp.add(columnTF);
        paneComp.add(moveButton);
        paneComp.add(saveButton);
        paneComp.add(startButton);
        paneComp.add(quitButton);
        //Setting non-start items to false
        moveButton.setEnabled(false);
        saveButton.setEnabled(false);
        rowTF.setEnabled(false);
        columnTF.setEnabled(false);
        //Adding Listeners
        moveButton.addActionListener(new GameActionListener);
        saveButton.addActionListener(new GameActionListener);
        startButton.addActionListener(new GameActionListener);
        quitButton.addActionListener(new GameActionListener);
        //Setting bascs for the GUI
        setTitle("Reversi Game!");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1000, 800);
        pane.setBackground(Color.GRAY);
        
        
    }
    //inner class for action listener
    class GameActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event){
            String command = event.getActionCommand();
            //quit game
            if (command == "Quit!"){
                System.exit(0);
            }
            else if (command == "Start!"){
                //Enable the buttons for use
                rowTF.setEnabled(true);
                columnTF.setEnabled(true);
                moveButton.setEnabled(true);
                saveButton.setEnabled(true);
                startButton.setEnabled(false);
            }
            //save game
            else if (command == "Save!"){
                board.saveGame(JOptionPane.showInputDialog(this, "Game Name"));
                displayMessage("Saved!");
            }
            //makes moce
            else if (command == "Move!"){
               Move m = getMove(board.getCurrentPlayer());
               //test if move is legal
               if(board.moveLegal(m) == true){
                   board.makeMove(m);
                   board.setCurrentPlayer(board.getNExtPlayer());
                   myCanvas.repaint();
               }
               //illegal move
               else
                   displayStatus("Illegal Move!");
               displayStatus(board.geCurrentPlayer(), "Your Move!");
               //game over
               if (board.gameOver()){
                   Player p = board.getWinner();
                   displayWinner(p);
                   //Setting items to false when game ends
                   rowTF.setEnabled(false);
                   columnTF.setEnabled(false);
                   moveButton.setEnabled(false);
                   saveButton.setEnabled(false);
                   startButton.setEnabled(false);
                       
               }
               //Skip turn
               if (board.moveExist(board.getCurrentPlayer()) == false)
                   board.setCurrentPlayer(board.getNextPlayer());
            }
        }
    }
}

以下是我在编译时收到的错误:  ---- jGRASP exec:javac -g GUIDisplay.java

GUIDisplay.java:111:错误:'('或'['预期         moveButton.addActionListener(new GameActionListener);                                                            ^ GUIDisplay.java:112:错误:'('或'['预期         saveButton.addActionListener(new GameActionListener);                                                            ^ GUIDisplay.java:113:错误:'('或'['预期         startButton.addActionListener(new GameActionListener);                                                             ^ GUIDisplay.java:114:错误:'('或'['预期         quitButton.addActionListener(new GameActionListener);                                                            ^ 4个错误

---- jGRASP wedge2:进程的退出代码为1。  ---- jGRASP:操作完成。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

GameActionListener是一个类,因此当您创建新实例时,您可能希望像new GameActionListener()

那样调用构造函数