【问题标题】:put JLabel on top of the JPanel将 JLabel 放在 JPanel 之上
【发布时间】:2012-06-29 06:49:52
【问题描述】:

当我运行我的代码时,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();
      }
}

【问题讨论】:

  • 我觉得JLabel的Z index比JPanel的要小所以尽量增加JLabel的Z index
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 你为什么要扩展 JPanel 你在你的 paintComponent(...) 方法中到底在做什么,你没有用这些线在上面画任何东西。

标签: java swing jlabel graphics2d paintcomponent


【解决方案1】:

您从未使用过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());
    }
}

【讨论】:

    【解决方案2】:

    根据您要添加到 JLabel 的文本,您将paintComponent(Graphics g) 中的标签大小设置为 20 像素宽和 20 像素高。 20 px 宽度不是很宽。如果标签文本的长度超过几个字符,请尝试增加标签宽度。

    【讨论】:

      【解决方案3】:

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

      实际上标签位于 JPanel 的顶部,但是当您为“ColoredRect()”调用/创建对象时,它的位置超出了 jpanel 边界,传递的参数最少为

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

      因为label的位置是x+3,y+3表示77+3+label width 20=100; x+3 表示 17+3+ 标签高度 20=40,

      如果您将超过 77,17 的标签位置传递到面板边界之外

      或更改

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-23
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多