如何在gridlayout中编辑标签?

时间:2014-04-08 20:43:35

标签: java arrays swing jlabel grid-layout

我正在为我的java类决赛编写一个鼠和迷宫程序。我正在尝试编辑gridlayout中的标签,以便大鼠看起来正在改变位置。现在,代码还没有完成,所以它还没有工作,但这就是我所拥有的。

import java.io.*;
import java.net.*;
import java.awt.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FinalMaze {

    char[][] mazeArray = new char[][]{
        {'o', 'o', 'o', 'o', 'o', 'o'},
        {'o', 'w', 'w', 'r', 'w', 'o'},
        {'o', 'w', 'p', 'p', 'w', 'o'},
        {'o', 'w', 'p', 'w', 'w', 'o'},
        {'o', 'w', 'p', 'p', 'w', 'o'},
        {'o', 'w', 'w', 'p', 'w', 'o'},
        {'o', 'w', 'w', 'p', 'w', 'o'},
        {'o', 'o', 'o', 'o', 'o', 'o'},};
    static JFrame frame = new JFrame("Maze");
    char[][] ratArray = new char[3][3];
    static int xPos;
    static int yPos;

    public static void main(String[] args) throws Exception {

        new FinalMaze();
    }

    public FinalMaze() throws Exception {
        frame.setSize(1000, 1000);
        frame.setLayout(new GridLayout(0, 6));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        drawMaze();
        run();
    }

    private void drawMaze() {
        for (int i = 0; i < mazeArray.length; i++) {
            for (int j = 0; j < mazeArray[i].length; j++) {
                switch (mazeArray[i][j]) {
                    case 'p':
                        frame.add(new JLabel(""));
                        break;
                    case 'w':
                        frame.add(new JButton(""));
                        break;
                    case 'r':
                        frame.add(new JLabel("RAT"));
                        xPos = j;
                        yPos = i;
                        break;
                    case 'o':
                        JButton temp = new JButton("");
                        temp.setBackground(Color.BLACK);
                        frame.add(temp);
                }
            }
        }
    }

    public void run() throws Exception {
        ServerSocket srvsock = new ServerSocket(13000);
        Socket sock = srvsock.accept();
        PrintStream PS = new PrintStream(sock.getOutputStream());

        InputStreamReader IR = new InputStreamReader(sock.getInputStream());
        BufferedReader BR = new BufferedReader(IR);

        String currentLocation = null;

        while (currentLocation != "ooooooooo") {
            findLocation();
            PS.println(ratArray);
            currentLocation = BR.readLine();
            setLocation(currentLocation);
        }
    }

    private void setLocation(String currentLocation) {
        mazeArray[yPos - 1][xPos - 1] = currentLocation.charAt(0);
        mazeArray[yPos - 1][xPos] = currentLocation.charAt(1);
        mazeArray[yPos - 1][xPos + 1] = currentLocation.charAt(2);
        mazeArray[yPos][xPos - 1] = currentLocation.charAt(3);
        mazeArray[yPos][xPos] = currentLocation.charAt(4);
        mazeArray[yPos][xPos + 1] = currentLocation.charAt(5);
        mazeArray[yPos + 1][xPos - 1] = currentLocation.charAt(6);
        mazeArray[yPos + 1][xPos] = currentLocation.charAt(7);
        mazeArray[yPos + 1][xPos + 1] = currentLocation.charAt(8);

        int tempPos = currentLocation.indexOf('r');
        switch (tempPos) {
            case 1:

                yPos = yPos - 1;
                break;
            case 3:
                xPos = xPos - 1;
                break;
            case 5:
                xPos = xPos + 1;
                break;
            case 7:
                yPos = yPos + 1;
                break;
        }
    }

    private void findLocation() {
        ratArray[1][1] = mazeArray[yPos][xPos];
        ratArray[0][0] = mazeArray[yPos - 1][xPos - 1];
        ratArray[0][1] = mazeArray[yPos - 1][xPos];
        ratArray[0][2] = mazeArray[yPos - 1][xPos + 1];
        ratArray[1][0] = mazeArray[yPos][xPos - 1];
        ratArray[1][2] = mazeArray[yPos][xPos + 1];
        ratArray[2][0] = mazeArray[yPos + 1][xPos - 1];
        ratArray[2][1] = mazeArray[yPos + 1][xPos];
        ratArray[2][2] = mazeArray[yPos + 1][xPos + 1];
    }
}

我需要更改新位置和旧位置的标签,但我不知道如何在gridlayout中编辑标签。

0 个答案:

没有答案