【问题标题】:Image as a background in JScrollPane图像作为 JScrollPane 中的背景
【发布时间】:2015-08-30 01:19:11
【问题描述】:

如何在 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

【问题讨论】:

    标签: java image swing background jscrollpane


    【解决方案1】:

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

    • 通过new ImageIcon(myImage)从您的图像中制作一个 ImageIcon
    • 将该图标添加到 JLabel
    • 将 JLabel 放置到 JScrollPane 的视口中,这可以通过将 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();
             }
          });
       }
    }
    

    你在评论中提问:

    如果我必须添加其他对象(如按钮)怎么办?....我该怎么做?

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

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2019-12-16
      相关资源
      最近更新 更多