Quiero mostrar 2 objetos en JFrame. Intenté agregando objetos a JPanel y luego agregué JPanel a JFrame, pero tampoco funcionó. También traté de agregar objetos ball1 y ball1 directamente a JFrame, pero solo muestra el último objeto agregado. Quiero mostrar ambos objetos en JFrame a la vez. El código a continuación solo muestra el objeto ball1.Agregar 2 o más objetos a JFrame
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);
estoy dando diferentes coordenadas de los objetos donde están shown.I también han probado el segundo método, todavía sobrescribe el primer objeto. – user1717353
Incorrecto, usted llama 'f.getContentPane()' dos veces pasando 2 componentes diferentes, pero sin especificar restricciones. Y un panel con BorderLayout sin restricciones explícitamente especificado agregará el componente a la posición CENTRAL, el segundo reemplazando al primero. – icza