943,882 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3594
  • Java RSS
Jan 5th, 2009
-1

adding graphics to GUI

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. /**
  7.   Quick example showing images (specifically ImageIcons) on
  8.   JButtons and JLabels
  9.  
  10.   @author Sharon Tuttle
  11.   @version 11-04-08
  12. */
  13.  
  14. public class ImageIconEx1
  15. {
  16. /**
  17.   creates an ImageIconEx1Frame
  18.   @param args not used here
  19.   */
  20.  
  21. public static void main(String args[])
  22. {
  23. EventQueue.invokeLater(
  24. new Runnable()
  25. {
  26. public void run()
  27. {
  28. ImageIconEx1Frame mainFrame = new ImageIconEx1Frame();
  29. mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  30. mainFrame.setVisible(true);
  31. }
  32. });
  33. }
  34. }
  35. /**
  36.   A frame with a panel containing buttons and labels featuring
  37.   image icons
  38. */
  39.  
  40. class ImageIconEx1Frame extends JFrame
  41. {
  42. /**
  43.   constructs an ImageIconEx1Frame instance
  44.   */
  45.  
  46. public ImageIconEx1Frame()
  47. {
  48. this.setTitle("ImageIcons on JButtons and JLabels");
  49. this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  50.  
  51. // add ImageIconEx1 panel to frame
  52.  
  53. ImageIconEx1Panel panel = new ImageIconEx1Panel();
  54. this.add(panel);
  55. }
  56.  
  57. // data fields
  58.  
  59. private final static int DEFAULT_WIDTH = 500;
  60. private final static int DEFAULT_HEIGHT = 400;
  61. }
  62.  
  63. /**
  64.   A panel containing buttons and labels featuring
  65.   image icons
  66. */
  67.  
  68. class ImageIconEx1Panel extends JPanel
  69. {
  70. /**
  71.   constructs a ImageIconEx1 panel instance
  72.   */
  73.  
  74. public ImageIconEx1Panel()
  75. {
  76. ImageIcon myImageIcon = new ImageIcon("C:'\'Users'\'Kimberlie'\'Desktop'\'BlackJackGui'\'train.gif");
  77.  
  78. JButton myImageButton = new JButton(myImageIcon);
  79. JLabel myImageLabel = new JLabel(myImageIcon);
  80.  
  81. JButton myTextAndImageButton = new JButton("Button Text", myImageIcon);
  82. myTextAndImageButton.setFont(DISPLAY_FONT);
  83.  
  84. // note: the third argument here is with regard to the LABEL's horizontal
  85. // alignment, NOT the label's text;
  86.  
  87. JLabel myTextAndImageLabel = new JLabel("Label Text", myImageIcon,
  88. SwingConstants.CENTER);
  89. myTextAndImageLabel.setFont(DISPLAY_FONT);
  90.  
  91. this.add(myImageButton);
  92. this.add(myImageLabel);
  93. this.add(myTextAndImageButton);
  94. this.add(myTextAndImageLabel);
  95.  
  96. } // end ImageIconEx1Panel constructor
  97.  
  98. // data fields for ImageIconEx1Panel
  99.  
  100. private final static Font DISPLAY_FONT = new Font(
  101. "Dialog", Font.PLAIN, 20);
  102. }
Last edited by christiangirl; Jan 5th, 2009 at 10:31 pm.
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Feb 25th, 2010
-1

GUI with graphics

kindly make a program that can make GUI that can draw graphics when the buttons is clicked.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
alzymore is offline Offline
1 posts
since Feb 2010
Feb 25th, 2010
0
Re: adding graphics to GUI
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. .

Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Hi! Have a look see.
Next Thread in Java Forum Timeline: Function for escaping strings?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC