Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java JTextField paintComponent方法被反复调用。这正常吗?_Java_Swing_Jtextfield_Paintcomponent - Fatal编程技术网

java JTextField paintComponent方法被反复调用。这正常吗?

java JTextField paintComponent方法被反复调用。这正常吗?,java,swing,jtextfield,paintcomponent,Java,Swing,Jtextfield,Paintcomponent,将JTextField子类化后,我注意到当字段具有焦点时,即使没有用户交互,paintComponent方法也会被重复调用(大约每半秒一次) 我读了甲骨文的文章“在AWT和Swing中绘画”,但没有发现任何启示 这是正常的行为,还是错过了什么 下面是我的示例程序: (将光标定位在没有日志记录的第二个-非子类-JText字段中,会导致有日志记录的子类字段失去焦点,从而停止重复重新绘制) 当然这是正常的。当textfield具有焦点时,您可以看到光标闪烁,这意味着您可以看到带有新视觉表示的textf

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

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

这是正常的行为,还是错过了什么

下面是我的示例程序:
(将光标定位在没有日志记录的第二个-非子类-JText字段中,会导致有日志记录的子类字段失去焦点,从而停止重复重新绘制)


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

有意义。非常感谢。
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"));
    }
}