简体   繁体   English

图像作为JScrollPane中的背景

[英]Image as a background in JScrollPane

How to add an image as a background in JScrollPane? 如何在JScrollPane中添加图像作为背景? I tried this but the image doesn't display: 我试过了,但图片没有显示:

BufferedImage img = null;
    try {
        img = ImageIO.read(new File("C:\\Users\\Suraj\\Documents\\NetBeansProjects\\JavaApplication2\\src\\javaapplication2\\images\\2.jpg"));
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
    Image imag = img.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH);
    ImageIcon imageBack = new ImageIcon(imag);
    FlowLayout fl = new FlowLayout();
    frame.getContentPane().setLayout(fl);
    fl.addLayoutComponent(null, new JLabel(imageBack));

EDIT : I would like to add jlabels and jbuttons on the JScrollPane with the background 编辑:我想在JScrollPane上添加背景的jlabels和jbuttons

If your goal is to simply show an image in a JScrollPane without showing other components (such as a JTable) in the JScrollPane, then you should: 如果您的目标是只在JScrollPane中显示图像而不在JScrollPane中显示其他组件(例如JTable),那么您应该:

  • Make an ImageIcon out of your image via new ImageIcon(myImage) 通过new ImageIcon(myImage)从图像中创建一个ImageIcon
  • Add that Icon to a JLabel 将该图标添加到JLabel
  • Place the JLabel into the JScrollPane's viewport, something that can be done by passing the JLabel into the JScrollPane's constructor. 将JLabel放入JScrollPane的视口中,可以通过将JLabel传递给JScrollPane的构造函数来完成。

And your done. 而你完成了。

If you need to do something else, then describe your problem in greater detail. 如果您需要做其他事情,请更详细地描述您的问题。

For example, 例如,

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class ImageInScrollPane extends JPanel {
   public static final String IMAGE_PATH = "http://image.desk7.net/"
         + "Space%20Wallpapers/1422_1280x800.jpg";
   private static final int PREF_W = 500;
   private static final int PREF_H = 400;


   public ImageInScrollPane() throws IOException {
      URL imageUrl = new URL(IMAGE_PATH);
      BufferedImage img = ImageIO.read(imageUrl);
      Icon icon = new ImageIcon(img);
      JLabel label = new JLabel(icon);
      JScrollPane scrollPane = new JScrollPane(label);

      setLayout(new BorderLayout());
      add(scrollPane);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      ImageInScrollPane mainPanel = null;
      try {
         mainPanel = new ImageInScrollPane();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      JFrame frame = new JFrame("ImageInScrollPane");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

You ask in comment: 你在评论中提问:

what if I have to add other objects like buttons?....How do I do it? 如果我必须添加按钮等其他对象怎么办?....我该怎么办?

In this situation, I'd create a class that extends JPanel and display the image in this JPanel by drawing it within the paintComponent method override (if you search on this, you'll find many examples, some by me), then add the buttons/components to this image-drawing JPanel, and then adding this JPanel into the JScrollPane's viewport as I do with the JLabel above. 在这种情况下,我创建了一个扩展JPanel的类,并通过在paintComponent方法覆盖中绘制它来在此JPanel中显示图像(如果你搜索它,你会找到很多例子,有些是我的),然后添加这个图像绘制JPanel的按钮/组件,然后将这个JPanel添加到JScrollPane的视口中,就像我上面的JLabel一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM