如何使我的重置按钮工作

时间:2015-10-23 14:36:14

标签: java user-interface jframe jbutton reset

我正在编写一个生成随机迷宫的代码,每次运行程序时它看起来都不同,但我不能让我的重置按钮工作。以下是我的一些代码:

public class MakeFrame extends JPanel implements ActionListener {

JFrame frame;
JPanel buttonPanel;
JButton solve1;
JButton solve2;
JButton solve3;
JButton clear;
JButton reset;
Maze maze = new Maze();

void buildframe() {
    frame = new JFrame("maze"); //makes a frame, names it maze
    frame.add(this, BorderLayout.CENTER);
    frame.add(maze);

    buttonPanel = new JPanel();
    frame.add(buttonPanel, BorderLayout.NORTH);
    solve1 = new JButton("solve 1"); // create some buttons
    solve2 = new JButton("solve 2");
    solve3 = new JButton("solve 3");
    clear = new JButton("clear");
    reset = new JButton("reset");

    buttonPanel.add(solve1); // add the buttons to a panel
    buttonPanel.add(solve2);
    buttonPanel.add(solve3);
    buttonPanel.add(clear);
    buttonPanel.add(reset);

    solve1.addActionListener(this);// assigns action listeners to buttons
    solve2.addActionListener(this);
    solve3.addActionListener(this);
    clear.addActionListener(this);
    reset.addActionListener(this);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the frame visable, grey and close on exit.
    frame.setSize(455, 320);
    frame.setVisible(true);
    setBackground(Color.GRAY);

}

@Override

 public void actionPerformed(ActionEvent e) {
    if (e.getSource() == reset) {
   frame.add(new Maze());
   repaint();



    }
}
}

我不知道在动作执行的方法中放入什么来使其发挥作用,有人可以帮助我吗?

这是我的课堂迷宫:

public class Maze extends JPanel {

Cell[][] cells = new Cell[22][12];
String[][] route = new String[22][12];
Random random = new Random();

Maze() {
    othersetroute();
    createFloor();

    createWalls();
    setStartEnd();
}



void othersetroute() {
    int x = 1;
    int y = 1;
    int downorright;
    for (int i = 0; i < 50; i++) {
        downorright = random.nextInt(3);
        if (x == 20 && y == 10) {
            break;
        }
        if (x > 20 || y > 10) {
            break;
        }
        if (y == 10 || downorright == 0 || downorright == 1) {
            x++;
        } else {
            y++;
        }
        route[x][y] = "floor";
    }
    for (int a = 0; a < 22; a++) {
        for (int b = 0; b < 12; b++) {

            if (route[a][b] == null) {
                int floororwall;
                floororwall = random.nextInt(4);
                if (floororwall == 0 || floororwall == 1) {
                    route[a][b] = "walls";
                } else {
                    route[a][b] = "floor";
                }
                if (a % 2 == 1 && b % 2 == 1) {
                    route[a][b] = "floor";
                }
                if (a % 2 == 0 && b % 2 == 0) {
                    route[a][b] = "walls";
                }

            }
        }
    }

}

void setRoute() {
    int x = 1;
    int y = 1;
    int leftcounter = 0;
    int rightcounter = 0;
    int upcounter = 0;
    int downcounter = 0;

    for (int i = 0; i < 150; i++) {

        String direction;
        if (x > 20 || y > 10) {
            break;
        } else if (x == 20 && y == 10) {
            break;
        } else if (downcounter == 14 && upcounter == 5 && leftcounter == 10 && rightcounter == 29) {
            break;
        } else if (x == 20) {
            int k = random.nextInt(3);
            if (k == 0) {
                direction = "left";
            } else if (k == 1) {
                direction = "up";
            } else {
                direction = "down";
            }

        } else if (y == 10) {
            int k = random.nextInt(3);
            if (k == 0) {
                direction = "left";
            } else if (k == 1) {
                direction = "up";
            } else {
                direction = "right";
            }
        } else if (x == 1 || y == 1) {
            int k = random.nextInt(3);
            if (k == 0) {
                direction = "down";
            } else {
                direction = "right";
            }

        } else {
            int k = random.nextInt(4);
            if (k == 0) {
                direction = "left";
            } else if (k == 1) {
                direction = "up";
            } else if (k == 2) {
                direction = "right";
            } else {
                direction = "down";
            }
        }
        if (direction.equals("right") && rightcounter < 30) {
            x++;
            rightcounter++;
        } else if (direction.equals("down") && downcounter < 15) {
            y++;
            downcounter++;
        } else if (direction.equals("left") && leftcounter < 11) {
            x = x - 1;
            leftcounter++;
        } else if (direction.equals("up") && upcounter < 6) {
            y = y - 1;
            upcounter++;
        }
        System.out.println(x);
        System.out.println(y);
        route[x][y] = "floor";

    }
    for (int a = 0; a < 22; a++) {
        for (int b = 0; b < 12; b++) {

            if (route[a][b] == null) {
                int floororwall;
                floororwall = random.nextInt(4);
                if (floororwall == 0 || floororwall == 1) {
                    route[a][b] = "walls";
                } else {
                    route[a][b] = "floor";
                }
                if (a % 2 == 1 && b % 2 == 1) {
                    route[a][b] = "floor";
                }
                if (a % 2 == 0 && b % 2 == 0) {
                    route[a][b] = "walls";
                }

            }
        }
    }
}

void createFloor() {
    for (int x = 0; x < 22; x++) {
        for (int y = 0; y < 12; y++) {
            if (route[x][y].equals("walls")) {
                cells[x][y] = new Cell("walls");
            }
            if (route[x][y].equals("floor")) {
                cells[x][y] = new Cell("floor");

            }
        }
    }
}

void createWalls() {

    for (int i = 0; i < 12; i++) {
        cells[0][i] = new Cell("walls");

        cells[21][i] = new Cell("walls");
    }
    for (int i = 0; i < 22; i++) {
        cells[i][0] = new Cell("walls");
        cells[i][11] = new Cell("walls");
    }
    for (int x = 0; x < 22; x++) {
        for (int y = 0; y < 12; y++) {
            if (cells[x][y] == null) {
                cells[x][y] = new Cell("walls");

            }
        }
    }
}

void setStartEnd() {

    cells[1][1] = new Cell("start");
    cells[20][10] = new Cell("end");
}

@Override
public void paintComponent(Graphics g) {

    for (int x = 0; x < 22; x++) {
        for (int y = 0; y < 12; y++) {
            if (cells[x][y].getType().equals("#")) {
                g.setColor(Color.GREEN);
                g.fillRect(x * 20, y * 20, x * 20 + 20, y * 20 + 20);
            }
            if (cells[x][y].getType().equals(" ")) {
                g.setColor(Color.WHITE);
                g.fillRect(x * 20, y * 20, x * 20 + 20, y * 20 + 20);
            }
            if (cells[x][y].getType().equals("S") || cells[x][y].getType().equals("E")) {
                g.setColor(Color.PINK);
                g.fillRect(x * 20, y * 20, x * 20 + 20, y * 20 + 20);
            }
        }
    }

}
void solutionOne(){ // least visited
    int visits[][]= new int [20][10];
    for (int i = 0; i<21; i++){
        for (int j = 0; j<10; j++){
            visits[i][j]=0;
        }
    }
    for (int i = 0; i<100; i++){

    }

}
}

