2012-04-25 62 views
1

我正在試圖將JLabel添加到我的健康欄上,該欄是由紅色矩形頂部的綠色矩形模擬的。儘管我聲明瞭一個JLabel並將其附加到我的rectangleComponent,但它永遠不會顯示出來。有任何想法嗎? 這是我的rectangleComponent和它被初始化的Frame。在Java中,如何將JLabel添加到矩形?

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Rectangle2D; 

import javax.swing.JComponent; 
import javax.swing.JLabel; 

public class RectangleComponent extends JComponent 
{ 
    private Color color; 
    private int width; 
    private int height; 
    private int origWidth; 
    private JLabel label; 
    private Rectangle2D rectangle; 
    private boolean wantLabel; 
    private int xCoord; 
    private int yCoord; 

    public RectangleComponent(int x, int y, Color color, int width, int height, boolean wantLabel) 
    { 
     this.width = width; 
     this.height = height; 
     this.color = color; 
     origWidth = width; 
     xCoord = x; 
     yCoord = y; 
     this.wantLabel = wantLabel; 
     if(wantLabel) 
     { 
      label = new JLabel(this.width + "/" + origWidth); 
      label.setLabelFor(this); 
     } 
     setBounds(xCoord, yCoord, width, height); 
     rectangle = new Rectangle2D.Float(xCoord, yCoord, width, height); 
    } 

    public RectangleComponent(int x, int y, Color color, boolean wantLabel) 
    { 
     width = 125; 
     height = 18; 
     xCoord = x; 
     yCoord = y; 
     this.color = color; 
     origWidth = width; 
     this.wantLabel = wantLabel; 
     if(wantLabel) 
     { 
      label = new JLabel(this.width + "/" + origWidth); 
      label.setLabelFor(this); 
     } 
     setBounds(xCoord, yCoord, width, height); 
     rectangle = new Rectangle2D.Float(xCoord, yCoord, width, height); 
    } 

    public void paintComponent(Graphics g) 
    { 
     Graphics2D graphics2 = (Graphics2D) g; 
     rectangle = new Rectangle2D.Float(xCoord, yCoord, width, height); 
     graphics2.setPaint(color); 
     graphics2.fill(rectangle); 
     graphics2.draw(rectangle); 
     if(wantLabel) 
      label.setText(this.width + "/" + origWidth); 
    } 

    public Dimension getPreferredSize() 
    { 
     return (new Dimension(width, height)); 
    } 

    public void subtractLife(int amount) 
    { 
     width -= amount; 
     if(width > 0) 
     { 
      rectangle.setRect(xCoord, yCoord, width, height); 
      repaint(); 
     } 
     else 
      width = 0; 
    } 

    public void addLife(int amount) 
    { 
     width += amount; 
     if(width < origWidth) 
     { 
      rectangle.setRect(xCoord, yCoord, width, height); 
      repaint(); 
     } 
     else width = origWidth; 
    } 
} 


import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class GameFrame extends JFrame 
{ 
    private SpellBarComponent bar; 
    private JPanel mainPanel = new JPanel(); 
    private JPanel buttonPanel = new JPanel(); 
    Color green = new Color(29, 180, 29); 
    Color red = new Color(255, 0, 0); 
    private RectangleComponent life; 
    private RectangleComponent death; 

    public GameFrame(char x) 
    { 
     setSize(1024, 768); 
     setTitle("Game"); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     mainPanel.setLayout(null); 
     createPanels(x); 
     buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     repaint(); 
     getContentPane().add(mainPanel, BorderLayout.CENTER); 
     getContentPane().add(buttonPanel, BorderLayout.PAGE_END); 
     setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); 
     setLocationByPlatform(true); 
     life.subtractLife(10); 
     setVisible(true); 

    } 

    public RectangleComponent getLife() 
    { 
     return life; 
    } 

    private void createHealth() 
    { 
     life = new RectangleComponent(0, 0, green, true); 
     death = new RectangleComponent(0, 0, red, true); 
    } 

    private void createPanels(char x) 
    { 
     createBar(x); 
     createHealth(); 
     mainPanel.add(buttonPanel); 
     mainPanel.add(life); 
     mainPanel.add(death); 
     buttonPanel.add(bar.getSpell1()); 
     buttonPanel.add(bar.getSpell2()); 
     buttonPanel.add(bar.getSpell3()); 
    } 

    private void createBar(char x) 
    { 
     bar = new SpellBarComponent(x); 
    } 
} 

回答

4

您錯過了在組件中調用add(label)的信息。因此您的標籤不附在其上。順便說一句,我認爲它真的被禁止,如果不是禁止的話,修改paintComponent中的標籤。您應該在修改widh和/或origWidth時執行該調用。

+0

我打電話給label.setLabelFor(這個),我認爲它加了。在我的構造函數中爲RectangleComponent調用add(label)並不能解決問題。 – keyert 2012-04-26 20:47:11

+1

@keyert這是因爲您擴展了JComponent而沒有設置標籤邊界或設置LayoutManager。你的標籤可能有一個寬度和高度爲0. RectangleComponent可以擴展JPanel。此外,設置setLabelFor不會將標籤添加到組件,這僅用於助記符:http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setLabelFor(java。 awt.Component)。最後,不是使用布爾標誌來顯示或不使用標籤,而是使用標籤上的setVisible。 – 2012-04-26 22:16:37

0

在我看來你錯過了paintComponent中label.paint()的調用。或者,我認爲你可以在你的構造函數中調用add(label),將該標籤添加爲此容器的子項,並讓paintChildren處理它。

因爲所有你想要的都是帶有自定義背景的標籤,所以最好是擴展JLabel,重寫paintComponent來繪製自定義背景,然後調用super.paintComponent來繪製前景。

+0

我嘗試在我的paintComponent中添加label.paint(g),但這並沒有解決問題。在我的構造函數中調用add(label)也沒有。 – keyert 2012-04-26 20:52:16