java - 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 中的示例集成
解决方案
我发现了问题。
它不在现场,而是在我的自定义按钮的 paintComponent() 中。
我有
ImageIcon bicon = ( mouseHover ) ? new ImageIcon( iconHover ) : new ImageIcon( icon );
setIcon(bicon);
getIcon().paintIcon( this, g2, getIconX(), getIconY() );
==> paintComponent中的setIcon不是一个好主意,它会导致循环和 CPU 使用率。
感谢您提供有用的建议。
推荐阅读
- python-3.x - 在 Dash/Plotly 中显示 PIL 图像
- javascript - 在反应路由器中切换页面时未加载js文件
- netsuite - Netsuite - 发送电子邮件操作按钮不起作用
- r - 如何在 visNetwork 中显示非彩色图形且易于阅读的文本?
- python - Pyspark + Anaconda 上的 IllegalArgumentException
- c# - Azure webjob convert time to Universal time
- html - 如何控制在社交媒体或移动设备上分享网页时显示的图像?
- maven - 使用 Kafka 运行 Apache Beam 时出现依赖性错误
- c# - Xamarin Forms Bind 命令在 ItemSource 之外
- javascript - 有没有办法对使用 jest 从子 React 组件调用的不可访问的回调函数进行单元测试