java - 图像作为 JScrollPane 中的背景

标签 java image swing background jscrollpane

如何在 JScrollPane 中添加图片作为背景? 我试过了,但图像不显示:

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));

编辑:我想在带有背景的 JScrollPane 上添加 jlabels 和 jbuttons

最佳答案

如果您的目标只是在 JScrollPane 中显示图像而不在 JScrollPane 中显示其他组件(例如 JTable),那么您应该:

  • 通过 new ImageIcon(myImage)
  • 用你的图像制作一个 ImageIcon
  • 将该图标添加到 JLabel
  • 将 JLabel 放入 JScrollPane 的视口(viewport)中,这可以通过将 JLabel 传递到 JScrollPane 的构造函数中来完成。

大功告成。

如果您需要做其他事情,请更详细地描述您的问题。

例如,

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();
         }
      });
   }
}

你在评论中提问:

what if I have to add other objects like buttons?....How do I do it?

在这种情况下,我将创建一个扩展 JPanel 的类,并通过在 paintComponent 方法覆盖中绘制图像来在该 JPanel 中显示图像(如果您搜索此内容,您会发现许多例子,一些是我做的),然后将按钮/组件添加到这个图像绘制 JPanel,然后将这个 JPanel 添加到 JScrollPane 的视口(viewport)中,就像我对上面的 JLabel 所做的那样。

关于java - 图像作为 JScrollPane 中的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30830028/

相关文章:

php - 在 mySQL 数据库中存储图像的问题

java - 当我的应用程序打开一个新的 java 程序时,如何防止我的应用程序使用新主题重新绘制自身?

java - 我如何垂直滚动到 JPanel?

java - maven 自定义程序集 - 如何复制外部引用文件夹

java - 如何使用 java 和 YaHP Html to Pdf Converter 将 HTML 转换为 PDF 并打开 pdf 文件

java - 无法使用 Apache log4j2/slf4j 发送错误邮件

java - 使用独立的本地 spark java 程序读取 GCS 文件

python-3.x - 如何优化我的 Python 反向图像搜索以限制到特定域?

java - 错误: Compressor is abstract; cannot be instantiated Android

java - 从 DefaultTableModel 更新 JTable