4

我正在编写一个 gui,我想使用嵌入一些图片,但在将它嵌入到我的主程序之前,我编写了该代码来测试它:

    public class guikopie extends javax.swing.JFrame{
        public guikopie() {
            a = new javax.swing.JLabel();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            a.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulpen.jpg"));       
            add(a);//here i add it to the jlabel
            pack();
        }

        public static void main(String args[]){
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new guikopie().setVisible(true);
                }
            });
        }

        private javax.swing.JLabel a;
    }

我的问题是:为什么这段代码不显示图片?

4

2 回答 2

7

我认为人们可能会因为我重复这一点而讨厌我:P:

  • 不要扩展 JFrame 类
  • 类名以大写字母开头,即Guikopie

还取决于背景的用途,即是否将徽标添加到特定位置,JPanel然后使用 aJLabel很好,但是,如果将其用作背景,则不是;因为它会随着更多组件的添加而移动,因此我们不应该将背景添加为组件,而是在组件上绘制背景。

至于你的问题:

我的问题是:为什么这段代码不显示图片?

您的代码非常适合我,因此您的图片位置一定不正确。

我做了一个简短的例子,展示了如何添加ImageJPanel背景然后添加JPanelJFrame,它还包括ImgUtils调整图片大小的类:

在此处输入图像描述

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JFrameWithPicture {

    public JFrameWithPicture() throws MalformedURLException, IOException {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new JFrameWithPicture();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    private void initComponents() throws MalformedURLException, IOException {
        JFrame frame = new JFrame("Frame with JPanel and background");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Image background = ImageUtils.scaleImage(300, 300, ImageIO.read(new URL("http://images2.layoutsparks.com/1/98191/naruto-14-red-design.jpg")));
        final Dimension jpanelDimensions = new Dimension(new ImageIcon(background).getIconWidth(), new ImageIcon(background).getIconHeight());

        frame.add(new JPanel() {
            @Override
            protected void paintComponent(Graphics grphcs) {
                super.paintComponent(grphcs);
                grphcs.drawImage(background, 0, 0, this);
            }

            @Override
            public Dimension getPreferredSize() {
                return jpanelDimensions;
            }
        });

        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }
}

class ImageUtils {

    public static BufferedImage scaleImage(int width, int height, String filename) {
        BufferedImage bi;
        try {
            ImageIcon ii = new ImageIcon(filename);
            bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(ii.getImage(), 0, 0, width, height, null);
        } catch (Exception e) {
            return null;
        }
        return bi;
    }

    static Image scaleImage(int width, int height, BufferedImage filename) {
        BufferedImage bi;
        try {
            bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(filename, 0, 0, width, height, null);
        } catch (Exception e) {
            return null;
        }
        return bi;
    }
}

或者,您可能想使用Image#getScaledInstance(int width,int height,int hints)调整大小,但这有其危险,主要问题是:

Image.getScaledInstance()不返回完成的缩放图像。当使用图像像素时,它会将大部分缩放工作留给以后使用。

于 2012-10-21T09:43:05.463 回答
0
import javax.swing.*;
public class guikopie extends javax.swing.JFrame{
    public guikopie() {
        a = new javax.swing.JLabel();
        JPanel panel = new JPanel();
        panel.add(a);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        a.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulips.jpg"));
        getContentPane().add(panel);
        pack();
    }

    public static void main(String args[]){
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new guikopie().setVisible(true);
            }
        });
    }

    private javax.swing.JLabel a;
}

检查此代码。你的路径可能是错误的。

于 2012-10-21T09:21:15.750 回答