【问题标题】:Adding/Removing from a JPanel从 JPanel 添加/删除
【发布时间】:2012-12-07 18:41:15
【问题描述】:

我正在尝试制作一个基本地图,按下按钮移动一个角色。 我正在使用 Netbeans,到目前为止一切顺利!除了尝试从 JPanel 中删除 JLabel,然后向其添加新的 JLabel。

这是我的完整代码:

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();



        }

    }}

注意:系统输出只是我用来检查什么时候被调用的调试方法。

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

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

为什么位于 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();


        }

    }
}

}

【问题讨论】:

    标签: java swing jpanel jlabel keylistener


    【解决方案1】:

    应该在添加/删除后调用

    revalidate();
    repaint();
    

    不要从paint() 拨打repaint()

    【讨论】:

    • @StanislavL 我添加它们,我现在的代码是    map[locX][locY].removeAll();地图[locX][locY].add(blankLabel);重新验证();重绘();位置 += 1;但现在它只显示面板,里面什么都没有,所以它是一个小盒子哈哈
    • 您的代码中有我们看不到的内容。发布 SSCCE 让我们检查一下
    • 我相信这是一个 SSCCE,我是新手,所以请原谅我。完整代码在顶部,我刚刚添加了我的主类。你应该能够编译和运行这两个类!
    • 我修复了我的代码。看来我必须重新初始化 JLabel 才能将其添加到 JPanel 并留下来.. 可能会对此提出疑问
    • 调用 map[locX][locY].revalidate()/repaint() 您从数组成员中添加/删除但重新验证主容器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多