Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Colocando un JPanel en una nueva línea usando GridBagLayout

Estoy tratando de encontrar una manera de colocar un panel que contenga cuadros combinados en mi JFrame en una nueva línea usando gridbaglayout. De momento aparece junto a otro panel del mismo tipo.

Esto es lo que parece:

https://i.stack.imgur.com/74Eui.gif

Aquí está el código para las áreas particulares

 JPanel saleOfferPanel = new JPanel(); JLabel dateOfSale = new JLabel("Select Date of Sale:"); saleOfferPanel.add(dateOfSale); JLabel saleDay = new JLabel("Day:"); saleOfferPanel.add(saleDay); JComboBox<Integer> dayOfSale = new JComboBox<>(); saleOfferPanel.add(dayOfSale); JLabel saleMonth = new JLabel("Month:"); saleOfferPanel.add(saleMonth); JComboBox<Integer> monthOfSale = new JComboBox<>(); saleOfferPanel.add(monthOfSale); JLabel saleYear = new JLabel("Year:"); saleOfferPanel.add(saleYear); JComboBox<Integer> yearOfSale = new JComboBox<>(); saleOfferPanel.add(yearOfSale); JPanel endSalePanel = new JPanel(); JLabel endOfSale = new JLabel("Select End Date of Sale:"); saleOfferPanel.add(endOfSale); JLabel endDay = new JLabel("Day:"); saleOfferPanel.add(endDay); JComboBox<Integer> endDayOfSale = new JComboBox<>(); saleOfferPanel.add(endDayOfSale); JLabel endMonth = new JLabel("Month:"); saleOfferPanel.add(endMonth); JComboBox<Integer> endMonthOfSale = new JComboBox<>(); saleOfferPanel.add(endMonthOfSale); JLabel endYear = new JLabel("Year:"); saleOfferPanel.add(endYear); JComboBox<Integer> endYearOfSale = new JComboBox<>(); saleOfferPanel.add(endYearOfSale); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 0.5; gbc.weighty = 0; gbc.anchor = GridBagConstraints.NORTHWEST; //other panels... //panels in question gbc.gridx = 0; gbc.gridy = 9; gbc.gridheight = 1; add(saleOfferPanel, gbc); gbc.gridx = 0; gbc.gridy = 10; gbc.gridheight = 1; add(endSalePanel, gbc);

Esperando que alguien pueda ofrecer ayuda.

Gracias

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Cambié un poco su código y logré cambiar su posición. aquí está el fragmento de código.

 `// setLayout(new GridBagLayout()); GridBagLayout gb = new GridBagLayout(); this.setLayout(gb); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1; gbc.weighty = 1; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.BOTH; //other panels... //panels in question gbc.gridwidth = GridBagConstraints.REMAINDER; //this will tell GridBagLayout component below should be last one on this line. gb.setConstraints(saleOfferPanel, gbc); this.add(saleOfferPanel); gb.setConstraints(endSalePanel, gbc); this.add(endSalePanel); this.pack(); this.setSize(500, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);`
over 4 years ago · Santiago Trujillo Denunciar

0

Mire cuidadosamente el código que crea y endSalePanel :

 JPanel endSalePanel = new JPanel(); JLabel endOfSale = new JLabel("Select End Date of Sale:"); //[KA] Here's where it all starts to go wrong!!! You wanted to add // endOfSale to endSalePanel, but... saleOfferPanel.add(endOfSale); JLabel endDay = new JLabel("Day:"); saleOfferPanel.add(endDay);

¡Cortar y pegar ataca de nuevo!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda