我想在jlabel中顯示圖像的直方圖,但它不工作。paintComponent方法在JLabel上不顯示任何內容?
//hist : array containing histogram values
//wid: width of image
//ht: height of image
mylabel.add(new showData(hist,wid,ht));
,我使用來顯示直方圖的代碼是:
class showData extends JLabel{
int w,h;
int hist[] = new int[256];
int max_hist=0;
public showData(int[] histValue,int w, int h) {
System.arraycopy(histValue, 0, hist, 0, 256);
this.w = w;
this.h = h;
for (int i = 0; i < 256; i++) {
if(hist[i]>max_hist)
max_hist=hist[i];
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (w - 256)/2;
int lasty = h - h * hist[0]/max_hist;
for (int i=0; i<256; i++, x++) {
int y = h - h * hist[i]/max_hist;
g.setColor(new Color(i, i, i));
g.fillRect(x, y, 1, h);
g.setColor(Color.red);
g.drawLine(x-1,lasty,x,y);
lasty = y;
}
}
}
當調試,我發現,是越來越調用showData()方法,但的paintComponent()沒有。爲什麼這樣? Jlabel'mylabel'沒有顯示任何內容?
爲了更好地幫助越早,張貼[SSCCE](http://sscce.org/ )。 – 2012-03-31 14:37:41
可能的重複[如何設置JLabel的背景顏色?](http://stackoverflow.com/questions/2380314/how-do-i-set-a-jlabels-background-color) – trashgod 2012-03-31 14:41:46
爲什麼你使用JLabel的? JPanel看起來更合適。確保正確調整大小(或者設置preferredSize並在周圍容器中使用layoutManager,或者強制面板的大小/位置) – 2012-03-31 14:43:10