Я создал пользовательский PasswordField, расширяющий JPasswordField .И это работает нормально.Но проблема в том, что когда я использую это поле пароля и запускаю JFrame, это потребляет HIGH CPU Usage.Я только добавляю этот компонент и запускаю его.Но когда я использую другой компонент, такой как JButton или TextField, он работает нормально и загрузка процессора нормальная.Поэтому мне нужно знать, как решить эту проблему или есть ли проблема в моем коде, который ест процессор.Кто-нибудь может мне помочь?Заранее спасибо.
Вот код,
public class PasswordField extends JPasswordField implements ComponentListener
{
private boolean materialMode = false;
private Color borderColorNoFocus = MaterialColor.GREY_300;
private Color borderColorFocus = new Color(0, 112, 192);
private Dimension d = new Dimension(250, 42);
private String placeholder = "";
private Color phColor = new Color(0, 112, 192);
private boolean band = true;
private final JButton button = new JButton();
private ImageIcon iconBlack = new ImageIcon(getClass().getResource("/Library/Images/visibleBlack.png"));
private ImageIcon iconWhite = new ImageIcon(getClass().getResource("/Library/Images/visibleWhite.png"));
private boolean xBlackIcon = false;
private int btnHeight = this.d.height - 10;
private boolean txtVisible = false;
public PasswordField()
{
this.setEchoChar('*');
this.setSize(this.d);
this.setPreferredSize(this.d);
this.setVisible(true);
this.setMargin(new Insets(3, 6, 3, 6));
this.setFont(Roboto.BOLD.deriveFont(14.0F));
this.setForeground(new Color(0, 112, 192));
this.button.setText("");
this.button.setBorderPainted(false);
this.button.setContentAreaFilled(false);
this.button.setMargin(new Insets(2, 2, 2, 2));
this.button.setVisible(true);
this.button.setFocusPainted(false);
this.button.setCursor(new Cursor(12));
this.button.setBackground(new Color(0, 112, 192));
this.add(this.button);
this.setVisible(true);
this.addComponentListener(this);
this.setSelectionColor(this.button.getBackground());
updateButton();
this.getDocument().addDocumentListener(new DocumentListener()
{
@Override
public void removeUpdate(DocumentEvent e)
{
PasswordField.this.band = (PasswordField.this.getText().length() <= 0);
}
@Override
public void insertUpdate(DocumentEvent e)
{
PasswordField.this.band = false;
}
@Override
public void changedUpdate(DocumentEvent de) {}
});
this.button.addMouseListener(new MouseListener()
{
@Override
public void mouseClicked(MouseEvent e)
{
if (PasswordField.this.txtVisible)
{
((JPasswordField)PasswordField.this.button.getParent()).setEchoChar('*');
PasswordField.this.iconBlack = new ImageIcon(getClass().getResource("/Library/Images/visibleBlack.png"));
PasswordField.this.iconWhite = new ImageIcon(getClass().getResource("/Library/Images/visibleWhite.png"));
PasswordField.this.txtVisible = false;
}
else
{
((JPasswordField)PasswordField.this.button.getParent()).setEchoChar('\000');
PasswordField.this.iconBlack = new ImageIcon(getClass().getResource("/Library/Images/invisibleBlack.png"));
PasswordField.this.iconWhite = new ImageIcon(getClass().getResource("/Library/Images/invisibleWhite.png"));
PasswordField.this.txtVisible = true;
}
((JPasswordField)PasswordField.this.button.getParent()).requestFocus();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e)
{
PasswordField.this.button.setOpaque(true);
PasswordField.this.button.setIcon(PasswordField.this.xBlackIcon ? PasswordField.this.iconBlack : PasswordField.this.iconWhite);
}
@Override
public void mouseExited(MouseEvent e)
{
PasswordField.this.button.setOpaque(false);
PasswordField.this.button.setIcon(null);
}
});
this.setPlaceholder("Password Field");
}
public boolean isxDarkIcon()
{
return this.xBlackIcon;
}
public void setxDarkIcon(boolean xDarkIcon)
{
this.xBlackIcon = xDarkIcon;
}
public Color getBotonColor()
{
return this.button.getBackground();
}
public void setBotonColor(Color botonColor)
{
this.button.setBackground(botonColor);
setSelectionColor(this.button.getBackground());
}
public Color getBorderColorFocus()
{
return this.borderColorFocus;
}
public void setBorderColorFocus(Color borderColorFocus)
{
this.borderColorFocus = borderColorFocus;
}
public Color getBorderColorNoFocus()
{
return this.borderColorNoFocus;
}
public void setBorderColorNoFocus(Color borderColorNoFocus)
{
this.borderColorNoFocus = borderColorNoFocus;
}
public boolean isMaterialMode()
{
return this.materialMode;
}
public void setMaterialMode(boolean materialMode)
{
this.materialMode = materialMode;
}
private void updateBorder()
{
Border border = BorderFactory.createMatteBorder(0, 0, 2, 0, this.borderColorFocus);
setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, this.button.getSize().width + 5)));
}
private void updateButton()
{
this.btnHeight = (getSize().height - 10);
this.button.setSize(new Dimension(this.btnHeight, this.btnHeight));
this.button.setPreferredSize(new Dimension(this.btnHeight, this.btnHeight));
this.button.setLocation(getWidth() - this.button.getWidth() - 5, 5);
updateBorder();
}
@Override
public void componentResized(ComponentEvent e)
{
updateButton();
}
@Override
public void componentMoved(ComponentEvent e) {}
@Override
public void componentShown(ComponentEvent e) {}
@Override
public void componentHidden(ComponentEvent e) {}
public void setPlaceholder(String placeholder)
{
this.placeholder = placeholder;
}
public String getPlaceholder()
{
return this.placeholder;
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (isMaterialMode())
{
if (isFocusOwner())
{
Border border = BorderFactory.createMatteBorder(0, 0, 2, 0, this.borderColorFocus);
setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, this.button.getSize().width + 5)));
this.phColor = getForeground();
}
else
{
Border border = BorderFactory.createMatteBorder(0, 0, 2, 0, this.borderColorNoFocus);
setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, this.button.getSize().width + 5)));
this.phColor = this.borderColorNoFocus;
}
}
else
{
Border border = BorderFactory.createMatteBorder(0, 0, 2, 0, this.borderColorFocus);
setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, this.button.getSize().width + 5)));
this.phColor = getForeground();
}
g.setColor(new Color(this.phColor.getRed(), this.phColor.getGreen(), this.phColor.getBlue(), 90));
g.drawString(this.band ? this.placeholder : "",
getMargin().left,
getSize().height / 2 + getFont().getSize() / 2);
}
}