Results 1 to 8 of 8
  1. #1
    owain1221 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Exclamation Posistioning Jpanels

    Okay, ive got a project and would like to know how to position Jpanels in a Jframe.

    I'm making a fruit machine and cannot posistion the buttons and frames as i do not know how too, please may i have some help


    Many Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you avoid using absolute positioning and instead learn to use the AWT/Swing layout managers. As always, the Sun Swing tutorials will show you the way. Please start here.

  3. #3
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Gridlayout() is the easiest way

    if you want all of your buttons in one row add them to their own panel and it will automatically do flowlayout. but you shoould read the api on gridlayout do you have the api on your computer? ill post a link

  4. #4
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Java 2 Platform SE 5.0


    scroll down on the left window till you see gridlayout

  5. #5
    owain1221 is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    they all mention buttons but i want to have Jpanels there as im making a fruit machine and i need images in them

    here is my code it wont work, dont kno why. so far only one column and a exit button.

    Java Code:
    mport java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    public class TheElectronicBandit extends JFrame implements ActionListener
    {
    
    	private JPanel panelLine1Top, panelLine1Middle, panelLine1Bottom;
    	private JButton btnClose;
    	
    	public static void main(String[] args) 
    	{
    		TheElectronicBandit fruitMachine = new TheElectronicBandit();
    
    	}
    	
    	public void fruitmachine()
    	{
    		JPanel Line1 = new JPanel();
    		setLayout(new BoxLayout(Line1, BoxLayout.X_AXIS));
    		panelLine1Top = new JPanel();
    		panelLine1Top.setSize(50, 50);
    		
    		panelLine1Middle = new JPanel();
    		panelLine1Middle.setSize(50, 50);
    		panelLine1Middle.setBackground(Color.blue);
    		
    		panelLine1Bottom = new JPanel();
    		panelLine1Bottom.setSize(50, 50);
    		
    		add(panelLine1Top); add(panelLine1Middle); add(panelLine1Bottom);  add(btnClose); 
    		
    		setTitle("The Electronic Bandit - Can you beat the bandit?!");
    		setSize(500, 500);
    		setVisible(true);
    	}
    	public void actionPerformed(ActionEvent event)
    	{
    		if(event.getSource()== btnClose)
    		{
    		System.exit(0);
    		}
    	}
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    they all mention buttons but i want to have Jpanels
    I don't know what you mean here as the tutorials in my link above can be applied to JPanels or any other Swing component.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by owain1221 View Post
    here is my code it wont work, dont kno why. so far only one column and a exit button.
    Where do you call code that actually creates and displays a GUI? Your main method creates and initializes a TheElectronicBandit object, but this object has no constructor. So what happens is a JFrame-derived object is created, but since it is never displayed, the program just ends.

    You need to give this class a constructor and construct your GUI within this constructor.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This code is a bit suspect:
    Java Code:
        JPanel Line1 = new JPanel();
        setLayout(new BoxLayout(Line1, BoxLayout.X_AXIS));
    You create a JPanel Line1 and never use it, but then create a BoxLayout which you are adding by default to a JFrame's contentPane, but then tell the box layout to use the Line1 JPanel instead. This will surely cause an error and isn't how BoxLayout is to be used. Please check out the BoxLayout tutorial (you can get to it from my first link above) as it will show you how to use this layout. First and foremost, the component that is within the BoxLayout constructor call must be the same component that sets its layout to boxlayout.

Similar Threads

  1. JList and JPanels
    By JetsYanks in forum New To Java
    Replies: 8
    Last Post: 12-25-2009, 03:11 PM
  2. Multiple JPanels Not Working
    By jgezau in forum AWT / Swing
    Replies: 5
    Last Post: 04-17-2009, 05:52 AM
  3. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 12:46 PM
  4. Help with overlapping JPanels
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 03-09-2009, 11:19 PM
  5. How to add Images to JPanels?
    By Soda in forum New To Java
    Replies: 3
    Last Post: 12-08-2007, 06:54 PM

Tags for this Thread

Posting Permissions

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