Java paintComponent方法在JLabel上不显示任何内容?

Java paintComponent方法在JLabel上不显示任何内容?,java,swing,histogram,jlabel,paintcomponent,Java,Swing,Histogram,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

我试图在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”没有显示任何内容?

如果标签不透明,您可能需要在showData中调用重新绘制。

如果标签不透明,您可能需要在showData中调用重新绘制。

以下代码对我适用。注意构造函数中preferredSize的设置:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

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

class ShowData extends JLabel {
    int w, h;
    int hist[];
    int max_hist = 0;

    public ShowData(int[] histValue, int w, int h) {
        hist = new int[histValue.length];
        System.arraycopy(histValue, 0, hist, 0, histValue.length);
        this.w = w;
        this.h = h;
        setPreferredSize(new Dimension(w, h * 2));
        for (int i = 0; i < hist.length; i++) {
            if (hist[i] > max_hist) {
                max_hist = hist[i];
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int x = (w - hist.length) / 2;
        int lasty = h - h * hist[0] / max_hist;
        for (int i = 0; i < hist.length; 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;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ShowData data = new ShowData(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 100,
             100);
        frame.add(data);
        frame.pack();
        frame.setVisible(true);    
    }
}

下面的代码适合我。注意构造函数中preferredSize的设置:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

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

class ShowData extends JLabel {
    int w, h;
    int hist[];
    int max_hist = 0;

    public ShowData(int[] histValue, int w, int h) {
        hist = new int[histValue.length];
        System.arraycopy(histValue, 0, hist, 0, histValue.length);
        this.w = w;
        this.h = h;
        setPreferredSize(new Dimension(w, h * 2));
        for (int i = 0; i < hist.length; i++) {
            if (hist[i] > max_hist) {
                max_hist = hist[i];
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int x = (w - hist.length) / 2;
        int lasty = h - h * hist[0] / max_hist;
        for (int i = 0; i < hist.length; 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;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ShowData data = new ShowData(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 100,
             100);
        frame.add(data);
        frame.pack();
        frame.setVisible(true);    
    }
}
看看这个:

/**
 * 
 */
package org.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;

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

/**
 * @author Sinisa
 *
 */
public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel jLabel = new JLabel();
        Test t = new Test();
//      jLabel.add();
        frame.add(t.new showData(new int[]{1, 2, 3},200,200));
        frame.setVisible(true);
        frame.setSize(new Dimension(800, 600));
    }


    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, 3);
            this.w = w;
            this.h = h;
//          this.setText("sds");
            for (int i = 0; i < 256; i++) {
                if(hist[i]>max_hist)
                    max_hist=hist[i];
            }
        }


        /**
        * {@inheritDoc}
        */
        @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;
        }
        }
        }
}
看看这个:

/**
 * 
 */
package org.test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;

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

/**
 * @author Sinisa
 *
 */
public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel jLabel = new JLabel();
        Test t = new Test();
//      jLabel.add();
        frame.add(t.new showData(new int[]{1, 2, 3},200,200));
        frame.setVisible(true);
        frame.setSize(new Dimension(800, 600));
    }


    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, 3);
            this.w = w;
            this.h = h;
//          this.setText("sds");
            for (int i = 0; i < 256; i++) {
                if(hist[i]>max_hist)
                    max_hist=hist[i];
            }
        }


        /**
        * {@inheritDoc}
        */
        @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;
        }
        }
        }
}

为了更快地获得更好的帮助,请发布一个可能的副本,说明您为什么使用JLabel?JPanel看起来更合适。确保其大小正确设置preferredSize并在周围的容器中使用layoutManager,或者强制设置panel@trashgod:opacity设置为true,如果我在其上显示一个BuffereImage,它的工作绝对正常,但同一图像的直方图不会被显示!!您是否设置了JLabel的大小?还是你强迫它更喜欢的尺寸?如果不是,则标签的大小将为0,0,并且永远不会被绘制。为了更快地获得更好的帮助,请发布一个可能的副本。为什么要使用JLabel?JPanel看起来更合适。确保其大小正确设置preferredSize并在周围的容器中使用layoutManager,或者强制设置panel@trashgod:opacity设置为true,如果我在其上显示一个BuffereImage,它的工作绝对正常,但同一图像的直方图不会被显示!!您是否设置了JLabel的大小?还是你强迫它更喜欢的尺寸?如果不是,则标签的大小将为0,0,并且它将永远不会被绘制。在showData中,在哪里调用重新绘制是合适的?它应该被称为super.repaint,对吗?每次模型更改时都调用repaint;您应该使用您的实现,而不是super的实现。@Saurabh和Andrew提到使用SSCCE编辑您的问题,问题可能在以前代码的其他部分+1在showData中调用repaint是否合适?它应该被称为super.repaint,对吗?每次模型更改时都调用repaint;您应该使用您的实现,而不是super的实现。@Saurabh和Andrew提到使用SSCCE编辑您的问题,问题可能在之前+1代码的其他部分