Results 1 to 1 of 1
- 03-10-2009, 09:55 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
This code places objects into a JFrame. It is currently set to use the ImageIconCycler class above.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 @SuppressWarnings("serial") 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(){ setIcon(notmoving); setActionCommand("notmoving"); addActionListener(this); } // these are the actions to be performed by the JButton. public void actionPerformed(ActionEvent e) { if ("notmoving".equals(e.getActionCommand())) { setIcon (left); setActionCommand("left"); } { if ("left".equals(e.getActionCommand())) { setIcon (up); setActionCommand("up"); } { if ("up".equals(e.getActionCommand())) { setIcon (right); setActionCommand("right"); } { if ("right".equals(e.getActionCommand())) { setIcon (notmoving); setActionCommand("notmoving"); } } } } } }
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).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 */ @SuppressWarnings("serial") 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 */ arrows1.setBounds(50,20,50,50); arrows2.setBounds(50,90,50,50); arrows3.setBounds(50,160,50,50); arrows4.setBounds(50, 230,50,50); JPanel mypanel = new JPanel(); JFrame myframe = new JFrame(); mypanel.setLayout(null); mypanel.add(arrows1); mypanel.add(arrows2); mypanel.add(arrows3); mypanel.add(arrows4); myframe.setContentPane(mypanel); myframe.setSize(150,350); myframe.setVisible(true); } }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
-
How can I place two JButtons in the middle of the JFrame?
By rajkobie in forum New To JavaReplies: 2Last Post: 04-29-2011, 03:44 AM -
Help with JButtons into JFrame
By g6pd in forum New To JavaReplies: 5Last Post: 03-10-2011, 04:49 AM -
Using a grid to place objects
By Shellback3 in forum AWT / SwingReplies: 2Last Post: 01-29-2011, 11:37 PM -
JFrame: JButtons in a grid
By jackal in forum New To JavaReplies: 2Last Post: 06-09-2010, 07:56 PM -
How do i place objects onto a grid?
By Nakira in forum New To JavaReplies: 7Last Post: 11-14-2008, 06:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks