I have to write a very simple code (GUI code) and my last buttons are supposed to be in the middle at the bottom of the frame. Well, they are not in the middle. They are at the bottom but on the left side of the frame.
/* I called my JFrame win; :)
* The JPanel Jp1;
* I used this: win.add(Jp1, BorderLayout.CENTER);
* What did I do wrong? I don't want to post the whole code cause everything is working;
* well except this;
*/

You'll want to add the the JButton to a JPanel, and add the JPanel to the JFrame.

JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("My Button");

panel.add(button);
frame.setLayout(new BorderLayout()); // might not need this, test it.
frame.add(panel, BorderLayout.CENTER);

Well, that's exactely what I did... hahaha... But it's not working. I changed it to NORTH just to see how the output will look like but it didn't change either.

Im getting the same problem, my two JButtons dont appear where I want them too. I think it something to do with the Layout but I am not sure. I aint using a layout manager but when I do it shows buttons in the top centre :(

Yeah. It may be because of the way we align them. I don't know. I'm gonna go through it again and I hope I'm gonna get something positive. Good luck buddy. I hope you gonna share it if you find a solution for yours. if I find one, I will post it too. :u

If you want to align components properly you have a couple of options. Let's say you just want to use the one panel set on top of a frame. You could set the layout of the panel to GridBagLayout and specify exactly where you want each button to be, its size and its style (not just through GridBagLayout)

You could also create several panels and place each one onto a frame and set the frame's layout to BorderLayout or whatever to tell it where to place each panel. Here's a simple example using two classes:

import java.awt.BorderLayout;
import javax.swing.*;

public class dummyFrame extends JFrame 
{
	dummyPanel dp = new dummyPanel();
	
	public dummyFrame()
	{
		setSize( 300,300 );
		setLayout( new BorderLayout() );
		
		add( dp, BorderLayout.SOUTH );
		
		setVisible( true );
		setLocationRelativeTo( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}
	
	public static void main(String [] args)
	{
		new dummyFrame();
	}
} // END CLASS

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.*;

public class dummyPanel extends JPanel 
{
	JButton aButton;
	GridBagConstraints c = new GridBagConstraints();
	
	public dummyPanel()
	{
		setLayout( new GridBagLayout() );
		setBackground(Color.BLACK);
		
		aButton = new JButton( "Press Me" );
		c.gridx = 0;
		c.gridy = 0;
		add( aButton, c );
		
		aButton = new JButton( "No - Press Me!" );
		c.gridx = 1;
		c.gridy = 0;
		add( aButton, c );
		
		aButton = new JButton( "Yo!" );
		c.gridx = 3;
		c.gridy = 0;
		add( aButton, c );
	}
} // END CLASS

Also if you add buttons to a frame but don't specify the layout they will be placed onto of one another whereas if you set the layout to flow layout they will all appear in sequence :P Hope this cleared it the problem alittle

BTW to the OP we need to see a-bit of coding to demonstrate effort - I've probably given away to much already :P but for your original question you could use GridBagLayout as I have above - notice the middle button? Examine it's c.gridx & c.gridy positions...

This article has been dead for over six months. Start a new discussion instead.