面板顶部的图形

时间:2012-02-28 22:09:53

标签: java swing jpanel jlabel paintcomponent

我在使用面板上绘制的图形时出现问题,该面板上面有一个JLabel。我使用自定义paintComponent(Graphics g)方法在Panel上绘制String。我希望字符串显示在JLabel之上而不是JLabel之下,这是当前正在发生的事情。如果你想看到它,那么这是paintComponent方法:

public void paintComponent(Graphics g){
    super.paintComponent(g);

    if(a.shouldAnnotate()){
        FontMetrics size= g.getFontMetrics(); 
        if(getWidth()>=(a.dispX()+size.stringWidth(a.annotationText()))){
            g.setColor(Color.white);
            g.fillRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
            g.setColor(Color.black);
            g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
            g.drawString(a.annotationText(), a.dispX(), a.dispY());
        }else{
            String sub="";
            int letters=0;
            g.setColor(Color.white);
            g.fillRect(a.dispX()-3,a.dispY()-12,getWidth(),15);
            g.setColor(Color.black);

            for(int i=0;i<a.annotationText().length();i++){
                if(a.dispX()+letters+16<=getWidth()){
                    sub+=a.annotationText().substring(i,i+1);
                    letters=size.stringWidth(sub);
                }else{
                    sub=sub+"...";
                    i=a.annotationText().length();
                }
            }
            g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(sub)+3,15);
            g.drawString(sub,a.dispX(),a.dispY());
        }
    }
}

我不太喜欢图形,但我需要了解这个问题的解决方法。

1 个答案:

答案 0 :(得分:4)

为此,我认为您必须覆盖paint或paintChildren,并在那里绘制文本:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.*;

@SuppressWarnings("serial")
public class DrawOverLabel extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = 100;
   private static final Font TEXT_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 32);
   private static final int LABEL_COUNT = 40;

   public DrawOverLabel() {
      for (int i = 0; i < LABEL_COUNT; i++) {
         JLabel label = new JLabel("Label");
         label.setForeground(Color.green);
         label.setFont(label.getFont().deriveFont(Font.BOLD, 20));
         add(label);
      }
   }

   @Override
   public void paint(Graphics g) {
      super.paint(g);

      g.setColor(Color.blue);
      g.setFont(TEXT_FONT);
      g.drawString("in paint", 20, 30);

//      super.paint(g);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);

      g.setColor(Color.red);
      g.setFont(TEXT_FONT);
      g.drawString("in paintComponent", 20, 60);

//      super.paintComponent(g);
   }

   @Override
   protected void paintChildren(Graphics g) {
      super.paintChildren(g);

      g.setColor(Color.gray);
      g.setFont(TEXT_FONT);
      g.drawString("in paintChildren", 20, 100);

//       super.paintChildren(g);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("DrawOverLabel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new DrawOverLabel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

但更好的解决方案是使用JLayeredPane或玻璃板来达到此效果。