2012-06-06 32 views
1

我工作的這個繪圖程序項目的BorderLayout的(更喜歡,擴展它),我來到一個討厭的錯誤。我加了一個JLabel上一個JPanel,在BorderLayout的南部,但它不僅沒有被添加到JPanel的南部,之後我畫上的JPanel的東西也變得模糊。我不明白的是這兩件事情發生的原因。我有一個問題有一個JLabel和一個JPanel

代碼的類的:

import java.awt.BorderLayout; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionAdapter; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Painter extends JPanel{ 

private int pointCount = 0; // count the number of points 

// array of 10000 java.awt.Point references 
private Point[] points = new Point[10000]; 
JLabel myCount = new JLabel(); 

// set up GUI and register mouse event handler 
public Painter() { 

    myCount.setText("Points so far: " + pointCount); 
    add(myCount, BorderLayout.SOUTH); 

    // handle frame mouse motion event 
    addMouseMotionListener(
      new MouseMotionAdapter() { // anonymous inner class 
       public void mouseDragged(MouseEvent event) { 
        if (pointCount < points.length) { 
         points[pointCount] = event.getPoint(); // find the point 
         ++pointCount; // increment number of points in the array 
         repaint(); 
         myCount.setText("Points so far: " + pointCount); 
        } // end if  
      } // end of mouseDragged method 
     } // end anonymous inner class 
    ); // end of addMouseMotionListener 
}// end Painter constructor 

// draw ovals in a 4-by-4 bounding box at specified locations on window 
public void paintComponent(Graphics g) { 
    super.paintComponents(g); // clears drawing area 

    // draw all points in the array 
    for (int i = 0; i < pointCount; i++) { 
     g.fillOval(points[i].x, points[i].y, 12, 12); 
    } // end for loop 
} // end method paintComponent 
} // end of Painter class 

回答

3

第一個問題可以通過調用setLayout的(新的BorderLayout)來解決;爲您的構造函數的第一行

第二個問題可以通過從paintComponents去掉「S」,只是調用的paintComponent

+0

*「從paintComponents去掉‘S’」 *好斑點來解決。 –

+0

謝謝你,先生。還有一個問題。 JPanel是不是默認有BorderLayout? – NlightNFotis

+0

@NlightNfotis是什麼讓你認爲?該文檔不說的默認佈局 – ControlAltDel