首页 > 解决方案 > paintComponent() TextField 导致无限循环(自身 + 父级)

问题描述

经过几天的研究,我请求您的帮助。以下代码有效,但会产生大量 CPU 消耗。似乎这个简单的文本字段在循环中执行“paintComponent”,不仅是它自己的,还有它的父级(JPanel)。你能给我一个纠正的方法吗?

谢谢 :)

package view;

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

import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import constants.Colors;
import constants.Polices;
import constants.Spacing;

public class FieldText extends JTextField 
{
    private static final long serialVersionUID = 4526307090633268880L;
    private int xheight = 96;
    public Boolean hinted = false;                      // Définit si le     contenu == au hint ou si le contenu a été entré par l'utilisateur.
    protected Color bgColor = Colors.INPUT;             // Background normal.
    protected Color textColor = Colors.TEXT_INPUT;      // Couleur du texte normal.
    protected Color bgHinted = Colors.INPUT;            // Background lors que le placeholder est actif.
    protected Color textHinted = Colors.TEXT_INPUT;     // Couleur du texte placeholder.
    protected Font textFont = Polices.INPUTS;           // Police texte utilisateur.
    protected Font textHintFont = Polices.INPUTS_HINT;  // Police     placeholder.

    public FieldText()
    {   
        super();
        init(null, null);
    }


    /**
     * @param text String Texte du champ (valeur) 
     * @param text String PlaceHoler
     */
        public FieldText( String text, String hint )
        {   
            super();
            init(text, hint);
        }

    /**
     * @param hint Sting Texte du champ (valeur).
     */
        public FieldText( String hint )
        {   
            super();
            init(null, hint);
        }


    private void init( String text, String hint )
    {
        setOpaque(false);
        setBackground(bgColor);
        setForeground(textColor);
        setMinimumSize( new Dimension( 200, 64 ) );
        setBorder( new CompoundBorder (
                                                BorderFactory.createMatteBorder(0, 5, 0, 0, Colors.GREEN), 
                                            new EmptyBorder(     Spacing.PADDING_INPUTS )   
                                       ) 
        );
        setFont( Polices.INPUTS );

        if ( text != null && text.length() > 0 )
        {
            setText(text);  
        }

        setHeight(-1);          // Height by default.
    }


    /**
     * Définit la hauteur de l'élément. 
     * @param height int Hauteur à attribuer à l'élément. -1 pour utiliser la hauteur par défaut (xheight).
     */
        public void setHeight( int height )
        {
            setPreferredSize( new Dimension(this.getWidth(), (height>-1)     ? height : xheight) );
        }


    @Override
    protected void paintComponent(Graphics g) 
    {       
        Graphics2D g2d = (Graphics2D)g;

        GradientPaint gp = new GradientPaint (
            0, 0, new Color( 255, 255, 255, 50 ),
            0, 20, new Color( 179, 179, 179, 50 ) 
        );

        g2d.setPaint( gp );
        g2d.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 10, 10);

        super.paintComponent(g2d);

        System.out.println("======> inside FieldText.paintComponent() ");
    }
}`

JTextField 在 JFrame 中的 JPanel 中。

对不起,我的英语不好 ...

这里有一个导致循环的小例子。

编辑: src/views/GuestLoginView 中的示例集成

这是完整的src

标签: javaswingpaintcomponent

解决方案


我发现了问题。

它不在现场,而是在我的自定义按钮的 paintComponent() 中。

我有

  ImageIcon bicon = ( mouseHover ) ? new ImageIcon( iconHover ) : new ImageIcon( icon );
        setIcon(bicon);
        getIcon().paintIcon( this, g2, getIconX(), getIconY() ); 

==> paintComponent中的setIcon不是一个好主意,它会导致循环和 CPU 使用率。

感谢您提供有用的建议。


推荐阅读