2011-12-05 18 views
2

JPanelフォームの主な静的メソッドのrepaint()メソッドにアクセスしようとしたときにエラーが発生しました:"Cannot make a static reference to the non-static method repaint() from the type Component"。メインメソッドを"public void main"に変更しようとしたとき、または静的を削除しようとするとコンパイルできません。この問題を解決する適切なアプローチは何ですか?どのように正しく静的なrepaint()メソッドのフォーム静的メインにアクセスするには?

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); 
    } 

私はすべてのコード形式がメイン置き換え、私はどのように対処する見ることができない1つの詳細があります。私の主な方法では、JFrameを作成し、それに時間を追加しました(Swingタイマー)。すべてのコードをコンストラクタにパースすると、内部で(コンストラクタ自体をJFrameタイマーに追加するために)呼び出すのは意味がありません。どのようにそれを解決するには? コンストラクタ:

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(); 
    } 

おそらく、私は別のクラスにタイマ・メソッドを移動し、f.add(new setTimers());のようなものを呼び出す必要があります....

+0

タイマーはコンポーネントではないため、フレームに追加することはできません(f)。タイマーの問題を説明してください。なぜなら、ここで行われているようにOKであるはずだからです。 –

答えて

2

は悪い習慣です。オブジェクト自体に存在する必要があります。これにより、静的アクセスの問題も解決します。これを行う方法は次のとおりです。

すべてのコードを別の非静的メソッドに移動します(例:​​)。 Javaのnaming conventions Javaクラスによっても

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

は、キャメルケース・ネーミングを使用します。その後、あなたのpublic static void main方法では、このようなことinitメソッドを呼び出します。あなたのクラスは "Mull"に名前を変更する必要があります。

3

はあなたのメインのコンストラクタ内のメソッドまたはいくつかのinitで持っているすべてのものを置きますあなたのクラスのメソッドとその後、あなたのメインのnew mull()を実行します。

P.S. http://java.about.com/od/javasyntax/a/nameconventions.htm:mull => Mull

2

f.repaint();(fは最終値)。 mainメソッドにすべてのあなたのロジックを置く

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

または

f.getContentPane().repaint(); 
+0

彼は 'full 'ではなく' mull.this'で 'repaint'を呼び出しています。 – bezmax

関連する問題