View Single Post
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

adding graphics to GUI

 
0
  #1
Jan 5th, 2009
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:
  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.
Reply With Quote