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: Beginner: Show Image in Label when Results Show Up

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner: Show Image in Label when Results Show Up

    Below I am trying to set up so that when the results show up an Image appears in Lb4.




    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.util.Scanner;
    import java.util.Random;
     
    public class HighLow extends Applet implements ActionListener
    {
     
      //declare variables
      Panel nPanel, sPanel, tcPanel, bcPanel, cPanel;
      Button one, clear;
      Label top,lb1, lb2, lb3, lb4, bottom;
      TextField txt;
      int number, cnumber,count;
      final int MAX = 25;
     
     public void init()
      {
     
      //create random number
      Random generator = new Random();
    		cnumber = generator.nextInt(MAX) + 1;
     
      //set north panel
      nPanel = new Panel();
      nPanel.setBackground(Color.blue);
      top = new Label("High/Low Game ~What Number am I thinking of?~");
     
      //set south panel
      sPanel = new Panel();
      sPanel.setBackground(Color.red);
      bottom = new Label("Pick a number between 1 and 25");
     
      //set center, topcenter, bottomcenter panel
      cPanel = new Panel();
      tcPanel = new Panel();
      bcPanel = new Panel();
     
      //add submit buttonm labels and textfield
     
      one = new Button("Submit");
      one.addActionListener(this);
      clear = new Button("Clear");
      clear.addActionListener(this);
      one.setPreferredSize(new Dimension(100,100));
      clear.setPreferredSize(new Dimension(20,20));
      lb1 = new Label();
      lb2 = new Label();
      lb3 = new Label("                                                              ");
      txt = new TextField();
      lb4 = new Label("                                                               ");
     
      //set topcenter GridLayout
      tcPanel.setLayout (new GridLayout(3,1));
      tcPanel.add(lb3);
      tcPanel.add(txt);
      tcPanel.add(lb2);
     
     
     
      //set bottomcenter GridLayout
      bcPanel.setLayout (new GridLayout(3,1));
      bcPanel.add(lb1);
      bcPanel.add(one);
      bcPanel.add(lb4);
     
     
     
      //combine tc and bc panels
      cPanel.setLayout(new BorderLayout(7,1));
      //cPanel.add(clear);
      cPanel.add(tcPanel, BorderLayout.WEST);
      cPanel.add(bcPanel, BorderLayout.EAST);
     
      //set layout and label for top
      nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
      nPanel.add(top);
     
      //set layout and label for bottom
      sPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
      sPanel.add(bottom);
     
      //set layout for the Applet
      this.setLayout(new BorderLayout());
      add(nPanel, BorderLayout.NORTH);
      add(cPanel, BorderLayout.CENTER);
      add(sPanel, BorderLayout.SOUTH);
     }
     
    public void actionPerformed(ActionEvent e)
    {
     
     
    	number = Integer.parseInt(txt.getText());
     
    	count++;
     
    	if (number > cnumber)
    	{
    		lb3.setText("Number is too high");
    	    lb3.setBackground(Color.red);
    	    lb4.setText("");
    	}
    	else if (number < cnumber)
    	{
    		lb2.setText("Number is too low");
    		lb2.setBackground(Color.yellow);
    		lb3.setText("");
    	}
    	else if (number == cnumber)
    	{
     
    		bottom.setText("Guessed it in " + count + " tries!!");
    		bottom.setBackground(Color.green);
    		sPanel.setBackground(Color.green);
    		top.setText("CONGRATULATIONS!");
    		top.setBackground(Color.green);
    		nPanel.setBackground(Color.green);
     
    		//bottom.setText("It only took you " + count + " guesses!!!");
    	}
     
     
    }
    }

    So I would need to add it to this statement because its when the results show. Im not sure of the code I would use get get the image to show up in the lower right Panel.

    else if (number == cnumber)
    	{
     
    		bottom.setText("Guessed it in " + count + " tries!!");
    		bottom.setBackground(Color.green);
    		sPanel.setBackground(Color.green);
    		top.setText("CONGRATULATIONS!");
    		top.setBackground(Color.green);
    		nPanel.setBackground(Color.green);
                    lb4.getImage("winner.jpg);/////////////////////////////something like this will not work I need some code. Please help


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Beginner: Show Image in Label when Results Show Up

    So if I understand the question correctly, you wish to set the icon of a JLabel. Firstly, see the API for JLabel. It defines the setIcon method which can be used to set the icon. Depending upon where the image is, you can then pass an ImageIcon defined by the file to this method.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner: Show Image in Label when Results Show Up

    I need it so when the results show, so does the image. You know what im saying

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner: Show Image in Label when Results Show Up

    Sorry I guess Im still not understaning how to incorporate this in my code.

    What I have am I close?

    else if (number == cnumber)
    	{
     
    		bottom.setText("Guessed it in " + count + " tries!!");
    		bottom.setBackground(Color.green);
    		sPanel.setBackground(Color.green);
    		top.setText("CONGRATULATIONS!");
    		top.setBackground(Color.green);
    		nPanel.setBackground(Color.green);
    		ImageIcon icon = new ImageIcon("winner.gif");////////////added
    		JLabel img = new JLabel(icon);//////////////////////////////added
     
    	}
     
     
    }
    }

Similar Threads

  1. Show files name in JFrame
    By altisw5 in forum AWT / Java Swing
    Replies: 15
    Last Post: November 30th, 2011, 08:30 AM
  2. Applet doesnt show
    By chonch in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 03:22 PM
  3. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 12:55 AM
  4. Show Text With cursor
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2010, 10:02 AM
  5. How do i show all the values in one window(JOptionPane)??
    By Antonioj1015 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 25th, 2009, 09:24 PM