logo

BorderLayout (LayoutManagers)

Gerenciadores de layout Java

Os LayoutManagers são usados ​​para organizar os componentes de uma maneira específica. O Gerenciadores de layout Java nos facilita controlar o posicionamento e o tamanho dos componentes em formulários GUI. LayoutManager é uma interface implementada por todas as classes de gerenciadores de layout. Existem as seguintes classes que representam os gerenciadores de layout:

comando git push
  1. java.awt.BorderLayout
  2. java.awt.FlowLayout
  3. java.awt.GridLayout
  4. java.awt.CardLayout
  5. java.awt.GridBagLayout
  6. javax.swing.BoxLayout
  7. javax.swing.GroupLayout
  8. javax.swing.ScrollPaneLayout
  9. javax.swing.SpringLayout etc.

Java BorderLayout

O BorderLayout é usado para organizar os componentes em cinco regiões: norte, sul, leste, oeste e centro. Cada região (área) pode conter apenas um componente. É o layout padrão de uma moldura ou janela. O BorderLayout fornece cinco constantes para cada região:

    público estático final int NORTE público estático final int SUL público estático final int EAST final estático público int OESTE público estático final int CENTRO

Construtores da classe BorderLayout:

    BorderLayout():cria um layout de borda, mas sem espaços entre os componentes.BorderLayout(int hgap, int vgap):cria um layout de borda com os espaços horizontais e verticais fornecidos entre os componentes.

Exemplo de classe BorderLayout: usando o construtor BorderLayout()

Nome do arquivo: Fronteira.java

 import java.awt.*; import javax.swing.*; public class Border { JFrame f; Border() { f = new JFrame(); // creating buttons JButton b1 = new JButton('NORTH');; // the button will be labeled as NORTH JButton b2 = new JButton('SOUTH');; // the button will be labeled as SOUTH JButton b3 = new JButton('EAST');; // the button will be labeled as EAST JButton b4 = new JButton('WEST');; // the button will be labeled as WEST JButton b5 = new JButton('CENTER');; // the button will be labeled as CENTER f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center f.setSize(300, 300); f.setVisible(true); } public static void main(String[] args) { new Border(); } } 

Saída:

Classe BorderLayoutbaixe este exemplo

Exemplo de classe BorderLayout: usando o construtor BorderLayout(int hgap, int vgap)

O exemplo a seguir insere espaços horizontais e verticais entre botões usando o construtor parametrizado BorderLayout(int hgap, int gap)

Nome do arquivo: BorderLayoutExample.java

 // import statement import java.awt.*; import javax.swing.*; public class BorderLayoutExample { JFrame jframe; // constructor BorderLayoutExample() { // creating a Frame jframe = new JFrame(); // create buttons JButton btn1 = new JButton('NORTH'); JButton btn2 = new JButton('SOUTH'); JButton btn3 = new JButton('EAST'); JButton btn4 = new JButton('WEST'); JButton btn5 = new JButton('CENTER'); // creating an object of the BorderLayout class using // the parameterized constructor where the horizontal gap is 20 // and vertical gap is 15. The gap will be evident when buttons are placed // in the frame jframe.setLayout(new BorderLayout(20, 15)); jframe.add(btn1, BorderLayout.NORTH); jframe.add(btn2, BorderLayout.SOUTH); jframe.add(btn3, BorderLayout.EAST); jframe.add(btn4, BorderLayout.WEST); jframe.add(btn5, BorderLayout.CENTER); jframe.setSize(300,300); jframe.setVisible(true); } // main method public static void main(String argvs[]) { new BorderLayoutExample(); } } 

Saída:

Classe BorderLayout

Java BorderLayout: sem especificar região

O método add() da classe JFrame pode funcionar mesmo quando não especificamos a região. Nesse caso, apenas o último componente adicionado é mostrado no quadro e todos os componentes adicionados anteriormente são descartados. O componente mais recente cobre toda a área. O exemplo a seguir mostra o mesmo.

Nome do arquivo: BorderLayoutWithoutRegionExample.java

 // import statements import java.awt.*; import javax.swing.*; public class BorderLayoutWithoutRegionExample { JFrame jframe; // constructor BorderLayoutWithoutRegionExample() { jframe = new JFrame(); JButton btn1 = new JButton('NORTH'); JButton btn2 = new JButton('SOUTH'); JButton btn3 = new JButton('EAST'); JButton btn4 = new JButton('WEST'); JButton btn5 = new JButton('CENTER'); // horizontal gap is 7, and the vertical gap is 7 // Since region is not specified, the gaps are of no use jframe.setLayout(new BorderLayout(7, 7)); // each button covers the whole area // however, the btn5 is the latest button // that is added to the frame; therefore, btn5 // is shown jframe.add(btn1); jframe.add(btn2); jframe.add(btn3); jframe.add(btn4); jframe.add(btn5); jframe.setSize(300,300); jframe.setVisible(true); } // main method public static void main(String argvs[]) { new BorderLayoutWithoutRegionExample(); } } 

Saída:

Classe BorderLayout