将JLabel置于JPanel之上

use*_*790 2 java swing jlabel graphics2d paintcomponent

当我运行我的代码然后JLabel在后面看JPanel.为什么会这样?我必须在面板顶部显示标签.

public class ColoredRect extends JPanel{

      public double x, y, width, height;  
      public JLabel name;

      public ColoredRect(double x,double y,String label)
      {
          name = new JLabel(label);
          this.x = x;
          this.y = y;
          this.width = 100;
          this.height =40;

          setLocation((int)x,(int)y);
          setSize((int)width,(int)height);
          setBackground(Color.red);

          add(name);
       }

       public void paintComponent(Graphics g) {
            // Draw all the rects in the ArrayList.
        super.paintComponent(g);  // Fills with background color, white.
        name.setForeground(Color.BLACK);
        name.setVisible(true);
        name.setLocation((int)x+3, (int)y+3);
        name.setSize(20, 20);
        name.repaint();
       }

       public void setnewPosition(double x, double y)
      {
          this.x =x;
          this.y =y;
          this.setLocation((int)x,(int) y);
          repaint();
      }
}
Run Code Online (Sandbox Code Playgroud)

nIc*_*cOw 5

您从未使用setOpaque()方法将其值设置为OPAQUE.这里看一下这个例子,看看你如何绘制JPanel并添加JLabel它.

import java.awt.*;
import javax.swing.*;

public class PanelPaintingExample
{
    private ColouredRectangle cRect, cRect1, cRect2;    
    private Rectangle rect;

    public PanelPaintingExample()
    {       
        rect = new Rectangle(0, 0, 200, 30);
    }

    private void displayGUI()
    {   
        JFrame frame = new JFrame("Panel Painting Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        cRect = new ColouredRectangle(Color.RED, "LABEL 1"
                                               , Color.WHITE
                                               , rect);
        cRect1 = new ColouredRectangle(Color.BLUE, "LABEL 2"
                                               , Color.WHITE
                                               , rect);
        cRect2 = new ColouredRectangle(Color.MAGENTA, "LABEL 3"
                                               , Color.WHITE
                                               , rect);                                     

        frame.add(cRect);
        frame.add(cRect1);
        frame.add(cRect2);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PanelPaintingExample().displayGUI();
            }
        });
    }
}

class ColouredRectangle extends JPanel
{
    private Color backColour;
    private Color foreColour;
    private String text;
    private Rectangle rect;

    private JLabel label;

    public ColouredRectangle(Color b, String text
                                     , Color f, Rectangle rect)
    {
        this.backColour = b;
        this.foreColour = f;
        this.text = text;
        this.rect = rect;

        label = new JLabel(this.text, JLabel.CENTER);
        label.setOpaque(true);
        label.setBackground(backColour);
        label.setForeground(foreColour);

        add(label);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(200, 30));
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(backColour);
        g.fillRect((int)rect.getX(), (int)rect.getY()
                              , (int)rect.getWidth()
                              , (int)rect.getHeight());
    }
}
Run Code Online (Sandbox Code Playgroud)