这是我的班级单元格:p

public class Cell {
private String cell;

Cell(String type){
    if (type.equals("walls")){
        walls();
    }
    if (type.equals("floor")){
        Floor();
    }
    if (type.equals("start")){
        start();
    }
    if(type.equals("end")){
        end();
    }
}
void walls(){
    cell = "#";
}
void start(){
    cell = "S";
}
void end(){
    cell = "E";
}
void Floor(){
    cell = " ";
}


public String getType(){
return cell;
}



}

2 个答案:

答案 0 :(得分:1)

你有太多的这个

<div class="header">
    <img class="banner" src="//notrealdomain1.com/banner.png">
</div>

/* All in one selector */
.banner {
    display: block;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background: url(http://notrealdomain2.com/newbanner.png) no-repeat;
    width: 180px; /* Width of new image */
    height: 236px; /* Height of new image */
    padding-left: 180px; /* Equal to width of new image */
}

哪个会生成另一个JPanel,但我找不到任何运行整个代码的主要方法,所以在这里你去,享受xD

<强> MakeFrame.java

MakeFrame extends JPanel

<强> TestMaze.java

公共类TestMaze {

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MakeFrame extends JFrame implements ActionListener{


JPanel buttonPanel;
JButton solve1;
JButton solve2;
JButton solve3;
JButton clear;
JButton reset;

Maze maze;

public MakeFrame(){
    super("Maze");
    init();
}

public void init() {
     //makes a frame, names it maze
    maze = new Maze();
    super.getContentPane().add(maze, BorderLayout.CENTER);

    buttonPanel = new JPanel();
    super.getContentPane().add(buttonPanel, BorderLayout.NORTH);
    solve1 = new JButton("solve 1"); // create some buttons
    solve2 = new JButton("solve 2");
    solve3 = new JButton("solve 3");
    clear = new JButton("clear");
    reset = new JButton("reset");

    buttonPanel.add(solve1); // add the buttons to a panel
    buttonPanel.add(solve2);
    buttonPanel.add(solve3);
    buttonPanel.add(clear);
    buttonPanel.add(reset);

    solve1.addActionListener(this);// assigns action listeners to buttons
    solve2.addActionListener(this);
    solve3.addActionListener(this);
    clear.addActionListener(this);
    reset.addActionListener(this);

    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    // makes the frame visable, grey and close on exit.
    super.setSize(455, 320);
    super.setVisible(true);
    super.setBackground(Color.GRAY);

}

public void repaint(){
    init();
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == reset) {
        super.getContentPane().removeAll();
        repaint();
    }
}
}

答案 1 :(得分:0)

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField.getText(");  
}