2011-11-23 35 views
2

我無法訪問我的課程MyPanel的公共方法,它擴展了JPanel。在MyPanel我以前有private方法叫做moveSquare,但爲了使它適用於外部請求,我將其更改爲public,但它仍然不起作用?如何使它工作?如何訪問外部類的公共方法以重新繪製JPanel?

import javax.swing.SwingUtilities; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.BorderFactory; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseMotionAdapter; 

public class mull { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JPanel p = createAndShowGUI(); 
       iterate(p); 
       // "The method iterate(MyPanel) in the type mull is not applicable for the arguments (JPanel)" 
      } 
     }); 
    } 

    private static JPanel createAndShowGUI() { 
     System.out.println("Created GUI on EDT? "+ 
     SwingUtilities.isEventDispatchThread()); 
     JFrame f = new JFrame("Swing Paint Demo"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // how to grab link to this panel - in order to use it in iteration loop ? 
     MyPanel p = new MyPanel(); 
     f.add(p); 
//  f.add(new MyPanel()); 
     f.pack(); 
     f.setVisible(true); 
     return p; 
    } 
    private static void iterate(JPanel p){ 
     // the loop should change square position on each iteration 
     // how to implement ? 

     for (int i = 0; i < 999; i++){ 
      ((MyPanel) p).moveSquare(100 + i*10, 200 + i*10); // here is problem: 
      //"Cannot make a static reference to the non-static method moveSquare(int, int) from the type MyPanel" 
     } 


    } 
} 

class MyPanel extends JPanel { 

    private int squareX = 50; 
    private int squareY = 50; 
    private int squareW = 200; 
    private int squareH = 200; 

    public MyPanel() { 

     setBorder(BorderFactory.createLineBorder(Color.black)); 

     addMouseListener(new MouseAdapter() { 
      public void mousePressed(MouseEvent e) { 
       moveSquare(e.getX(),e.getY()); 
      } 
     }); 

     addMouseMotionListener(new MouseAdapter() { 
      public void mouseDragged(MouseEvent e) { 
       moveSquare(e.getX(),e.getY()); 
      } 
     }); 

    } 
    // originally this method was private - in orger to access it within mull, it vas changed to public 
    public void moveSquare(int x, int y) { 
     int OFFSET = 1; 
     if ((squareX!=x) || (squareY!=y)) { 
      repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET); 
      squareX=x; 
      squareY=y; 
      repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET); 
     } 
    } 


    public Dimension getPreferredSize() { 
     return new Dimension(900,700); 
    } 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g);  
     g.drawString("This is my custom Panel!",10,20); 
     g.setColor(Color.RED); 
     g.fillRect(squareX,squareY,squareW,squareH); 
     g.setColor(Color.BLACK); 
     g.drawRect(squareX,squareY,squareW,squareH); 
    } 
} 

這個語法做什麼((MyPanel) p).moveSquare(100 + i*10, 200 + i*10);? 如何在iterate(p)中繪製自定義形狀(橢圓形)?

+0

你有什麼錯誤? –

+1

+1發佈http://sscce.org/ – mKorbel

回答

2

它似乎並不像你打電話iterate()

但是在任何情況下,您的moveSquare方法都不是靜態的,因此需要使用特定的MyPanel實例。所以你可能想要保留對你的面板的參考。再說,而不是f.add(new MyPanel()),你可以使用:

MyPanel p = new MyPanel(); 
f.add(p); 

,然後傳遞到piterate在那裏打電話p.moveSquare(100,200)

2

您需要在某處引用MyPanel。在仔細考慮類的頂部添加此:

MyPanel myPanel = null; 

然後,當您添加的面板做這樣的:

myPanel = new MyPanel(); 
f.add(myPanel); 

您現在可以從你的類馬爾內側基準myPanel。此外,類應該以Java中的首字母開頭(因爲),所以將其改爲Mull。

相關問題