簡體   English   中英

自定義JPasswordField中的CPU使用率高

[英]High CPU usage in Custom JPasswordField

我創建了一個自定義PasswordField,它擴展了JPasswordField 而且工作正常。 但是問題是當我使用此密碼字段並運行JFrame時,這會消耗大量CPU使用率。 我只添加並運行它。 但是,當我使用其他組件(如JButton或TextField)時,它可以正常工作,並且CPU使用率正常。 因此,我需要知道如何解決該問題,或者我的代碼中是否存在占用CPU的問題。 有誰能夠幫助我? 提前致謝。

這是代碼,

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);
    }
}

我已經在機器上重現了您的Java代碼並對其進行了調試。
我發現了為什么您的代碼會導致CPU高。

您在paintComponent()方法中使用了setBorder()方法,這是錯誤的,因為如果查看父類中的setBorder()方法,您將看到,如果邊框已更改,則此方法始終調用repaint() (這很正常)

因此,如果您在paint()方法中調用了一些會調用repaint()的方法,那么您將陷入無限循環。

paint() -> repaint() -> paint() -> repaint() -> paint().....

在您的代碼中,您使用了setBorder 3次。

如果您將setBorder代碼外包並從其他地方調用,則可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM