簡體   English   中英

java JTextField paintComponent 方法反復調用。 這正常嗎?

[英]java JTextField paintComponent Method called repeatedly. Is that normal?

有了 JTextField 的子類,我注意到當字段有焦點時,paintComponent 方法被重復調用(大約每半秒一次),即使沒有用戶交互。

我閱讀了 Oracle 文章“在 AWT 和 Swing 中繪畫”,但沒有從中得到任何啟示。

這是正常行為,還是遺漏了什么?

這是我的例子Proggy:
(將 Cursor 定位在第二個 - 沒有子類化 - 沒有日志記錄的 JTextField 會導致具有日志記錄的子類化失去焦點,從而停止重復重繪)

import java.awt.*;
import javax.swing.*;

public class SwingPaintDemo2 extends JFrame {

    public SwingPaintDemo2(final String title) {
        super(title);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Box box = new Box(BoxLayout.Y_AXIS);

        box.add(new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(250, 200);
            }
            @Override
            public void paintComponent(final Graphics g) {
                super  .paintComponent(               g);

                System.out.println("MyPanel.paintComponent......: " + g);

                g.drawString("This is my custom Panel!", 10, 20);
            }
        });
        box.add(new JTextField("JayTekst") {
            @Override
            public void paintComponent(final Graphics g) {
                super  .paintComponent(               g);

                System.out.println("JayTextField.paintComponent.: " + g);
            }
        });
        box.add(new JTextField("JText"));
        this.add(box);
        this.pack();
        this.setVisible(true);
    }
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> new SwingPaintDemo2("Swing Paint Demo"));
    }
}

當然這是正常的。 textfield獲得焦點時,您可以看到 cursor 閃爍,這意味着您看到帶有新視覺表示的textfield ,這意味着paintComponent()

暫無
暫無

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

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