Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display 2 objects on JFrame.

I tried by adding objects to JPanel and then add JPanel to JFrame but it also did not work.

I also tried to add ball and ball1 objects directly to JFrame but it shows the last added object only. I want to show both objects on JFrame at a time.

The below given code only shows ball1 object.
Java
JFrame f = new JFrame("Moving"); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//making 2 objects 
Ballbewegung2 ball = new Ballbewegung2();
Ballbewegung3 ball1 = new Ballbewegung3(); 
JPanel contentPane = new JPanel(new BorderLayout());
JPanel contentPane1 = new JPanel(new BorderLayout());                                  
//adding objects to JPanel
contentPane.add(ball, BorderLayout.CENTER);                 
contentPane1.add(ball1, BorderLayout.CENTER);                 
//Adding JPanel to JFrmae
f.getContentPane().add(contentPane);
f.getContentPane().add(contentPane1);
f.setSize(500, 500);
f.setVisible(true);
Posted

1 solution

You're adding both Objects to the same area in the Borderlayout. That's why the first Object gets overwritten.

Try this:
Java
JFrame f = new JFrame("Moving"); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//making 2 objects 
Ballbewegung2 ball = new Ballbewegung2();
Ballbewegung3 ball1 = new Ballbewegung3(); 
// JPanel contentPane = new JPanel(new BorderLayout());
// JPanel contentPane1 = new JPanel(new BorderLayout());                                  
//adding objects to JPanel
//contentPane.add(ball, BorderLayout.CENTER);       
contentPane.add(ball, BorderLayout.NORTH);                 
contentPane1.add(ball1, BorderLayout.CENTER);                 
//Adding JPanel to JFrmae
f.getContentPane().add(contentPane);
f.getContentPane().add(contentPane1);
f.setSize(500, 500);
f.setVisible(true);

It would be nice to have Ballbewegung as a direct extension to Jpanel - that would solve 2 problems at once.

Additional:
This is a simple demonstration to show the function of the Borderlayout.
Please note that every JPanel is added to another region - except the "another South" Panel, which overwrites the original "South" Panel.
Java
public class MultiplePanels extends JFrame {
	
	public static void main(String[] args) {
		MultiplePanels oFrame = new MultiplePanels();
		oFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		oFrame.setSize(600, 300);
		oFrame.setVisible(true);
		
		oFrame.add(new MyPanel("North"),  BorderLayout.NORTH); 
		oFrame.add(new MyPanel("Center"), BorderLayout.CENTER);
		oFrame.add(new MyPanel("West"),   BorderLayout.WEST);
		oFrame.add(new MyPanel("East"),   BorderLayout.EAST);
		oFrame.add(new MyPanel("South"),  BorderLayout.SOUTH);
		// this one will overwrite the original south Panel:
		oFrame.add(new MyPanel("another South"),  BorderLayout.SOUTH); 
	}
}

Java
public class MyPanel extends JPanel {
	
	public MyPanel(String strName) { // I'm passing an argument to the JPanel
		this.setLayout(new BorderLayout()); // The panel got a layout too
		// Components can be declared and initialized on the fly - as long as they do not need to be changed later.
		this.add(createLabel(strName), BorderLayout.CENTER);
	}
	private Component createLabel(String strName) {
		JLabel lblPanelnumber = new JLabel("This is the Panel " + strName); 
		lblPanelnumber.setHorizontalAlignment(SwingConstants.CENTER);
		return lblPanelnumber;
	}
}


Second Additional:

Java
public class MultiplePanels extends JFrame {
	
	public MultiplePanels() {
		
	}
	
	public static void main(String[] args) {
		MultiplePanels oFrame = new MultiplePanels();
		oFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		oFrame.setSize(400, 400);
		oFrame.setBackground(Color.WHITE);
		oFrame.setVisible(true);
		oFrame.setLayout(new GridLayout(2, 1));

		oFrame.getContentPane().add(new Ballbewegung(10, 40)); 
		oFrame.getContentPane().add(new Ballbewegung(10, 80)); 
	}
}


Java
class Ballbewegung extends JPanel implements Runnable {
	// Initialisierung der Variablen
	int x_pos = 10; // x - Position des Balles
	int y_pos = 100; // y - Position des Balles
	int radius = 20; // Radius des Balles
	
	public Ballbewegung(int x_pos, int y_pos){
		this.x_pos = x_pos;
		this.y_pos = y_pos;
		init();
	}

	public void init() {
//		setBackground(Color.blue);
		this.setOpaque(false);
		this.start();
	}

	public void start() {
		Thread th = new Thread(this);
		th.start();
	}

	@Override
	public void run() {
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while (true) {
			x_pos++;
			if (x_pos >= 400)
				x_pos = 10;
			repaint();
			try {
				Thread.sleep(200);
			} catch (InterruptedException ex) {
			}
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.red);
		g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
	}
}
 
Share this answer
 
v4
Comments
naeemashfaq 3-Oct-12 15:10pm    
Thanks for replying. How can I have direct extension to Jpanel? I have also tried with default layout but all in vain. I am also unable to view the code
TorstenH. 3-Oct-12 15:19pm    
are you American or German? cause those class names are German.

You can extend the c lass just by adding an "extends JPanel" to your object Ballbewegung declaration. Then you treat your Ballbewegung directly like a JPanel.
naeemashfaq 3-Oct-12 15:57pm    
I am from Sweden. I am new to Java programming. I am using the same Border Layout but the coordinates where I am displaying the objects are different. I tried this code but it also shows last object. My classes are extending JPanel.then how can I show both objects?
TorstenH. 4-Oct-12 1:39am    
I have added some code to show you what happens. You might want to try this one.
naeemashfaq 4-Oct-12 6:56am    
This is my class that creates a circle shape with specific x and y coordinates. Similarly I have another same class but with different x and y coordinate positions. I create 1 object of each class and want to display them on specific positions. I tried the code, it works well with your example but not working with my classes.
class Ballbewegung2 extends JPanel implements Runnable
{
// Initialisierung der Variablen
int x_pos = 10; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int radius = 20; // Radius des Balles
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
Thread th = new Thread (this);
th.start ();
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
x_pos ++;
if(x_pos >= 400)
x_pos = 10;
repaint();
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900