JFrame setText() 问题中的 JLabel

JLabel in JFrame setText() issue

我是一名编码新手,当我在处理一个包含 JFrame、JPanel 和 JLabel 的项目时,我 运行 遇到了 JLabel 出现在两个地方的奇怪问题,我将其设置为位于框架的左上角,而不是它应该位于的位置。

代码的基本概要如下:

import java.awt.event.*;     // for key listener
import java.awt.*;         

import javax.swing.JFrame; 
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.*;

import java.util.Random;

public class Game extends JFrame {



   JFrame Game;
   JLabel label;
   GamePanel panel;   // GamePanel class is the panel class that extends JPanel

   public Game(String title) {

      super(title);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setSize(420 + 110,420 + 220);
      this.setLocationRelativeTo(null); 
      this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
      this.setResizable(false);

      label = new JLabel("Score : " + score);
      label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
      label.setAlignmentY(JLabel.CENTER_ALIGNMENT);

      panel = new GamePanel(..appropriate variables);

      this.add(panel);
      this.add(label);
      this.add(Box.createRigidArea(new Dimension(0,50)));

      panel.setFocusable(true);
      panel.addKeyListener(new KeyHandler());

   }

   // some variables..


   class KeyHandler extends KeyAdapter {

      public void keyPressed(KeyEvent e)
      {
         int keycode = e.getKeyCode(); 

         switch(keycode) {

             //cases for arrow keys, which will update the variable that panel uses

         }

         //some other tasks for the game..

         panel.updateVariables(...appropriate variables to update);
         panel.repaint();
         label.setText("Score : " + score);

      }

   }

   public static void main(String [] args)  {

      Game me = new Game("GAME");        

      me.setVisible(true);


   }

}   

所以问题的总结是,每当 setText 实际更新标签时,标签似乎会出现在两个地方:JFrame 的左上角和我指定它出现的地方。有趣的是,左上角的 "buggy" 标签在更新真实标签时也会更新。

提前致谢!

编辑:这是游戏面板的布局。

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.BasicStroke;

public class GamePanel extends JPanel
{      

   // some variables..

   // setXxx methods for corresponding variables

   void drawCursor(int x, int y)
   {
      //draws thicker rectangle around the selected position
      //uses setStroke, setColor, and draw(Rectangle)
   }

   public void paintComponent( Graphics g )   
   {
      Graphics2D g2 = (Graphics2D) g;         

      for(int i = 0; i < size; i++) {

         for(int j = 0; j < size; j++) {

            switch(gameGrid[j][i]) {   // I have an array grid[][] that has information of which color goes into the specific position of the grid

               // each case will setColor the corresponding color and use g2.fill(new Rectangle(...));
               case 0:
                  g2.setColor(...);
                  g2.fill(new Rectangle(...));
                  break;
               // and so on

            }
            g2.setColor(Color.BLACK);
            g2.draw(new Rectangle(50 + (j * 420/size), 50 + (i * 420/size), 420/size,420/size));         
         }    
      }
      drawCursor(x, y);
   }
}

正如我在评论中所说,您正在覆盖 paintComponent(在 GamePanel 中)但没有调用 super.paintComponent...

public void paintComponent( Graphics g )   
{
    Graphics2D g2 = (Graphics2D) g;   
    for(int i = 0; i < size; i++) { 

Graphics 是一种共享资源,用于绘制在给定绘制周期内更新的所有组件的内容,这意味着,当您收到对 Graphics 上下文的引用时, 它仍然会有你之前画在上面的 "stuff" 。 paintComponent的其中一项工作是清除上下文准备绘画

在进行任何自定义绘画之前请致电 super.paintComponent(g);

public void paintComponent( Graphics g )   
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;   
    for(int i = 0; i < size; i++) { 

查看 Painting in AWT and Swing and Performing Custom Painting 了解有关如何在 Swing 中绘画的详细信息