Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 请帮助:当使用GridLayout(20,20)时,JPanel中的JButton网格显示一个JButton,而不是400_Java_Swing_Jpanel_Jbutton_Grid Layout - Fatal编程技术网

Java 请帮助:当使用GridLayout(20,20)时,JPanel中的JButton网格显示一个JButton,而不是400

Java 请帮助:当使用GridLayout(20,20)时,JPanel中的JButton网格显示一个JButton,而不是400,java,swing,jpanel,jbutton,grid-layout,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(Graph

谢谢你试着帮助我

我想创建一个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());

}
}`
import javax.swing.*;
导入java.awt.event.*;
导入java.awt.*;
类节点扩展了JButton{
节点(){
超级();
设置尺寸(新尺寸(20,20));
}
受保护组件(图形g){
超级组件(g);
g、 setColor(Color.white);
g、 fillRect(0,0,getHeight(),getWidth());
}
}
公共类地图
{
静态最终整数n=20;
JPanel p;
公共JPanel init(){
p=新的JPanel();
p、 setLayout(新的GridLayout(n,n));
p、 挫折背景(颜色:黑色);
p、 setFont(新字体(“SansSerif”,Font.BOLD,24));
对于(int i=0;i
我得到了以下输出: 请加载代码并检查。我无法发布图像,因为我缺少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());
}
}
import java.util.ArrayList;
导入javax.swing.*;
导入java.awt.*;
公共类节点扩展了JButton实现{
/*连接到的节点*/
专用节点北;
私有节点东;
私有节点南部;
私有节点西;
私人ArrayList睦邻主义者;
私人诊所;
私人浮动g;
私有浮动h;
私有节点父节点;
私人INTX;
私营企业;
私有布尔等状态;
私有布尔isStart;
私人目标;
节点(整数x,整数y){
超级();
neightourlist=newarraylist();
这个.x=x;
这个。y=y;
this.visted=false;
此.g=整数.MAX_值;
this.isostacle=false;
this.isStart=false;
this.isGoal=false;
}
公共无效集合北(节点北){
//用邻居列表中的新节点替换旧节点
if(neightourlist.contains(this.north))
睦邻主义者。移除(本。北);
睦邻主义者加(北);
//设置新节点
this.north=north;
}
公共空间集合东(节点东){
//用邻居列表中的新节点替换旧节点
if(neighbourList.contains(this.east))
睦邻主义者。移除(本。东);
睦邻主义者加(东);
//设置新节点
this.east=东方;
}
南部公共空间(节点南部){
//用邻居列表中的新节点替换旧节点
if(neightourlist.contains(this.south))
睦邻主义者。移除(本。南);
睦邻主义者(南部);
//设置新节点
这个。南=南;
}
公共空间西侧(节点西侧){
//用邻居列表中的新节点替换旧节点
if(neighbourList.contains(this.west))
睦邻主义者。移除(本。西);
睦邻主义者,加(西);
//设置新节点
this.west=west;
}
公共ArrayList GetNeightourList(){
回归睦邻主义;
}
公共布尔值(){
回访;
}
已访问的公共void集合(已访问的布尔值){
这是参观过的;
}
公共浮动getG(){
返回g;
}
公共无效设置(浮动f){
这个。g=f;
}
公共节点getParent(){
返回父母;
}
公共void setParent(节点父节点){
this.parent=parent;
}
公共浮动getH(){
返回h;
}
公共空间塞斯(浮标h){
这个,h=h;
}
公共int getX(){
返回x;
}
公共int getY(){
返回y;
}
公共布尔等Stable(){
回程等压线;
}
公共无效设置障碍(布尔等状态){
this.isostacle=等stacle;
}
公共布尔值isStart(){
返回isStart;
}
公共void setStart(布尔值isStart){
this.isStart=isStart;
}
公共布尔值isGoal(){
返回目标;
}
public void setGoal(布尔值isGoal){
this.isGoal=isGoal;
}
公共布尔等于(节点){
返回(node.x==x)和&(node.y==y);
}
公共整数比较(节点其他节点){
浮动此与目标的总距离=h+g;
float otherTotalDistanceFromGoal=otherNode.getH()+otherNode.getG();
if(thisTotalDistanceFromGoalotherTotalDistanceFromGoal)
返回1;
其他的
返回0;
}
受保护组件(图形g){
超级组件(g);
g、 setColor(Color.white);
g、 fillRect(0,0,getHeight()-1,getWidth()-1);
}
}
公共类地图{
私有int映射宽度;
私人地图高度;
私有数组列表映射;
私有地址x=0;
私有地址y=0;
私有int goalLocationX=0;
私有int goalLocationY=0;
私有int[][]obstacleMap;
私人JPanel p;
专用记录器日志=新记录器();
图像缓冲区;
映射(int-mapWidth、int-mapHeight、int[][]obstacleMap){
this.mapWidth=mapWidth;
this.mapHeight=mapHeight;
this.obstacleMap=obstacleMap;
createMap();
log.addToLog(“\tMap已创建”);
registerEdges();
log.addToLog(“\tMap节点边缘已注册”);
}
私有void createMap(){
节点;
map=newarraylist();

对于(int x=0;x在
节点
类中,方法
getParent
getX
getY
是超类
JButton
中的重写方法。当版面管理器调用
getX
getY
时,您的方法会被调用,这会混淆版面管理器。重命名您的三个方法o任何其他内容,例如
nodeGetParent
nodeGetX
nodeGetY


这里要学习的经验是,在扩展类时必须小心,不要意外地重写超类的方法。

节点
类中,方法
getParent
getX
getY
被覆盖