将JLabel放在JPanel的顶部。

时间:2023-02-09 15:52:13

When I run my code then JLabel is looking behind the JPanel. Why this is happening? I have to show the label on top of panel.

当我运行我的代码时,JLabel正在查看JPanel。这是为什么呢?我必须在面板上显示标签。

Code

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

3 个解决方案

#1


5  

You never used the setOpaque(), method to set it's value as being OPAQUE. Here have a look at this example, see how you draw on the JPanel and add JLabel on it.

您从未使用setOpaque()方法来设置其值为不透明。这里有一个示例,看看如何在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());
    }
}

#2


1  

Depending on what text you want to add to your JLabel, you set the size of the label in paintComponent(Graphics g) to be 20 px wide and 20 px height. 20 px width isn't very wide. Try to increase the label width if you have a label text longer than a couple of chars.

根据您想要添加到JLabel的文本,您设置了在paintComponent(图形g)中标签的大小为20像素宽,20像素高。20像素宽度不是很宽。如果标签文本的长度超过了两个字符,那么试着增加标签的宽度。

#3


1  

I checked entire code and executed

我检查了整个代码并执行了。

actually the label is on the top of the JPanel but it location gone to out of the jpanel boundary when you calling/object creating for "ColoredRect()" pass the parameters as minimum as

实际上,标签位于JPanel的顶部,但是当您调用/对象为“ColoredRect()”创建“ColoredRect()”时,该标签位于JPanel边界之外。

//pass the values and check it
ColoredRect(77,17,"string");

because location of label is x+3 and y+3 means 77+3+label width 20=100; x+3 means 17+3+ label heigth 20=40,

因为标签的位置是x+3 y+3意味着77+3+标签宽度20=100;x+3意味着17+3+标签heigth 20=40,

if you pass more than 77,17 the label location out of panel boundary

如果您超过了77,17的标签位置超出了面板边界。

OR change the

或改变

this.width = 1000;
this.height =500;

#1


5  

You never used the setOpaque(), method to set it's value as being OPAQUE. Here have a look at this example, see how you draw on the JPanel and add JLabel on it.

您从未使用setOpaque()方法来设置其值为不透明。这里有一个示例,看看如何在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());
    }
}

#2


1  

Depending on what text you want to add to your JLabel, you set the size of the label in paintComponent(Graphics g) to be 20 px wide and 20 px height. 20 px width isn't very wide. Try to increase the label width if you have a label text longer than a couple of chars.

根据您想要添加到JLabel的文本,您设置了在paintComponent(图形g)中标签的大小为20像素宽,20像素高。20像素宽度不是很宽。如果标签文本的长度超过了两个字符,那么试着增加标签的宽度。

#3


1  

I checked entire code and executed

我检查了整个代码并执行了。

actually the label is on the top of the JPanel but it location gone to out of the jpanel boundary when you calling/object creating for "ColoredRect()" pass the parameters as minimum as

实际上,标签位于JPanel的顶部,但是当您调用/对象为“ColoredRect()”创建“ColoredRect()”时,该标签位于JPanel边界之外。

//pass the values and check it
ColoredRect(77,17,"string");

because location of label is x+3 and y+3 means 77+3+label width 20=100; x+3 means 17+3+ label heigth 20=40,

因为标签的位置是x+3 y+3意味着77+3+标签宽度20=100;x+3意味着17+3+标签heigth 20=40,

if you pass more than 77,17 the label location out of panel boundary

如果您超过了77,17的标签位置超出了面板边界。

OR change the

或改变

this.width = 1000;
this.height =500;