如何正确访问非静态repaint()方法表单静态main?

时间:2011-12-05 09:28:40

标签: java swing

当我尝试访问JPanel表单主静态方法的repaint()方法时,我收到错误:"Cannot make a static reference to the non-static method repaint() from the type Component"。当我尝试将main方法更改为"public void main"或删除静态时 - 它无法编译。解决这个问题的正确方法是什么?

public class mull extends JPanel  { ...
....
public static void main(String[] args) {

        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new JPanel();

        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {

                if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
                        && (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
                    ringPoints += 100;
                    allowedToDrawRing = false;
                }
                if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
                        && (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
                    squarePoints += 100;
                    allowedToDrawSquare = false;
                }
                if(!allowedToDrawSquare && !allowedToDrawRing){
//                  stop timer
                    // display points
                    ringTimer.stop();
                    squareTimer.stop();
                    showScores = true;
                    repaint(); // Cannot make a static reference to the non-static method repaint() from the type Component

                }
            }
        });

        f.add(p);
        f.add(new mull());
        f.setSize(frameWidth, frameHeight);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    } 

我将所有代码替换为main,并且有一个细节我看不懂如何处理。在我的main方法中,我创建了JFrame并添加了它(Swing计时器)。当我将所有代码都放到构造函数中时,它不起作用,并且在内部调用构造函数本身是没有意义的(为了添加到JFrame Timers)。怎么解决? 构造:

public Mull() {

        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new JPanel();

        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {

                if ((e.getX() >= ringX && e.getX() <= (ringSize + ringX))
                        && (e.getY() >= ringY && e.getY() <= (ringSize + ringY))) {
                    ringPoints += 100;
                    allowedToDrawRing = false;
                }
                if ((e.getX() >= squareX && e.getX() <= (squareSize + squareX))
                        && (e.getY() >= squareY && e.getY() <= (squareSize + squareY))) {
                    squarePoints += 100;
                    allowedToDrawSquare = false;
                }
                if(!allowedToDrawSquare && !allowedToDrawRing){
//                  stop timer
                    // display points
                    ringTimer.stop();
                    squareTimer.stop();
                    showScores = true;
                    repaint();

                }
            }
        });

        f.add(p);
        f.add(new Mull());
        f.setSize(frameWidth, frameHeight);
        f.setLocationRelativeTo(null);
        f.setVisible(true);


        ActionListener actionListenerRing = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {
                  if(ringCounter < 3){
                    xRing = (int) ((getWidth() - ringSize) * (Math.random()));
                    yRing = (int) ((getHeight() - ringSize) * (Math.random()));
                    while( !(
                            (xSquare + squareSize) < (xRing)
                            || (xSquare) > (xRing + ringSize )
                            || (ySquare + squareSize) < (yRing)
                            || (ySquare) > (yRing + ringSize)
                            )
                            ){
                        xRing = (int) ((getWidth() - ringSize) * (Math.random()));
                        yRing = (int) ((getHeight() - ringSize) * (Math.random()));


                    }
                    setParamsRing(xRing, yRing, ringSize);
                    ringCounter++;
                repaint();


                  } 
              }
            };
        ActionListener actionListenerSquare = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    if(squareCounter < 3){
                    xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
                    ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
                    while( !(
                            (xSquare + squareSize) < (xRing)
                            || (xSquare) > (xRing + ringSize )
                            || (ySquare + squareSize) < (yRing)
                            || (ySquare) > (yRing + ringSize)
                            )
                            ){
                        xSquare = (int) ((getWidth() - squareSize) * (Math.random()));
                        ySquare = (int) ((getHeight() - squareSize) * (Math.random()));
                    }
                    setParamsSquare(xSquare, ySquare, squareSize);
                    squareCounter++;
                repaint();


                    }   
                }
              };


        ringTimer = new Timer(700, actionListenerRing);
        ringTimer.setInitialDelay(1000);
        ringTimer.start();

        squareTimer = new Timer(500, actionListenerSquare);
        squareTimer.setInitialDelay(1000);
        squareTimer.start();
    }

也许,我应该将Timers方法移到另一个类,然后调用类似f.add(new setTimers()); ....

3 个答案:

答案 0 :(得分:3)

将main方法中的所有内容放在构造函数或类的某个init方法中,然后在main中执行new mull()

P.S。 http://java.about.com/od/javasyntax/a/nameconventions.htm:mull =&gt;马尔

答案 1 :(得分:3)

将所有逻辑放入main方法是一种不好的做法。它应该驻留在对象本身中。这也可以解决您的静态访问问题。以下是如何做到这一点:

将所有代码移动到另一个非静态方法,例如public void init() {}。然后,在public static void main方法中,调用这样的init方法:

mull m = new mull();
m.init();

此外,通过Java naming conventions Java类使用CamelCase命名。所以你的班级应该改名为“Mull”。

答案 2 :(得分:2)

f.repaint();(f应该是最终的)。

final JFrame f=(JFrame)e.getSource();

f.getContentPane().repaint();