JFrame中的图像 - Swing / AWT

时间:2013-09-10 15:22:27

标签: java swing jframe

前几天,我浪费了很多时间在某个方面搜索在JFrame中显示图像。这是我的最终解决方案:

jPanel1 = new javax.swing.JPanel(){
        @Override
        public void paintComponent(Graphics g) {
            BufferedImage image = null;
            try {
                BufferedImage in = ImageIO.read(Startup.class.getResource("imagem.jpg"));
                image = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
                g.drawImage(in, 0, 0, null);
            } catch (Exception ex) {}
            super.paintComponents(g);
        }
    };

我只是想知道,如果是这样做的一种方法,或者存在其他解决方案,例如可以轻松使用的Swing o AWT上的图像组件?

1 个答案:

答案 0 :(得分:1)

您可以使用JLabel显示图像,这比您的解决方案简单得多。例如:

label.setIcon(new ImageIcon("Path/to/your/image.jpg"));