Results 1 to 4 of 4
- 02-16-2012, 01:55 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
adding different images using if statements
i have decided to create a dictionary for minecraft.
i would like it so when the user inputs the name of an item in minecraft into a text field, a description and picture will show.
i can get the descriptions showing easily but the images... not so easy. i decided to create another program just to try and do it before adding to the code i already have so that i don't mess it up somehow >.<
the new program was just to show an image depending on what the user inputs. for example to show a picture of a banana when "banana" was entered. and to save time, i used a scanner instead of a JTextField to get user input for each picture (this shouldn't effect it?).
the problem i am having is that images aren't showing up at all. here is the code i have just to show 3 images depending on the input.
i have also tried creating a jlabel before the if statement and just adding it within each if statement with a different image. anyone know what im doing wrong? probably painfully simple but ya'know :)Java Code:import java.util.Scanner; import javax.swing.*; import java.awt.*; public class main { JFrame frame1 = new JFrame(); JPanel panel1 = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); Scanner joe = new Scanner(System.in); public void frames(){ frame1.setTitle("pictures"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setSize(500,500); frame1.setVisible(true); panel1.setBackground(Color.black); frame1.add(panel1); } public void ifs(){ if(joe.equals("banana")){ JLabel label1 = new JLabel(); label1.setIcon(new ImageIcon("banana.png")); c.gridx = 0; c.gridy = 0; panel1.add(label1, c); } else if(joe.equals("picture 2")){ JLabel label2 = new JLabel(); label2.setIcon(new ImageIcon("minecraft1.png")); c.gridx = 0; c.gridy = 0; panel1.add(label2, c); } else{ JLabel label3 = new JLabel(); label3.setIcon(new ImageIcon("topofitems.png")); c.gridx = 0; c.gridy = 0; panel1.add(label3, c); } } //main method run public static void main(String[]args){ main j = new main(); j.frames(); } }
sorry if the code is messy, im new so havent really learnt how to lay it out.
thanks for your time and effort :DLast edited by Norm; 02-16-2012 at 02:25 AM. Reason: added code tags
- 02-16-2012, 02:25 AM #2
Re: adding different images using if statements
Are you sure you have the correct path to the images?images aren't showing up at all
Create a File object and print its path.
How do you execute the code that loads the images?
Where is the ifs method called?
You need to look at how to use the Scanner class.
It has methods you call to read input from the user.Last edited by Norm; 02-16-2012 at 02:27 AM.
- 02-16-2012, 03:49 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 7
- Rep Power
- 0
Re: adding different images using if statements
this was the problem thanks :Dwhere is the ifs method called?
i had it in my head for some reason that if i called the frames method, it would call anything after that. i see that illogical now i think about it and feel like a pumpkin?
anyway, cheers norm :) +rep for you <3
- 02-16-2012, 03:54 AM #4
Similar Threads
-
Arrays and For statements for Images in Applet
By SummitWhiteMaro in forum Java AppletsReplies: 0Last Post: 03-07-2011, 03:59 AM -
Help: Displaying Images AND Adding Buttons
By Rhez in forum New To JavaReplies: 2Last Post: 08-05-2010, 06:19 AM -
Adding multiple images to frame
By 435.mahesh in forum AWT / SwingReplies: 1Last Post: 04-24-2009, 08:31 PM -
[new] need help on GUI adding images
By counterfox in forum AWT / SwingReplies: 5Last Post: 03-19-2009, 10:29 AM -
if else statements and using images
By Joshr in forum New To JavaReplies: 8Last Post: 10-05-2008, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks