Una pregunta amigos, como podria meter todos los elementos en un jpanel, y que mantengan su orden ya que tengo que hacer varios jpanel.
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class Pregunta2 extends JFrame implements ActionListener{
private JPanel jpnl_contenedor;
private JLabel jlbl_pregunta1;
private JLabel jlbl_respuesta1;
private JLabel jlbl_respuesta2;
private JLabel jlbl_respuesta3;
private JLabel jlbl_respuesta4;
private JButton jbtn_respuesta1;
private JButton jbtn_respuesta2;
private JButton jbtn_respuesta3;
private JButton jbtn_respuesta4;
private JLabel jlbl_respuestaFinal;
private JLabel jlbl_imagen;
public Pregunta2(){
super("La verdad sobre Ellos...");
this.ordenarVentana();
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void ordenarVentana(){
/*Alineo los elementos uno a uno en Columnas-Filas,
es decir utilizo celdas */
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
jlbl_pregunta1 = new JLabel("¿Que color de pelo es el mas comun?");
jlbl_pregunta1.setFont(new Font("Arial", Font.BOLD, 20));
jlbl_pregunta1.setHorizontalAlignment(SwingConstants.CENTER);
jlbl_pregunta1.setHorizontalTextPosition( SwingConstants.CENTER );
jlbl_pregunta1.setVerticalTextPosition( SwingConstants.TOP );
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_pregunta1,gbc);
jlbl_respuesta1 = new JLabel("Opcion A - El pelo moreno");
jlbl_respuesta1.setFont(new Font("Arial", Font.PLAIN, 18));
jlbl_respuesta1.setHorizontalAlignment(SwingConstants.CENTER);
jlbl_respuesta1.setHorizontalTextPosition( SwingConstants.CENTER );
jlbl_respuesta1.setVerticalTextPosition( SwingConstants.CENTER );
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_respuesta1,gbc);
jlbl_respuesta2 = new JLabel("Opcion B - El pelo rubio");
jlbl_respuesta2.setFont(new Font("Arial", Font.PLAIN, 18));
jlbl_respuesta2.setHorizontalAlignment(SwingConstants.CENTER);
jlbl_respuesta2.setHorizontalTextPosition( SwingConstants.CENTER );
jlbl_respuesta2.setVerticalTextPosition( SwingConstants.CENTER );
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_respuesta2,gbc);
jlbl_respuesta3 = new JLabel("Opcion C - El pelo castaño");
jlbl_respuesta3.setFont(new Font("Arial", Font.PLAIN, 18));
jlbl_respuesta3.setHorizontalAlignment(SwingConstants.CENTER);
jlbl_respuesta3.setHorizontalTextPosition( SwingConstants.CENTER );
jlbl_respuesta3.setVerticalTextPosition( SwingConstants.CENTER );
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_respuesta3,gbc);
jlbl_respuesta4 = new JLabel("Opcion D - El pelo pelirrojo");
jlbl_respuesta4.setFont(new Font("Arial", Font.PLAIN, 18));
jlbl_respuesta4.setHorizontalAlignment(SwingConstants.CENTER);
jlbl_respuesta4.setHorizontalTextPosition( SwingConstants.CENTER );
jlbl_respuesta4.setVerticalTextPosition( SwingConstants.CENTER );
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_respuesta4,gbc);
jbtn_respuesta1 = new JButton("Opcion A");
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jbtn_respuesta1,gbc);
jbtn_respuesta2 = new JButton("Opcion B");
gbc.gridx = 1;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jbtn_respuesta2,gbc);
jbtn_respuesta3 = new JButton("Opcion C");
gbc.gridx = 2;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jbtn_respuesta3,gbc);
jbtn_respuesta4 = new JButton("Opcion D");
gbc.gridx = 3;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jbtn_respuesta4,gbc);
jlbl_respuestaFinal = new JLabel("¡Elige la opcion correcta!");
jlbl_respuestaFinal.setFont(new Font("Arial", Font.BOLD, 20));
jlbl_respuestaFinal.setForeground(Color.blue);
jlbl_respuestaFinal.setHorizontalAlignment(SwingConstants.CENTER);
jlbl_respuestaFinal.setHorizontalTextPosition( SwingConstants.CENTER );
jlbl_respuestaFinal.setVerticalTextPosition( SwingConstants.BOTTOM );
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_respuestaFinal,gbc);
jlbl_imagen = new JLabel("");
jlbl_imagen.setIcon(new ImageIcon(getClass().getResource("pelo.png")));
jlbl_imagen.setBounds(0 ,0 ,100 , 100);
jlbl_imagen.setHorizontalAlignment(SwingConstants.CENTER);
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 4;
gbc.gridheight = 4;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
add(jlbl_imagen,gbc);
jbtn_respuesta1.addActionListener(this);
jbtn_respuesta2.addActionListener(this);
jbtn_respuesta3.addActionListener(this);
jbtn_respuesta4.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtn_respuesta1){
jlbl_respuestaFinal.setText("¡¡¡Correcto, no solo en hombre tambien en mujeres");
jlbl_respuestaFinal.setForeground(Color.green);
}
if(e.getSource()==jbtn_respuesta2){
jlbl_respuestaFinal.setText("Puede ser el mas comun, pero de bote!!!!");
jlbl_respuestaFinal.setForeground(Color.red);
}
if(e.getSource()==jbtn_respuesta3){
jlbl_respuestaFinal.setText("Es raro si, pero no es asi...");
jlbl_respuestaFinal.setForeground(Color.red);
}
if(e.getSource()==jbtn_respuesta4){
jlbl_respuestaFinal.setText("Que Coraje de Pelo!!!");
jlbl_respuestaFinal.setForeground(Color.red);
}
}
}