import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Testing extends JFrame {

	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run(){
				new Testing();
			}
		});
	}

	private Testing(){
		super("tester");
		launch();
	}

	private void launch(){
		setBounds(200, 200, 300, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel topPanel = new JPanel();
		topPanel.setSize(getWidth(), getHeight()/9);		
		topPanel.setBackground(Color.black);
		JButton t = new JButton("top");
		topPanel.add(t);

		JPanel bottomPanel = new JPanel();
		bottomPanel.setBackground(Color.blue);
		JButton b = new JButton("bottom");
		bottomPanel.add(b);
		
		add(topPanel);
		add(bottomPanel);
		setVisible(true);
	}
}

I'm still kinda new at this JPanel stuff, but why are my 2 JPanels overlapping? I mean the colors are split up perfectly, but the buttons come up one over the other when I hover the mouse over them even though I add each button to there own panel...

If you resize the window, you can see why....

I think I have to set the origin for the 2nd panel below the edge of the 1st panel right? I tried to use the x and y parts of bottomPanel.setBounds(int x, int y, int height, int width), but it seems to just ignore it...

Any suggestions?

Recommended Answers

All 2 Replies

Layouts are your friends

add(topPanel, BorderLayout.NORTH);

You should have go on these tutorials

Awesome.. Thanks. It works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.