为什么我的JLabel没有出现?

时间:2018-05-28 02:50:02

标签: java swing jpanel jlabel

我做了很多研究,我仍然不确定如何解决这个问题。我正在尝试制作游戏,在其中我需要一个图标在屏幕上的各个位置显示。现在,我只是想让图标可见。

我有一个处理键盘输入的类(名为KeyInputHandler)和另一个创建窗口和背景的类(名为DrawGameBoard)。在DrawGameBoard中,我有一个名为moveIcon的方法,它应该显示一个Icon。

public class DrawGameBoard extends JPanel
{ 
 public static DrawGameBoard panel = new DrawGameBoard();
 public static JFrame window = new JFrame("Fill the Boxes");
 public static Container c = window.getContentPane();

 public void moveIcon(int x, int y, JLabel label)
{
  c.add(label);
  //label.setLocation(x,y);
  c.validate();
  c.repaint();
  System.out.println("tried to show a label");
}
public void paintComponent(Graphics g) 
{
 super.paintComponent(g);
 g.setColor(Color.GRAY);
 g.fillRoundRect(5, 5, 585, 585, 15, 15);
 for(int x = 25; x < 515; x+=79)
 {
   for(int y = 25; y< 515; y+=79)
   {
     g.setColor(Color.BLACK);
     g.fillRect(x, y, 68, 68);
     g.setColor(Color.WHITE);
     g.fillRect(x+10, y+10, 48, 48);
   } 
 }
} 
   public static void main(String[] args)
 {
 KeyInputHandler k = new KeyInputHandler();

//create the window
//JFrame window = new JFrame("Fill the Boxes");
window.setBounds(0, 0, 600, 630);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);

//create the panel
//DrawGameBoard panel = new DrawGameBoard();
//Container c = window.getContentPane();
panel.setBackground(new Color(0,222,222));
panel.addKeyListener(k);
panel.setFocusable(true);
//panel.setLayout(null);//doesnt use default locations

c.add(panel);
window.setVisible(true);
}
}
}

这在KeyInputHandler中调用

   public void keyPressed(KeyEvent e)
   {
    System.out.println("keyPressed: ");
    BoxGame b = new BoxGame();
    DrawGameBoard d = new DrawGameBoard();

    //create icons to be moved
    MovingPlayerIcon icon1 = new MovingPlayerIcon(1,0);
    JLabel p1fill = new JLabel();
    p1fill.setIcon(icon1);

////////////////////////////////////////////////////////////////     
    //controls for p1
    if (e.getKeyCode() == KeyEvent.VK_A) {
      if(b.getX1()>0){
        b.setPos(b.getX1()-1, b.getY1(), 1);
      }
    System.out.println("A");
    d.moveIcon(0,0,p1fill);
    }
////////////////////////////////////////////////////////////////    
}

所以,当我点击A&#39; key,moveIcon方法被调用。我知道正在调用moveIcon方法,因为在按下A键时,&#34; A&#34;打印并且&#34;试图显示标签&#34;打印出来。我尝试用普通文本JLabel替换我的图标,并且没有显示。

其他信息:

我在moveIcon类中注释了setLocation(),因为在我将JPanel的Layout设置为null之前(它已经不再设置了)。 (我想把图标放在特定的位置,但我现在并不担心这一点)

BoxGame是一个处理有关玩家位置信息的类,例如当前的X和Y值。它不应该影响显示器。

MovingPlayerIcon是一个使用基于参数的颜色绘制图标的类。同样,我不认为这会影响显示,因为我尝试用普通文本JLabel替换图标,也没有发生任何事情。

那么,任何想法为什么JLabel都没有出现?

我已经用Java编程了一年多一点。我非常感谢您的时间和帮助。如果您需要任何其他信息,请告诉我(我尽可能具体)。非常感谢你!

1 个答案:

答案 0 :(得分:0)

因此,您需要将标签作为关键字&#39; A&#39;被压了。 您可以使用&#39; paintComponent&#39;而不是使用您的方法&#39; moveIcon&#39;,而只需在所需的位置和所需的时间绘制标签。方法。 在您的班级中声明这些变量 ..

boolean paint = false;
int x = 100;
int y = 100;
  • 现在,将以下代码行添加到&#39; paintComponent&#39;方法

    if(paint == true){  g.drawString(&#34;你想要的文字&#34;,x,y); }

  • 制作&#39; KeyInputHandler&#39;作为“绘制游戏板”的内部阶级。类

  • 现在,在你的&#39; keyPressed&#39;方法,执行以下代码行,当&#39; A&#39;键被按下

    DrawGameBoard.this.paint = true; DrawGameBoard.this.x = 100; DrawGameBoard.this.y = 100; 重绘();