问题设置实例变量

时间:2012-04-12 01:20:18

标签: java variables parameters

我使用以下代码将一些坐标从一个类传递到另一个

**已编辑以显示更多详细信息

在class1开始时:

public class BattleGui implements ActionListener {
    private int xCoordinate;
    private int yCoordinate;

    public void coordinateHandler(int xCoord, int yCoord) {
        xCoordinate=xCoord;
        yCoordinate=yCoord;
        System.out.println("Coordinates Received "+xCoord + " " +yCoord);
        System.out.println("Test "+xCoordinate + " " +yCoordinate);
    }
    public void actionPerformed(ActionEvent e) {
        String classname = getClassName(e.getSource());
        JComponent component = (JComponent)(e.getSource());
        cellState cs = new cellState();
        if (classname.equals("JMenuItem")) {
            JMenuItem menusource = (JMenuItem)(e.getSource());
            String menutext  = menusource.getText();

            // Determine which menu option was chosen
            if (menutext.equals("Load Game")) {
                /* BATTLEGUI    Add your code here to handle Load Game **********/
                System.out.println(cs.turnFeedback());
                LoadGame();
            }
            else if (menutext.equals("Save Game")) {
                /* BATTLEGUI    Add your code here to handle Save Game **********/
                SaveGame();
            }
            else if (menutext.equals("New Game")) {
                /* BATTLEGUI    Add your code here to handle Save Game **********/
                NewGame();
            }
        }
        // Handle the event from the user clicking on a command button
        else if (classname.equals("JButton")) {
            JButton button = (JButton)(e.getSource());
            int bnum = Integer.parseInt(button.getActionCommand());
            int row = bnum / GRID_SIZE;
            int col = bnum % GRID_SIZE;
            //col=yCoord;
            //row=xCoord;
            //System.out.println(e.getSource()); 
            //System.out.println(bnum / GRID_SIZE);
            fireShot(row, col);


            if (row==xCoordinate){
                if (col==yCoordinate){
                    button.setBackground(cellColour[cs.turnFeedback()]);
                }
                else {
                //Remember to change the 1 to whatever is reported back from cell class cell state
                //button.setBackground(cellColour[cs.turnFeedback()]);
                button.setBackground(Color.BLUE);
                }
            }
            else {
                button.setBackground(Color.BLUE);
            }
        }  
    }

从第2课开始:

    public void shipDeploy() {
        int gridLength;
        int lengthDraw;        
        int winningNumbers = 0;
        int xCoord;
        int yCoord;

        xCoord=99;
        yCoord=100;
        System.out.println(xCoord + "\n" + yCoord);
        BattleGui bGui = new BattleGui();
        //Passing the coordinates back to the battlegui coordinate handler class
        bGui.coordinateHandler(xCoord, yCoord);
    }

这将这两个值传递给第一个类中的坐标处理程序方法。

在这个类中我有一个xCoordinate变量用于各种方法,问题是我似乎无法设置这个,0总是在xCoordinate和yCoordinate这个方法之外返回而我不明白为什么,因为它们似乎在上面的System.out.println("Test "+xCoordinate + " " +yCoordinate);行中没问题。

1 个答案:

答案 0 :(得分:1)

我自己想出来,非常简单,actionlistener基本上是在解释零值的每个“事件”上重新实例化变量,只要我把这个过程带到它工作的监听器之外并设置值如预期。感谢输入的人,并希望这有助于一路上的人!