2012-12-20 93 views
2

我正在尝试制作一个基本地图,其中按下按钮可移动一个字符。 我正在使用Netbeans,并且到目前为止它进展顺利!除了试图从JPanel中删除JLabel,然后添加一个新的JLabel。从JPanel中添加/删除

这里是我的全码:

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

public class startMap extends JFrame { 

public int locX = 1; 
public int locY = 1; 
ImageIcon tile = new ImageIcon("src/tile.png"); 
JLabel tileLabel = new JLabel(tile); 
ImageIcon berry = new ImageIcon("src/berry.png"); 
JLabel berryLabel = new JLabel(berry); 
ImageIcon blank = new ImageIcon("src/blank.png"); 
JLabel blankLabel = new JLabel(blank); 
JLabel testL = new JLabel("LOL"); 
JPanel[][] map = new JPanel[7][7]; 

public startMap() { 
    setBackground(Color.BLACK); 
    setFocusable(true); 
    keyHandler kh = new keyHandler(); 
    addKeyListener(kh); 
    mapGUI(); 
} 

public void mapGUI() { 
    JPanel mainP = new JPanel(); 
    mainP.setBackground(Color.BLACK); 
    mainP.setLayout(new FlowLayout()); 
    for (int x = 0; x < 7; x++) { 
     for (int i = 0; i < 7; i++) { 
      map[x][i] = new JPanel(); 
      System.out.println("1"); 
      blankLabel = new JLabel(blank); 
      map[x][i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); 
      map[x][i].add(blankLabel); 
     } 
    } 
    for (int x = 1; x < 6; x++) { 
     for (int i = 1; i < 6; i++) { 
      System.out.println("2"); 
      map[x][i].removeAll(); 
      revalidate(); 
      repaint(); 
      tileLabel = new JLabel(tile); 
      map[x][i].add(tileLabel); 
      revalidate(); 
      repaint(); 
     } 
    } 
    System.out.println("3"); 
    map[locX][locY].removeAll(); 
    revalidate(); 
    repaint(); 
    map[locX][locY].add(berryLabel); 
    revalidate(); 
    repaint(); 
    for (int x = 0; x < 7; x++) { 
     for (int i = 0; i < 7; i++) { 
      mainP.add(map[x][i]); 
     } 
    } 

    add(mainP); 
} 

@Override 
public void paint(Graphics g) { 
    super.paint(g); 

} 

private class keyHandler extends KeyAdapter { 

    @Override 
    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      locY+=1; 
      map[locX][locY].removeAll(); 
      revalidate(); 
      repaint(); 
      map[locX][locY].add(berryLabel); 
      revalidate(); 
      repaint(); 


     } 

    } 
} 

}

这里是当用户点击右键有什么变化的平方。

这里是KeyAdapter代码:

private class keyHandler extends KeyAdapter { 

    @Override 
    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      locY+=1; 
      map[locX][locY].removeAll(); 
      revalidate(); 
      repaint(); 
      map[locX][locY].add(berryLabel); 
      revalidate(); 
      repaint(); 



     } 

    }} 

注:系统出刚刚调试方法,我用它来检查什么被称为时。

所以,当我运行它看起来像这样

向右移动并重新验证$重绘:

enter image description here

为什么位于1,1箱子去是灰色的?

帮助搞清楚如何使箱子保持白色正方形而不是变回灰色。

---------------------------- SSCCE ----------------- -----------------

使用完整的代码上面 ,这是主类:

import javax.swing.*; 

public class TestGame extends JFrame { 

public static void main(String[] args) { 
    startMap sm = new startMap(); 
    sm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    sm.setVisible(true); 
    sm.setSize(380,415); 
    sm.setResizable(false); 
} 
} 

修正版本:

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

public class startMap extends JFrame { 

public int locX = 1; 
public int locY = 1; 
ImageIcon tile = new ImageIcon("src/tile.png"); 
JLabel tileLabel = new JLabel(tile); 
ImageIcon berry = new ImageIcon("src/berry.png"); 
JLabel berryLabel = new JLabel(berry); 
ImageIcon blank = new ImageIcon("src/blank.png"); 
JLabel blankLabel = new JLabel(blank); 
JLabel testL = new JLabel("LOL"); 
JPanel[][] map = new JPanel[7][7]; 

public startMap() { 
    setBackground(Color.BLACK); 
    setFocusable(true); 
    keyHandler kh = new keyHandler(); 
    addKeyListener(kh); 
    mapGUI(); 
} 

public void mapGUI() { 
    JPanel mainP = new JPanel(); 
    mainP.setBackground(Color.BLACK); 
    mainP.setLayout(new FlowLayout()); 
    for (int x = 0; x < 7; x++) { 
     for (int i = 0; i < 7; i++) { 
      map[x][i] = new JPanel(); 
      System.out.println("1"); 
      blankLabel = new JLabel(blank); 
      map[x][i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); 
      map[x][i].add(blankLabel); 
     } 
    } 
    for (int x = 1; x < 6; x++) { 
     for (int i = 1; i < 6; i++) { 
      System.out.println("2"); 
      map[x][i].removeAll(); 
      tileLabel = new JLabel(tile); 
      map[x][i].add(tileLabel); 
      revalidate(); 
      repaint(); 
     } 
    } 
    System.out.println("3"); 
    map[locX][locY].removeAll(); 
    map[locX][locY].add(berryLabel); 
    revalidate(); 
    repaint(); 
    for (int x = 0; x < 7; x++) { 
     for (int i = 0; i < 7; i++) { 
      mainP.add(map[x][i]); 
     } 
    } 

    add(mainP); 
} 

@Override 
public void paint(Graphics g) { 
    super.paint(g); 

} 

private class keyHandler extends KeyAdapter { 

    @Override 
    public void keyPressed(KeyEvent e) { 
     if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
      berryLabel = new JLabel(berry); //THIS 
      tileLabel = new JLabel(tile); //And THIS had to be RE initialized idk why. 
      map[locX][locY].removeAll(); 
      map[locX][locY].add(tileLabel); 

      locY += 1; 
      map[locX][locY].removeAll(); 
      map[locX][locY].add(berryLabel); 
      revalidate(); 
      repaint(); 


     } 

    } 
} 

}

回答

3

应该在添加/除去

revalidate(); 
repaint(); 

不要paint()

+0

调用repaint() @StanislavL 我加入他们,我的代码现在是 地图[locX] [locY] .removeAll(); map [locX] [locY] .add(blankLabel); revalidate(); repaint(); locY + = 1; 但现在它只显示没有任何东西的面板,所以它是一个小小的盒子haha –

+0

你的代码中有些东西我们没有看到。张贴SSCCE让我们检查它 – StanislavL

+0

我相信这是一个SSCCE,我是新来的帮助,所以原谅我。 完整的代码在顶部,我刚刚添加的主要类。你应该可以编译和运行两个类! –