Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Picture won't go with the .jar (Wrong code or compile error?)

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Picture won't go with the .jar (Wrong code or compile error?)

    Hi ,

    I want to have a picture on the JFrame and for that I use the 2 lines of code below this patagraph. The problem is that when I compile it to .jar, I can only see the picture on the computer with the picture and if the picture stays on the same path like when I compiled it. How do I do to make the picture follow in the .jar file?

    a = new JLabel();
    a.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\Jonathan\\Mina dokument\\NetBeansProjects\\NameGen\\build\\classes\\p12.jpg"));

    When I compile it I do like this (I compile in NetBeans):

    I right click on the project
    Go to properties
    Set the class (the class in the code below) as main class
    Go to run
    Press Build Main Project

    If you no nothing about netbeans, tell me how you compile it and I can test that and see if it is the compilation that is the error.


    This is the whole code (if that may help)

    Excuse the Swenglish in the code ^^

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
     
     
    public class p12NameGennGUI extends JFrame implements ActionListener
    {
     
     
        JButton knapp;
        JLabel textTK;
        JLabel a;
        JTextField visaNamn;
     
     
       //arrayen för första delen i namnet
           String[] p12fornamn ={"Cool", "C3wL","CraZy","Crejzy","Master","Big","Fruity","Fruudy","FrUitY","Hardcore","HardCore","Hardcore","Mr","Infinite","Infinite","Infinit3","Max","MaxX","Maximum","Maximum","Savage","Savag3","Pro","Haxxor","HaXXor","Savage","Savag3"};
     
           int length1 = p12fornamn.length;
     
           //arrayen för mellandelen i namnet
           String[] p12mellannamn = {"_","","-"};
     
           int lenght2 = p12mellannamn.length;
     
            //arrayen för andra delen i namnet
           String[] p12efternamn = {"97","1997","NinetySevN","Ninetyseven","XzTazY","Ecztasy","Hero","Pro","Haxx","HaXx","Maestro","MajsTr0","Chief","ChI3f","Skateboard","1997","BoY","Kingen","King"};
     
           int lenght3 = p12efternamn.length;
     
     
     
     
        public p12NameGennGUI()
        {
     
            textTK = new JLabel("Tryck på knappen för ett slumpat P12 namn!");
            textTK.setForeground(Color.black);
            textTK.setFont(new Font("Arial", Font.BOLD, 12));
     
            knapp = new JButton("TRYCK NU");
     
     
     
    a = new JLabel();
    a.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\Jonathan\\Mina dokument\\NetBeansProjects\\NameGen\\build\\classes\\p12.jpg"));
     
     
     
     
            visaNamn = new JTextField();
            visaNamn.setColumns(15);
     
            this.add(textTK);
            this.add(knapp);
            knapp.addActionListener(this);
            this.add(visaNamn);
            this.add(a);
     
     
        }
     
     
     
     
        public static void main(String[] args)
        {
     
     
            p12NameGennGUI p = new p12NameGennGUI();
            p.setSize(300, 300);
            p.setLocation(200, 300);
            p.setDefaultCloseOperation(EXIT_ON_CLOSE);
            p.setLayout(new FlowLayout());
            p.setVisible(true);
        }
     
        public void actionPerformed(ActionEvent e)
        {
            //genererar första delen av namnet
            int p12namnet1 = (int) (Math.random() * length1);
            //genererar mellandelen av namnet
            int p12namnet2 = (int) (Math.random() * lenght2);
            //genererar andra delen av namnet
            int p12namnet3 = (int) (Math.random() * lenght3 );
     
     
            visaNamn.setText(p12fornamn[p12namnet1] + p12mellannamn[p12namnet2] +  p12efternamn[p12namnet3]);
     
     
        }
     
        private void setIcon(ImageIcon imageIcon)
        {
     
        }
    }//class end


    Regards
    Fermen


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Picture won't go with the .jar (Wrong code or compile error?)

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Fermen (March 31st, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Picture won't go with the .jar (Wrong code or compile error?)

    You will find your answer within this link. Let us know if you don't understand..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    Fermen (March 31st, 2011)

  6. #4
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Picture won't go with the .jar (Wrong code or compile error?)

    Quote Originally Posted by JavaPF View Post
    You will find your answer within this link. Let us know if you don't understand..
    I do like in this example below marked in red and the cmd says it compresses to a jar. The .jar ends up in the same folder as the mainclass and when I press the .jar file I just hear that "windows error-sound" and the .jar does not open.

    jar cvf TicTacToe.jar TicTacToe.class audio images

Similar Threads

  1. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM
  2. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 05:30 PM
  3. what's wrong in this code
    By Aravind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2011, 03:38 AM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM