Advertisement
luistavares

Utilizando o Layout BorderLayout em Java

Apr 19th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.TitledBorder;
  7.  
  8. public class ExemploBorderLayout {
  9.  
  10.     public static void main(String[] args) {
  11.         // Criando 5 rótulos de texto.
  12.         JLabel label1 = new JLabel("Norte");
  13.         JLabel label2 = new JLabel("Oeste");
  14.         JLabel label3 = new JLabel("Centro");
  15.         JLabel label4 = new JLabel("Leste");
  16.         JLabel label5 = new JLabel("Sul");
  17.  
  18.         // Criando 5 painéis com borda.
  19.         JPanel painel1 = new JPanel();
  20.         painel1.setBorder(new TitledBorder("B1"));
  21.         JPanel painel2 = new JPanel(); 
  22.         painel2.setBorder(new TitledBorder("B2"));
  23.         JPanel painel3 = new JPanel(); 
  24.         painel3.setBorder(new TitledBorder("B3"));
  25.         JPanel painel4 = new JPanel(); 
  26.         painel4.setBorder(new TitledBorder("B4"));
  27.         JPanel painel5 = new JPanel();
  28.         painel5.setBorder(new TitledBorder("B5"));
  29.        
  30.         // Adicionando os rótulos de texto nos painéis.
  31.         painel1.add(label1);
  32.         painel2.add(label2);
  33.         painel3.add(label3);
  34.         painel4.add(label4);
  35.         painel5.add(label5);
  36.        
  37.         // Dividindo o painél principal em regiões.
  38.         // Adicionando um painél em cada região.
  39.         JPanel painelPrincipal = new JPanel(new BorderLayout());
  40.         painelPrincipal.add(painel1, BorderLayout.NORTH);
  41.         painelPrincipal.add(painel2, BorderLayout.WEST);
  42.         painelPrincipal.add(painel3, BorderLayout.CENTER);
  43.         painelPrincipal.add(painel4, BorderLayout.EAST);
  44.         painelPrincipal.add(painel5, BorderLayout.SOUTH);
  45.        
  46.         // Criando uma janela e adicionando o painél principal.
  47.         JFrame frame = new JFrame("Exemplo BorderLayout");
  48.         frame.getContentPane().add(painelPrincipal);
  49.         frame.setSize(400, 400);
  50.         frame.setVisible(true);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement