2012-06-06 33 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