Results 1 to 1 of 1
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Rep Power

    Default A Simple way to use JButtons and place objects in a JFrame

    The code is self explanatory. I create JButtons in one class and put them in a JFrame with another class. I'll try to answer any questions you reply to this with :)

    Here I create my JButtons

    Java Code:
    This code creates one JButton that cycles through four
    ImageIcons.  To create the JButtons, you need to use 
    the class IconOrButtonImplementor, or another class that
    imports the class.
    /*Possible problem: 
     * Using method:    JButton myButton = new JButton();
     * This creates a separate JButton.  Because the class
     * extends JButton, the class itself is a JButton.
     * Because of this, you do NOT need to create a JButton.
     * You simply use the statements to modify the class
     * itself, import the class to the class creating the
     * frame, and create as many JButtons as you need.
     * I encountered this problem in the original creation of
     * this class.
    package buttonCreators;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    //SuppressWarnings is needed for Eclipse only.  May or
    //may not affect Java created in other IDEs 
    //and/or text editor
    public class ImageIconCycler extends JButton implements ActionListener{
    	/**creates all of my ImageIcons.  To compile, 
    	you will need to set the strings properly for your computer
    	public ImageIcon up, right, left,notmoving;{
    		up = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("pictures/Forward Arrow.PNG"));
    		right = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("pictures/Right Arrow.PNG"));
    		left = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("pictures/Left Arrow.PNG"));
    		notmoving = new ImageIcon(ClassLoader.getSystemClassLoader().getResource("pictures/No Move.PNG"));	
    	//sets initial state of the JButton ImageIconCycler
    	//remember, the class IS the JButton
    	public ImageIconCycler(){
    	// these are the actions to be performed by the JButton.
        public void actionPerformed(ActionEvent e) {
            if ("notmoving".equals(e.getActionCommand())) {
                setIcon (left);
                if ("left".equals(e.getActionCommand())) {
                    setIcon (up);
            	if ("up".equals(e.getActionCommand())) {
                    setIcon (right);
            	if ("right".equals(e.getActionCommand())) {
                    setIcon (notmoving);
    This code places objects into a JFrame. It is currently set to use the ImageIconCycler class above.

    Java Code:
     	This class places four identical objects that have been created
     	in a separate class in a single column within a JFrame.
      	Uses ImageIconCycler as example for the previously 
      	created objects.  (JButtons.ImageIconCycler)
      	This class can be used for any other Class that creates
      	an object.
    package UniversalUsage;
    import buttonCreators.ImageIconCycler;
    /**IMPORTANT: this is a class I created myself.  It is NOT a standard class 
    from the original Java JDK or SDK downloads.
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    /** SuppressWarnings is needed for Eclipse only.  It may 
    or may not affect Java created in other IDEs 
    and/or text editors */
    public class IconOrButtonImplementor{
    	public static void main(String[] args) {
    		ImageIconCycler arrows1 = new ImageIconCycler();
    		ImageIconCycler arrows2 = new ImageIconCycler();
    		ImageIconCycler arrows3 = new ImageIconCycler();
    		ImageIconCycler arrows4 = new ImageIconCycler();
    	 * The setBounds methods are used to position the
    	 * objects in a Column.  A Layout Manager could also
    	 * be used instead of the setBounds Methods
    		arrows4.setBounds(50, 230,50,50);
    		JPanel mypanel = new JPanel();
    		JFrame myframe = new JFrame();
    For the above code to compile correctly, you will need to set strings and imports, or create the proper packages and classes. The attached images are the ones used by my code. They are in a package (pictures), the ImageIconCycler is in a package (buttonCreators) that is different from where the class IconOrButtonImplementor is (uncategorized).
    Attached Thumbnails Attached Thumbnails A Simple way to use JButtons and place objects in a JFrame-forward-arrow.png   A Simple way to use JButtons and place objects in a JFrame-left-arrow.png   A Simple way to use JButtons and place objects in a JFrame-right-arrow.png   A Simple way to use JButtons and place objects in a JFrame-no-move.png  
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2011, 03:44 AM
  2. Help with JButtons into JFrame
    By g6pd in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 04:49 AM
  3. Using a grid to place objects
    By Shellback3 in forum AWT / Swing
    Replies: 2
    Last Post: 01-29-2011, 11:37 PM
  4. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  5. How do i place objects onto a grid?
    By Nakira in forum New To Java
    Replies: 7
    Last Post: 11-14-2008, 06:49 PM

Posting Permissions

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