Results 1 to 3 of 3
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default adding pictures to a button:

    Ok I am very new to Java but I am getting a hold of it pretty fast. I am more comfortable with c++ but really starting to like java a lot. I am really intrigued with swing but keep hitting walls.

    I want to add a picture to my buttons. The end statement will be a path finding algorithm that will with show the picture on all the buttons on the shortest path. I am using a constructor at the moment passing a picture object to it. It is suppose to work but its not on line 30:(

    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class Screen implements ActionListener 
    {
    	public JButton[][] b=new JButton[20][20];
    	
    	
    	Screen()
    	{
    		JFrame j = new JFrame("A Star Algorithm");
    		j.setSize(700,500);
    		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		j.setLayout(new GridLayout(20,20));
    		j.setResizable(true);
    		
    		ImageIcon icon = new ImageIcon("pic.gif");
    		
    		//create Buttons
    		for(int row=0;row<20;row++)
    		{
    			for(int col=0;col<20;col++)
    			{
    				b[row][col]=new JButton(icon);
    				b[row][col].setBackground(Color.YELLOW);
    				b[row][col].addActionListener(this);
    				j.add(b[row][col]);
    			}
    			
    		}
    		j.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent ae) 
    	{
    		
    		
    	}
    	
    	
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: adding pictures to a button:

    The code, as it is, should display the icon in each and every button, or no icon at all if the file could not be found ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: adding pictures to a button:

    I was putting it in the wrong place in eclipse. Now I need to figure out when that button is pressed for it display that picture. Then I can move on forward with my algorithm

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2013, 06:47 PM
  2. Album Class Adding the pictures
    By antnas in forum New To Java
    Replies: 2
    Last Post: 12-09-2012, 05:36 AM
  3. Replies: 22
    Last Post: 07-25-2012, 09:51 PM
  4. Java Applet that displays two pictures with a button...
    By yenujnassah in forum New To Java
    Replies: 0
    Last Post: 11-10-2011, 09:54 AM
  5. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •