【问题标题】:java JTextField paintComponent Method called repeatedly. Is that normal?java JTextField paintComponent 方法被反复调用。这正常吗?
【发布时间】:2020-07-24 02:07:18
【问题描述】:

有了 JTextField 的子类,我注意到当字段具有焦点时,paintComponent 方法被重复调用(大约每半秒一次),即使没有用户交互。

我阅读了Oracle文章“在AWT和Swing中绘画”,但没有发现任何启示。

这是正常行为,还是遗漏了什么?

这是我的示例 Proggy:
(将光标定位在第二个 - 不是子类 - 没有日志记录的 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"));
    }
}

【问题讨论】:

    标签: java swing jtextfield paintcomponent


    【解决方案1】:

    当然是正常的。当textfield 获得焦点时,您会看到光标闪烁,这意味着您看到textfield 具有新的视觉表示,即paintComponent()。

    【讨论】:

    • 这是有道理的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多