Jak zrobić żeby dwa panele były obok siebie?

0
public class MyApplet extends JApplet {
	static MyApplet m;
	Panel p;
	Panel2 p2;
	@Override
	public void init() {
		m = this;
		this.setSize(800, 600);
		this.setLayout(null);
		
		URL url = this.getClass().getResource("t.jpg");
		Image image = getImage(url);
		URL url2 = this.getClass().getResource("t1.jpg");
		Image image2 = getImage(url2);
		p = new Panel(image);
		p.setBounds(0, 0, 500, 600);
		this.add(p);
		p2 = new Panel2(image2);
		p2.setBounds(600, 10, 180, 580);
		this.add(p2);
	}
}

public class Panel extends JPanel {
	Image image;
	
	public Panel(Image image) {
		this.image = image;
	}

	protected void paintComponent(Graphics g) {
		g.drawImage(image, 0, 0, this);
	}
}

public class Panel2 extends JPanel {
	Image image;
	JButton b; 
	
	public Panel2(Image image) {
		this.image = image;
	}

	protected void paintComponent(Graphics g) {
		g.drawImage(image, 580, 10, this);
		b = new JButton();
		b.setBounds(600, 20, 100, 50);
		add(b);
	}
}

Mam taki kod i chciałby zrobić tak żeby dwa panele były obok siebie, albo żeby jeden był nad drugim. Niestety nie wiem jak to zrobić. To co ma wyświetla tylko jeden panel, ten który był pierwszy dodany do apletu. Jak zrobić żeby oba panele były jeden nad drugim albo obok siebie???

0

Panele są obok siebie, ale masz drobny błąd w Panel2

class Panel2 extends JPanel {
	Image image;
	JButton b;

	public Panel2(Image image) {
		this.image = image;
		b = new JButton(); //musi byc w konstruktorze
		add(b);
	}

	protected void paintComponent(Graphics g) {
		g.drawImage(image, 0, 0, this); //musi byc 0, gdyz jest liczone od tego komponentu, nie od rodzica
	}
}

Ale łatwiej użyć JSplitPane albo któregoś z LayoutManagerów (np. BorderLayout, jeden z lewej, drugi z prawej).

1 użytkowników online, w tym zalogowanych: 0, gości: 1