0
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?

2
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by TheWhite
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.