-1

次のコードを使用して、あるクラスから別のクラスに座標を渡しています

**詳細を表示するために編集

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);
            }
        }  
    }

class2 から:

    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);
    }

これにより、これら 2 つの値が最初のクラス内の座標ハンドラー メソッドに渡されます。

このクラス内には、さまざまなメソッドで使用される xCoordinate 変数があります。問題は、これを設定できないように見えることです。xCoordinate と yCoordinate のこのメソッドの外では常に 0 が返されます。その理由がわかりません。上記の行で問題ないSystem.out.println("Test "+xCoordinate + " " +yCoordinate);ようです。

4

1 に答える 1

1

私自身でそれを理解しただけで、実際には非常に単純で、アクションリスナーは基本的にすべての「イベント」でゼロ値を説明する変数を再インスタンス化し、リスナーの外でこのプロセスを実行するとすぐに、意図したとおりに値を設定しました。入力してくれてありがとう、これが途中で誰かを助けることを願っています!

于 2012-04-12T16:52:27.330 に答える