在java中,我希望有两个矩形,一个在另一个后面来模拟一个健康栏,它们不起作用

时间:2012-04-24 16:43:01

标签: java jpanel jlabel jcomponent

我在GameFrame中创建了两个rectangleComponents,但它们永远不会在同一个位置,尽管组件使用相同的X,Y Coords构建。有人知道怎么修这个东西吗?我还在顶部绿色矩形上制作了一个JLabel,但它从未出现过。 谢谢你的时间。

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);
        createPanels(x);
        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        mainPanel.setLayout(new GridLayout(0, 2, 5, 5));
        repaint();
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
        setLocationByPlatform(true);
        setVisible(true);

    }

    public RectangleComponent getLife()
    {
        return life;
    }

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

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

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

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

public class RectangleComponent extends JComponent
{
    private Color color;
    private int width;
    private int height;
    private RoundRectangle2D roundedRectangle;
    private int origWidth;
    private JLabel label;
    private boolean wantLabel;

    public RectangleComponent(Color color, int width, int height, boolean wantLabel)
    {
        this.width = width;
        this.height = height;
        this.color = color;
        origWidth = width;
        this.wantLabel = wantLabel;
        if(wantLabel)
        {
            label = new JLabel(this.width + "/" + origWidth);
            label.setLabelFor(this);
        }
    }

    public RectangleComponent(Color color, boolean wantLabel)
    {
        width = 125;
        height = 18;
        this.color = color;
        origWidth = width;
        this.wantLabel = wantLabel;
        if(wantLabel)
        {
            label = new JLabel(this.width + "/" + origWidth);
            label.setLabelFor(this);
        }
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D graphics2 = (Graphics2D) g;
        roundedRectangle = new RoundRectangle2D.Float(10, 10, width, height, 10, 10);
        graphics2.setPaint(color);
        graphics2.fill(roundedRectangle);
        graphics2.draw(roundedRectangle); 
        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)
        {
            roundedRectangle.setRoundRect(10, 10, width, height, 10, 10);
            repaint();
        }
        else
            width = 0;
    }

    public void addLife(int amount)
    {
        width += amount;
        if(width < origWidth)
        {
            roundedRectangle.setRoundRect(10, 10, width, height, 10, 10);
            repaint();
        }
        else width = origWidth;
    }
}

1 个答案:

答案 0 :(得分:2)

组件的边界由父组件的布局管理器覆盖。由于mainPanel使用的是GridLayout,因此其中的所有组件都位于不同的网格单元格中。如果您想手动定位这些组件,请将mainPanel的布局管理器设置为null