java - 请帮助 :Grid of JButtons in a JPanel shows one JButton instead of 400 when GridLayout(20, 20) 使用

标签 java swing jpanel jbutton grid-layout

感谢您尝试帮助我。

我想创建一个 20*20 正方形的网格。这是为了我的 A-Star 算法演示。 为了实验目的,我编写了以下代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Node extends JButton {
Node(){
    super();
    setSize(new Dimension(20,20));
}
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.white);
    g.fillRect(0,0,getHeight(),getWidth());
}
}
public class Map 
{
static final int n = 20;
JPanel p;
public JPanel init() {
    p=new JPanel();
    p.setLayout(new GridLayout(n, n));
    p.setBackground(Color.black);
    p.setFont(new Font("SansSerif", Font.BOLD, 24));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
                p.add(new Node());
        }
    }
    return p;
}
}
public class Display extends JApplet{
   public void init(){
     Map m=new Map();
     add(m.init());

}
}`

我得到以下输出: 请加载代码并检查。我无法发布图片,因为我缺少 10 点声誉

但是,当我尝试将实验代码添加到演示的原始代码时 它没有显示所需的输出。我的原始代码如下:

import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
public class Node extends JButton implements Comparable<Node>  {
/* Nodes that this is connected to */
private Node north;
private Node east;
private Node south;
private Node west;
private ArrayList<Node> neighbourList;
private boolean visited;
private float g;
private float h;
private Node parent;
private int x;
private int y;
private boolean isObstacle;
private boolean isStart;
private boolean isGoal;

Node(int x, int y) {
    super();
    neighbourList = new ArrayList<Node>();
    this.x = x;
    this.y = y;
    this.visited = false;
    this.g = Integer.MAX_VALUE;
    this.isObstacle = false;
    this.isStart = false;
    this.isGoal = false;
}

public void setNorth(Node north) {
    //replace the old Node with the new one in the neighbourList
    if (neighbourList.contains(this.north))
        neighbourList.remove(this.north);
    neighbourList.add(north);
    //set the new Node
    this.north = north;
}

public void setEast(Node east) {
    //replace the old Node with the new one in the neighbourList
    if (neighbourList.contains(this.east))
        neighbourList.remove(this.east);
    neighbourList.add(east);
    //set the new Node
    this.east = east;
}

public void setSouth(Node south) {
    //replace the old Node with the new one in the neighbourList
    if (neighbourList.contains(this.south))
        neighbourList.remove(this.south);
    neighbourList.add(south);
    //set the new Node
    this.south = south;
}

public void setWest(Node west) {
    //replace the old Node with the new one in the neighbourList
    if (neighbourList.contains(this.west))
        neighbourList.remove(this.west);
    neighbourList.add(west);
    //set the new Node
    this.west = west;
}

public ArrayList<Node> getneighbourList() {
    return neighbourList;
}

public boolean isVisited() {
    return visited;
}

public void setVisited(boolean visited) {
    this.visited = visited;
}

public float getG() {
    return g;
}

public void setG(float f) {
    this.g = f;
}

public Node getParent() {
    return parent;
}

public void setParent(Node parent) {
    this.parent = parent;
}

public float getH() {
    return h;
}

public void setH(float h) {
    this.h = h;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public boolean isObstacle() {
    return isObstacle;
}

public void setObstacle(boolean isObstacle) {
    this.isObstacle = isObstacle;
}

public boolean isStart() {
    return isStart;
}

public void setStart(boolean isStart) {
    this.isStart = isStart;
}

public boolean isGoal() {
    return isGoal;
}

public void setGoal(boolean isGoal) {
    this.isGoal = isGoal;
}

public boolean equals(Node node) {
    return (node.x == x) && (node.y == y);
}

public int compareTo(Node otherNode) {
    float thisTotalDistanceFromGoal = h + g;
    float otherTotalDistanceFromGoal = otherNode.getH() + otherNode.getG();
    if (thisTotalDistanceFromGoal < otherTotalDistanceFromGoal) 
        return -1;
    else if (thisTotalDistanceFromGoal > otherTotalDistanceFromGoal) 
        return 1;
    else 
        return 0;
}

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.white);
    g.fillRect(0,0,getHeight()-1,getWidth()-1);
}
}

public class Map {
private int mapWidth;
private int mapHeight;
private ArrayList<ArrayList<Node>> map;
private int startLocationX = 0;
private int startLocationY = 0;
private int goalLocationX = 0;
private int goalLocationY = 0;
private int[][] obstacleMap;
private JPanel p;
private Logger log = new Logger();
Image buffer;

Map(int mapWidth, int mapHeight, int[][] obstacleMap) {
    this.mapWidth = mapWidth;
    this.mapHeight = mapHeight;
    this.obstacleMap = obstacleMap;
    createMap();
    log.addToLog("\tMap Created");
    registerEdges();
    log.addToLog("\tMap Node edges registered");
}

private void createMap() {
    Node node;
    map = new ArrayList<ArrayList<Node>>();
    for (int x=0; x<mapWidth; x++) {
        map.add(new ArrayList<Node>());
        for (int y=0; y<mapHeight; y++) {
            node = new Node(x,y);
            if (obstacleMap[x][y] == 1)
                node.setObstacle(true);
            map.get(x).add(node);
        }
    }
}

/**
 * Registers the nodes edges (connections to its neighbours).
 */
private void registerEdges() {
    for ( int x = 0; x < mapWidth-1; x++ ) {
        for ( int y = 0; y < mapHeight-1; y++ ) {
            Node node = map.get(x).get(y);
            if (!node.isObstacle()){
                if (!(y==0))
                    node.setNorth(map.get(x).get(y-1));
                if (!(x==mapWidth))
                    node.setEast(map.get(x+1).get(y));
                if (!(y==mapHeight))
                    node.setSouth(map.get(x).get(y+1));
                if (!(x==0))
                    node.setWest(map.get(x-1).get(y));
            }
        }
    }
}

public ArrayList<ArrayList<Node>> getNodes() {
    return map;
}

public void setObstacle(int x, int y, boolean isObstacle) {
    map.get(x).get(y).setObstacle(isObstacle);
}

public Node getNode(int x, int y) {
    return map.get(x).get(y);
}

public void setStartLocation(Node start) {
    map.get(startLocationX).get(startLocationY).setStart(false);
    map.get(start.getX()).get(start.getY()).setStart(true);
    startLocationX = start.getX();
    startLocationY = start.getY();
}

public void setStartLocation(int x, int y) {
    map.get(startLocationX).get(startLocationY).setStart(false);
    map.get(x).get(y).setStart(true);
    startLocationX = x;
    startLocationY = y;
}

public void setGoalLocation(Node goal) {
    map.get(goalLocationX).get(goalLocationY).setGoal(false);
    map.get(goal.getX()).get(goal.getY()).setGoal(true);
    goalLocationX = goal.getX();
    goalLocationY = goal.getY();
}

public void setGoalLocation(int x, int y) {
    map.get(goalLocationX).get(goalLocationY).setGoal(false);
    map.get(x).get(y).setGoal(true);
    goalLocationX = x;
    goalLocationY = y;
}

public int getStartLocationX() {
    return startLocationX;
}

public int getStartLocationY() {
    return startLocationY;
}

public Node getStartNode() {
    return map.get(startLocationX).get(startLocationY);
}

public int getGoalLocationX() {
    return goalLocationX;
}

public int getGoalLocationY() {
    return goalLocationY;
}

public Node getGoalLocation() {
    return map.get(goalLocationX).get(goalLocationY);
}

public float getDistanceBetween(Node node1, Node node2) {
    //if the nodes are on top or next to each other, return 1
    if (node1.getX() == node2.getX() || node1.getY() == node2.getY()){
        return 1;
    } else { //if they are diagonal to each other return diagonal distance: sqrt(1^2+1^2)
        return (float) 1.4;
    }
}

public int getMapWidth() {
    return mapWidth;
}

public int getMapHeight() {
    return mapHeight;
}

public JPanel init(){ 
    int n=20;
    p=new JPanel();
    p.setLayout(new GridLayout(n, n));
    p.setSize(400,400);
    p.setFont(new Font("SansSerif", Font.BOLD, 24));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
                p.add(map.get(j).get(i));
        }
    }
    return p;
}

public void clear() {
    startLocationX = 0;
    startLocationY = 0;
    goalLocationX = 0;
    goalLocationY = 0;
    createMap();
    registerEdges();
}

} 

public class Display extends JApplet
{
private static int[][] M =   {{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
        {0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0},
        {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0},
        {0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,1,1,0,0,0},
        {0,1,1,1,1,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0},
        {0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0},
        {1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
        {0,0,0,0,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0},
        {0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0},
        {0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

public void init()
{ 
    JRootPane rootPane = this.getRootPane();    
    rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
    Map m=new Map(20,20,M);
    add(m.init());
}
}

这是我从上面的代码中得到的输出: 请加载代码并检查。我无法发布图片,因为我缺少 10 点声誉

最佳答案

Node 类中,getParentgetXgetY 方法是父类(super class)JButton 中的重写方法。当布局管理器调用 getXgetY 时,您的方法会被调用,这会混淆布局管理器。将三个方法重命名为其他名称,例如 nodeGetParentnodeGetXnodeGetY

这里要吸取的教训是,在扩展类时必须小心,不要意外覆盖父类(super class)的方法。

关于java - 请帮助 :Grid of JButtons in a JPanel shows one JButton instead of 400 when GridLayout(20, 20) 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18943984/

相关文章:

java - 为什么可比实现有效

Java Swing : How to build Connect 4 GUI

java - 调用 pack() 后,JPanel getWidth() 返回零

java - 在 java 中从 JComboBox 获取字符串值

java - PaintComponent 未在 JPanel 中调用

java - 如何在 JFrame 中的另一个 JPanel 之上显示/隐藏 JPanel?

Java Comparator 不比较每个对象

java - Playframework 消息加载问题

java - 二维数组未在网格中打印/不是完整数组?

java - swing java app 和 iframe - 双向通信