JLabel图片显示问题

时间:2015-07-12 16:38:56

标签: java image swing jlabel embedded-resource

我目前正试图在f1g = f1 (g applicativeList) f2g = f2 (g applicativeMaybe) 内简单地显示图像。我尝试使用下面的代码,但它似乎没有用。我无法解决我出错的地方 - 任何想法?

这是控制台中显示的错误消息:

JFrame

以下是代码:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Day1.PictureTester.<init>(PictureTester.java:22)
    at Day1.PictureTester.main(PictureTester.java:14)

4 个答案:

答案 0 :(得分:4)

当然,这不会奏效。阅读Class.getResource()的javadoc。它使用 ClassLoader 类路径加载资源,使用斜杠分隔的路径。

将您的图片放在与PictureTester类相同的包中,然后使用

PictureTester.class.getResource("pug-image3.jpg");

或者将它放在源文件夹的一些随机包中(如com.foo.bar),然后使用

加载它
PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");

答案 1 :(得分:1)

<script type="text/javascript">
$(document).ready(function(){
function slideout(){
    setTimeout(function(){
        $("#response").slideUp("slow", function () {});
    }, 2000);}
    $("#response").hide();
    $(function() {
        $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update:function() {
            var order = $(this).sortable("serialize") + '&update=update';
            $.post("updateList.php", order, function(theResponse){
                $("#response").html(theResponse);
                $("#response").slideDown('slow');
                slideout();
            });
        }
    });
  });
});
</script>

...

<?php
include("connect.php");
$draft_id='0';
$query  = "SELECT id, text FROM sort WHERE draft_id =" . $draft_id . " ORDER BY listorder ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{   
   $id = stripslashes($row['id']);
   $text = stripslashes($row['text']);
   echo "<li id='arrayorder_" . $id . "'>" . $text;
   echo "<div class='clear'></div>";
   echo "</li>";
}
?>

答案 2 :(得分:1)

这段代码解决了它,它帮助我思考在一个新项目中创建它:

package NewAppIdea;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


    public class PictureSplash extends JFrame {

        private static final long serialVersionUID = 1L;
        JLabel l1;

        PictureSplash(){
        setTitle("Pic");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg")));
        setLayout(new FlowLayout());
        setSize(399,399);
        setSize(400,400);
        }

        public static void main(String args[]) {
            new PictureSplash();
        }   
    }

答案 3 :(得分:0)

        Icon i=new ImageIcon("src//pics//scope.jpg");
        JLabel l1=new JLabel(i);

像这样使用JLabelImageIcon