为什么repaint()方法在这里不起作用?

时间:2014-08-12 05:47:43

标签: java swing timer paintcomponent repaint

public class Ova extends JPanel implements ActionListener{

int x=0;
Timer timer=new Timer(100,this);
int y=0,x1=5,y1=5;

public static void main(String[] ds)
{
    Ova ss=new Ova();
    ss.nn();    
}

private void nn() {     
    JFrame frame=new JFrame("fram");
    frame.setSize(1000,600);
    JPanel as=new JPanel();
    frame.add(as);  
    frame.add(new Ova());
    frame.setVisible(true);

    timer.start();
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);    
    g.drawOval(x, y, 50, 50);
}

public void actionPerformed(ActionEvent e) {
    if (x<0 || x>950){
        x1=-x1; 
    }
    if (y<0 || y>530)
    {
        y1=-y1;
    }

    x=x+x1;
    y=y+y1;

    repaint();
}
}

每当我将timer.start()放在paintComponent()repaint()方法有效,但如果我在paint组件方法之外启动计时器,则重绘方法不起作用。请解释原因。谢谢。

1 个答案:

答案 0 :(得分:3)

问题

您的参考问题不匹配

从这个片段开始......

public class Ova extends JPanel implements ActionListener {

    int x = 0;
    Timer timer = new Timer(100, this);
    int y = 0, x1 = 5, y1 = 5;

    public static void main(String[] ds) {
        Ova ss = new Ova();
        ss.nn();
    }

    private void nn() {
        JFrame frame = new JFrame("fram");
        frame.setSize(1000, 600);

        JPanel as = new JPanel();
        frame.add(as);
        frame.add(new Ova());
        frame.setVisible(true);
        timer.start();

    }

首先,main创建Ova的实例,然后方法nn创建Ova的新实例,然后启动timer ..

现在您需要问的问题是,Ova timer您启动了哪个...屏幕上的那个或main创建的那个......

我可以说,这是main创建的,在屏幕上看不到的

(可能)解决方案

首先从不在JFrame(或其他组件)中创建JPanel。我唯一一次这样做是通过static支持方法......

删除nn方法并将其替换为begin(或其类似方法)启动timer的方法。

main方法中,创建Ova的实例,将其添加到JFrame的实例并调用Ova的{​​{1}}方法。

begin

避免在任何内容上调用import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Ova extends JPanel implements ActionListener { int x = 0; Timer timer = new Timer(100, this); int y = 0, x1 = 5, y1 = 5; public static void main(String[] ds) { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } Ova ova = new Ova(); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(ova); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); ova.begin(); } }); } @Override public Dimension getPreferredSize() { return new Dimension(600, 600); } private void begin() { timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawOval(x, y, 50, 50); } @Override public void actionPerformed(ActionEvent e) { if (x < 0 || x > 950) { x1 = -x1; } if (y < 0 || y > 530) { y1 = -y1; } x = x + x1; y = y + y1; repaint(); } } ,而是覆盖组件的setSize方法并返回首选大小,然后在getPreferredSize上使用pack来包装框架围绕这个。您将获得更好,更可靠的结果。