[JavaSwing] BorderLayout
As you know the Container (as JFrame, JPanel, ...) used to store the control of its, however they may or may not default arrangement of objects is not our desired. So we need to use to do this Layout. In other words the Layout help we can arrange the containers in a reasonable manner and beautiful.
Content
Create and place for Container BorderLayout
Create log frame with BorderLayout
BorderLayout allows us to arrange the objects in 5 main areas (NORTH, SOUTH, WEST, EAST và CENTER) as shown below:
Also we can replace it with PAGE_START, PAGE_END, LINE_START, LINE_END, CENTER then also obtained such layout.
Code generated BorderLayout
package nguyenvanquan7826.BorderLayout; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; public class MyBorderLayout extends JFrame { public MyBorderLayout() { createJFrame(); } private void createJFrame() { setTitle("My BorderLayout"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); // set BorderLayout for JFrame setLayout(new BorderLayout()); // add content add(new JButton("NORTH"), BorderLayout.NORTH); add(new JButton("WEST"), BorderLayout.WEST); add(new JButton("CENTER"), BorderLayout.CENTER); add(new JButton("EAST"), BorderLayout.EAST); add(new JButton("SOUTH"), BorderLayout.SOUTH); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MyBorderLayout(); } }
We have 2 BorderLayout constructor method:
– BorderLayout(): BorderLayout initialized with the distance between the default position 0.
– BorderLayout(int hgap, int vgap): BorderLayout initialized with the distance between the designated position horizontally and vertically.
Create a log frame using BorderLayout
The BorderLayout we use is very common for our program. For example below is an example of log frame using BorderLayout.
Logging framework used 1 JPanel is MainPanel placed BorderLayout, MainPanel contains 5 JPanel with (itemPanel) placed respectively at 5 location of BorderLayout as known. In which the 3 ItemPanel in position WEST, CENTER EAST and then there 1 GridLayout including 2 line and 1 post.
package nguyenvanquan7826.BorderLayout; import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginFrame extends JFrame { public LoginFrame() { createJFrame(); } // create JFrame private void createJFrame() { setTitle("Login use BorderLayout"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 150); // add main panel add(createMainPanel()); // display setLocationRelativeTo(null); setVisible(true); } // create main panel private JPanel createMainPanel() { JPanel panel = new JPanel(new BorderLayout(10, 10)); panel.add(createTitlePanel(), BorderLayout.NORTH); panel.add(createNamePanel(), BorderLayout.WEST); panel.add(createInputPanel(), BorderLayout.CENTER); panel.add(createStatusPanel(), BorderLayout.EAST); panel.add(createLoginButtonPanel(), BorderLayout.SOUTH); return panel; } // create title panel private JPanel createTitlePanel() { JPanel panel = new JPanel(); panel.add(new JLabel("Login to my Blog")); return panel; } // create name item private JPanel createNamePanel() { JPanel panel = new JPanel(new GridLayout(2, 1, 5, 5)); panel.add(new JLabel("User name")); panel.add(new JLabel("Password")); return panel; } // create input panel private JPanel createInputPanel() { JPanel panel = new JPanel(new GridLayout(2, 1, 5, 5)); panel.add(new JTextField(20)); panel.add(new JPasswordField(20)); return panel; } // create status panel private JPanel createStatusPanel() { JPanel panel = new JPanel(new GridLayout(2, 1, 5, 5)); panel.add(new JLabel("Wrong")); panel.add(new JLabel("Wrong")); return panel; } // create login button panel private JPanel createLoginButtonPanel(){ JPanel panel = new JPanel(); panel.add(new JButton("Login")); return panel; } public static void main(String[] args) { new LoginFrame(); } }
The footnote in the code was relatively clear explanation of what we do, is just a little note in the Enter the password we use JPasswordField.
Refer to at: class BorderLayout, use BorderLayout
0 responses on [JavaSwing] BorderLayout