2011-10-26 16 views
5

Necesito mostrar un solo componente dentro de un JPanel, y quiero mantener ese componente en la esquina inferior derecha en todo momento. Traté de hacerlo con GridBagLayout:¿Cómo poner el componente en la esquina inferior derecha con GridBagLayout?

val infoArea = new TextArea { 
    text = "Hello!" 
    border = Swing.EmptyBorder(30) 
    background = Color.RED 
    editable = false 
} 
val p = new JPanel 
p.setLayout(new GridBagLayout) 
val c = new GridBagConstraints 
c.gridx = 0 
c.gridy = 0 
c.anchor = GridBagConstraints.LAST_LINE_END 
p.add(infoArea.peer,c) 
val f = new JFrame 
f.setContentPane(p) 
f.setVisible(true) 

Sin embargo, el área de texto está en el centro por alguna razón:

enter image description here

¿qué estoy haciendo mal aquí?

+0

Usted no ha leído el tutorial GridBagLayout aparece por su código (añadiendo solo un componente y sin agregar algunos componentes ficticios para llenar el JPanel, colocando cosas gridx y gridy 0, sin configurar todas las pertinentes GridBagConstraints ...), y no se puede esperar nada de esto complejo para trabajar sin estudiar primero. De todos modos, usaría JPanels anidados usando diseños más simples como BorderLayouts. –

+0

@HovercraftFullOfEels - Esperaba tener solo una celda y el componente que se colocaría en la parte inferior derecha (al establecer la restricción de anclaje). ¿Y qué otras restricciones son pertinentes aquí? – Rogach

+0

weightx, pesado para uno, pero independientemente, si solo está agregando un componente, no se puede hacer con GridBagLayout. Una vez más, anide JPanels cada uno con su propio diseño. De nuevo, BorderLayout funcionaría bien. –

Respuesta

5
final JFrame frame = new JFrame(); 
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
frame.setLayout(new GridBagLayout()); 

GridBagConstraints gbc = new GridBagConstraints(); 
gbc.gridx = 0; 
gbc.gridy = 0; 
gbc.fill = GridBagConstraints.BOTH; 
gbc.weightx = 1.0; 
gbc.weighty = 1.0; 
frame.add(Box.createGlue(), gbc); 

final JTextArea textArea = new JTextArea("SE"); 
textArea.setPreferredSize(new Dimension(50, 50)); 
textArea.setOpaque(true); 
textArea.setBackground(Color.RED); 
gbc = new GridBagConstraints(); 
gbc.gridx = 1; 
gbc.gridy = 1; 
gbc.fill = GridBagConstraints.NONE; 
gbc.weightx = 0.0; 
gbc.weighty = 0.0; 
frame.add(textArea, gbc); 

frame.setSize(640, 480); 
frame.setVisible(true); 

... si realmente quiere usar GridBagLayout

+1

Sí, usaste el "pegamento" de la Caja como tu componente ficticio. bien hecho, y 1+. –

+0

Esto funcionó mejor para mí. Por alguna razón, cuando usé dos diseños de bordes, toda la parte derecha del panel principal estaba pintada de gris. – Rogach

8

Por ejemplo:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 

import javax.swing.*; 

public class LayoutDemo { 
    private static void createAndShowGui() { 
     JLabel label = new JLabel("Hello"); 
     label.setOpaque(true); 
     label.setBackground(Color.red); 

     JPanel bottomPanel = new JPanel(new BorderLayout()); 
     bottomPanel.add(label, BorderLayout.LINE_END); 

     JPanel mainPanel = new JPanel(new BorderLayout()); 
     mainPanel.add(bottomPanel, BorderLayout.PAGE_END); 
     mainPanel.setPreferredSize(new Dimension(400, 400)); 


     JFrame frame = new JFrame("LayoutDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

enter image description here

Cuestiones relacionadas