logo

BorderLayout (LayoutManagers)

Java LayoutManagers

LayoutManagers tiek izmantoti, lai sakārtotu komponentus noteiktā veidā. The Java LayoutManagers palīdz mums kontrolēt komponentu izvietojumu un izmērus GUI formās. LayoutManager ir saskarne, ko ievieš visas izkārtojuma pārvaldnieku klases. Ir šādas klases, kas pārstāv izkārtojuma pārvaldniekus:

tcp un ip modelis
  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 utt.

Java BorderLayout

BorderLayout tiek izmantots, lai sakārtotu komponentus piecos reģionos: ziemeļos, dienvidos, austrumos, rietumos un centrā. Katrā reģionā (apgabalā) var būt tikai viens komponents. Tas ir noklusējuma rāmja vai loga izkārtojums. BorderLayout nodrošina piecas konstantes katram reģionam:

    publisks statisks fināls in ZIEMEĻI publiska statiskā fināla int DIENVIDI publiska statiskā fināla int EAST publisks statisks fināls int RIETUMI publiska statiska gala int CENTRS

BorderLayout klases konstruktori:

    BorderLayout():izveido apmales izkārtojumu, bet bez atstarpēm starp komponentiem.Apmales izkārtojums (int hgap, int vgap):izveido apmales izkārtojumu ar dotajām horizontālajām un vertikālajām atstarpēm starp komponentiem.

BorderLayout klases piemērs: BorderLayout() konstruktora izmantošana

Faila nosaukums: Border.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(); } } 

Izvade:

BorderLayout klaselejupielādējiet šo piemēru

BorderLayout klases piemērs: konstruktora BorderLayout(int hgap, int vgap) izmantošana

Šajā piemērā starp pogām tiek ievietotas horizontālās un vertikālās atstarpes, izmantojot parametrizēto konstruktoru BorderLayout(int hgap, int gap)

Faila nosaukums: 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(); } } 

Izvade:

BorderLayout klase

Java BorderLayout: nenorādot reģionu

JFrame klases metode add() var darboties pat tad, ja nav norādīts reģions. Šādā gadījumā rāmī tiek parādīts tikai pēdējais pievienotais komponents, un visi iepriekš pievienotie komponenti tiek izmesti. Jaunākā sastāvdaļa aptver visu apgabalu. Nākamais piemērs parāda to pašu.

Faila nosaukums: 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(); } } 

Izvade:

BorderLayout klase