| | |
adding graphics to GUI
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
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:
Here is the code:
Java Syntax (Toggle Plain Text)
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); }
Last edited by christiangirl; Jan 5th, 2009 at 10:31 pm.
![]() |
Similar Threads
- Help with Inventory part 6 program please!! (Java)
- Please help Errors will not compile (Java)
- Help Finishing Inventory Part 6 (Java)
- Simple Hangman Game (VB.NET)
- Inventory graphics prob (Java)
- Inventory program part 5 help (Java)
- Inventory part 5 (Java)
- Spelling Test using a button GUI environment..how do I create a brand new window? (Java)
Other Threads in the Java Forum
- Previous Thread: PrimeNumbers.java
- Next Thread: java app with INSERT statement...
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet compiler component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel jtable julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working





