JPanel不会显示图片?

时间:2018-03-25 10:58:45

标签: java image image-processing jpanel

我正在尝试使用JPanel显示图像。我有这段代码:

package training;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.plaf.SliderUI;

public class Training extends JPanel {
  public static BufferedImage image;
  double maxw, maxh;
  double w, h, ratio;

  public Training () {
    super();
    try {               
      image = ImageIO.read(new File("src/training/P.jpg"));

    }
    catch (IOException e)
    {
      //Not handled.
    }
    maxw = 750;
    maxh = 600;
    w = image.getWidth();
    h = image.getHeight();

    if (w > h) {
    if (w > maxw) {
        ratio = maxw / w;
        h = h * ratio;    // Reset height to match scaled image
        w = w * ratio;   
    }

    }
    if (w <= h) {
   if (h > maxh) {
        ratio = maxh / h;
        w = w * ratio;    // Reset height to match scaled image
        h = h * ratio;   
    }

    } 
  }

  public void paintComponent(Graphics g)        
  {
    Image i = image.getScaledInstance((int)w, (int)h,Image.SCALE_SMOOTH);
    g.drawImage(i, 0, 0, null);
    repaint();
  } 

    public static void main(String[] args) {

        System.out.println("User dir: " + System.getProperty("user.dir"));
        JPanel p = new JPanel();
        JFrame f = new JFrame("Window");

    f.setSize(1000, 600);
    f.add(p); 
    p.add(new Training());
   p.setSize(750, 600);    
    f.setVisible(true);
    p.setVisible(true);
}
}

以前,当我使用f.add(new Training())直接绘制到框架时它会起作用;没有先创建JPanel。窗框显示当时的图像。

如何让JPanel正确显示我的图像?

1 个答案:

答案 0 :(得分:0)

诀窍是将你的照片加载到JLabel&#39;中。您无法将图片直接放到&#39; JPanel&#39;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

public class Main extends JPanel {
    public static BufferedImage image;
    double maxw, maxh;
    double w, h, ratio;

    public Main () {
        super();
        try {
            image = ImageIO.read(new File("src/training/P.jpg"));;
            JLabel l = new JLabel(new ImageIcon(image));
            add(l);
        }
        catch (IOException e)
        {
            //Not handled.
        }
        maxw = 750;
        maxh = 600;
        w = image.getWidth();
        h = image.getHeight();

        if (w > h) {
            if (w > maxw) {
                ratio = maxw / w;
                h = h * ratio;    // Reset height to match scaled image
                w = w * ratio;
            }

        }
        if (w <= h) {
            if (h > maxh) {
                ratio = maxh / h;
                w = w * ratio;    // Reset height to match scaled image
                h = h * ratio;
            }

        }
    }

    public void paintComponent(Graphics g)
    {
        Image i = image.getScaledInstance((int)w, (int)h,Image.SCALE_SMOOTH);
        g.drawImage(i, 0, 0, null);
        repaint();
    }

    public static void main(String[] args) {

        System.out.println("User dir: " + System.getProperty("user.dir"));
        JFrame f = new JFrame("Window");
        JPanel p = new Main();
        f.setSize(1000, 600);
        p.setSize(750, 600);
        f.add(p);
        f.setVisible(true);
    }
}