JLabel中的JLabel setText()问题

时间:2015-02-23 22:38:52

标签: java swing jframe jpanel jlabel

我是新手编码员,当我在制作一个包含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的左上角和我指定它出现的位置。有趣的是"马车"当真实的标签更新时,左上角的标签也会更新。

提前致谢!

编辑:这是GamePanel的布局。

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

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,你取代paintComponentGamePanel},但不是来电super.paintComponent ......

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

Graphics是一个共享资源,用于绘制在给定绘制周期中更新的所有组件的内容,这意味着,当您收到对Graphics上下文的引用时,它仍然会有&#34;东西&#34;在你面前画上了它。 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 SwingPerforming Custom Painting,了解有关Swing如何绘画的详细信息