简体   繁体   中英

In Java, how do I add a JLabel to a rectangle?

I'm trying to add a JLabel onto my health bar that is a simulated by a green rectangle on top of a red rectangle. Although I declare a JLabel and attach it to my rectangleComponent, it never shows up. Any ideas? Here's my rectangleComponent and the Frame it's intialized into.

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

You are missing to call add(label) in your component. Your label is therefore not attached to it. Btw, I think it is really disencouraged, if not forbidden, to modify the label in your paintComponent. You should rather do that call when widh and/or origWidth are modified.

Looks to me that you're missing a call to label.paint() in paintComponent. Alternatively, I think you could call add(label) in your constructor, to add the label as a child of this container, and let paintChildren take care of it.

Since all you want is a label with a custom background, it may be better to extend JLabel, override paintComponent to paint your custom background, then call super.paintComponent to paint the foreground.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM