-1

在此处输入图像描述我在我的 JPanel 中绘制了一些标志(hh:mm -> time),但是当我更新它并调用 repaint 时,它会覆盖旧字母(它们不会消失)。如何解决这个问题?

4

1 回答 1

2

删除前几行电话后,

revalidate();

然后

repaint();

非常快速的代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestComponent extends JPanel {

    private String drawThis;

    public TestComponent() {
        this.drawThis = "Hello";
        JButton button = new JButton("Change");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                setDrawThis("World");
                repaint();
            }
        }); 
        this.add(button);
    }
    private void drawString(Graphics g, String text, int x, int y) {
        for (String line : text.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawString(g, drawThis, 20, 20);
    }

    public void setDrawThis(String s) {
        this.drawThis = s;
    }

    public static void main(String s[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TestComponent tc = new TestComponent();

        f.add(tc);
        f.setSize(220, 220);
        f.setVisible(true);
    }
}
于 2012-08-18T02:09:06.800 回答