954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

adding graphics to GUI

Hey, I am trying to make a program that has pictures on the GUI, but am having trouble making the picture show up.

Here is the code:

import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;


/**
   Quick example showing images (specifically ImageIcons) on
   JButtons and JLabels

   @author Sharon Tuttle
   @version 11-04-08
*/

public class ImageIconEx1
{
    /**
       creates an ImageIconEx1Frame
       @param args not used here
    */

    public static void main(String args[])
    {
        EventQueue.invokeLater(
           new Runnable()
           {
                public void run()
                {
                    ImageIconEx1Frame mainFrame = new ImageIconEx1Frame();
                    mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    mainFrame.setVisible(true);
                }
           });
    }
}
/**
   A frame with a panel containing buttons and labels featuring
   image icons
*/

class ImageIconEx1Frame extends JFrame
{
    /**
       constructs an ImageIconEx1Frame instance
    */

    public ImageIconEx1Frame()
    {
        this.setTitle("ImageIcons on JButtons and JLabels");
        this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        // add ImageIconEx1 panel to frame

        ImageIconEx1Panel panel = new ImageIconEx1Panel();
        this.add(panel);
    }

    // data fields

    private final static int DEFAULT_WIDTH = 500;
    private final static int DEFAULT_HEIGHT = 400;
}

/**
   A panel containing buttons and labels featuring
   image icons
*/

class ImageIconEx1Panel extends JPanel
{
    /**
       constructs a ImageIconEx1 panel instance
    */

    public ImageIconEx1Panel()
    {
        ImageIcon myImageIcon = new ImageIcon("C:'\'Users'\'Kimberlie'\'Desktop'\'BlackJackGui'\'train.gif");

        JButton myImageButton = new JButton(myImageIcon);
        JLabel myImageLabel = new JLabel(myImageIcon);

        JButton myTextAndImageButton = new JButton("Button Text", myImageIcon);
        myTextAndImageButton.setFont(DISPLAY_FONT);

        // note: the third argument here is with regard to the LABEL's horizontal
        //    alignment, NOT the label's text;

        JLabel myTextAndImageLabel = new JLabel("Label Text", myImageIcon,
                                                SwingConstants.CENTER);
        myTextAndImageLabel.setFont(DISPLAY_FONT);

        this.add(myImageButton);
        this.add(myImageLabel);
        this.add(myTextAndImageButton);
        this.add(myTextAndImageLabel);

    } // end ImageIconEx1Panel constructor

    // data fields for ImageIconEx1Panel

    private final static Font DISPLAY_FONT = new Font(
                                            "Dialog", Font.PLAIN, 20);
}
christiangirl
Junior Poster
108 posts since Apr 2008
Reputation Points: 20
Solved Threads: 1
 

kindly make a program that can make GUI that can draw graphics when the buttons is clicked.

alzymore
Newbie Poster
1 post since Feb 2010
Reputation Points: 6
Solved Threads: 0
 

You put the absolute path of the file, but did it find the file? You enclosed your directory names in single quotes, which doesn't make sense to me. I doubt it is finding the file. Read this which describes what will happen if the image isn't found: nothing will happen, the ImageIcon will be created normally, it just won't paint anything. Furthermore, you should read that article, it describes the whole process in detail and gives you helpful code and methods. Oh, and it looks like you never set a preferred or an absolute size on the JPanel. .

:)

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You