2

我正在尝试将 JLabel 添加到我的健康栏上,该健康栏是由红色矩形顶部的绿色矩形模拟的。尽管我声明了一个 JLabel 并将其附加到我的 rectangleComponent,但它从未出现过。有任何想法吗?这是我的 rectangleComponent 和它初始化的 Frame。

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

2 回答 2

4

您缺少调用add(label)您的组件。因此,您的标签没有附加到它上面。顺便说一句,我认为如果不是禁止的话,真的不鼓励修改您的paintComponent 中的标签。您应该在修改 widh 和/或 origWidth 时执行该调用。

于 2012-04-25T22:27:05.393 回答
0

在我看来,您在paintComponent 中缺少对label.paint() 的调用。或者,我认为您可以在构造函数中调用 add(label) ,将标签添加为该容器的子项,并让 paintChildren 处理它。

由于您想要的只是一个带有自定义背景的标签,因此最好扩展 JLabel,覆盖paintComponent 来绘制自定义背景,然后调用 super.paintComponent 来绘制前景。

于 2012-04-25T22:41:03.727 回答