[JavaSwing] The difference between LEFT and LEADING, RIGHT và TRAILING
As you already know how to align the objects in a Layout, namely at all FlowLayout mentioned it. When you use will not distinguish the difference between LEFT and LEADING, RIGHT và TRAILING. In this article we will learn that.
Even our objects are arranged in Container (ComponentOrientation) là left-to-right/top-to-bottom, with FlowLayout it is left-to-right. We will consider 2 The following example:
Khi ComponentOrientation là left-to-right
The following example will create 1 JPanel (main panel) contain 5 JPanel (item panel), Each item of this panel will be set FLowLayout with a different alignment and contains a JLabel (with text and icons) to demonstrate the alignment of their.
package Align; import java.awt.ComponentOrientation; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MyOrientation extends JFrame { public MyOrientation() { createJFrame(); } private void createJFrame() { setTitle("Orientation left to right"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 500); // add content to JFrame add(createMainPanel()); setLocationRelativeTo(null); setVisible(true); } private JPanel createMainPanel() { // create main panel JPanel panel = new JPanel(new GridLayout(5, 1, 5, 5)); // add panels item with align panel.add(cereateItemPanel(FlowLayout.LEFT, "LEFT")); panel.add(cereateItemPanel(FlowLayout.CENTER, "CENTER")); panel.add(cereateItemPanel(FlowLayout.RIGHT, "RIGHT")); panel.add(cereateItemPanel(FlowLayout.LEADING, "LEADING")); panel.add(cereateItemPanel(FlowLayout.TRAILING, "TRAILING")); return panel; } // create JPanel with align and JLabel title private JPanel cereateItemPanel(int align, String title) { // create JPanel JPanel panel = new JPanel(createFlowLayout(align)); // set Component Orientation panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); // add JLabel panel.add(createJLabel(title)); return panel; } // create FlowLayout with align private FlowLayout createFlowLayout(int algin) { FlowLayout layout = new FlowLayout(); layout.setAlignment(algin); return layout; } // create JLabel with title and icon private JLabel createJLabel(String title) { JLabel lb = new JLabel(title); // get icon for JLabel Icon icon = new ImageIcon(getClass().getResource("7826.png")); lb.setIcon(icon); // set location for text label lb.setHorizontalTextPosition(JLabel.CENTER); lb.setVerticalTextPosition(JLabel.BOTTOM); return lb; } public static void main(String[] args) { new MyOrientation(); } }
I get the first image. You can see we have not received an explanation for the problem posed for alignment under exactly the same LEFT LEADING, RIGHT nothing more than trailing. But this is only the first step alone, please change the layout of the JPanel using methods setComponentOrientation I will get into RIGHT_TO_LEFT as second and answers are revealed.
I can understand the LEFT and RIGHT way in accordance with the left margin alignment, absolute right, LEADING and TRAILING is also placing the left margin, relative to rely on a set of containers of its margin. When containers of it with the arrangement (Orientation) is the LEADING LEFT_TO_RIGHT is left, Trailing is right, Orientation is also when the LEADING RIGHT_TO_LEFT is right, Trailing the left.
0 responses on [JavaSwing] The difference between LEFT and LEADING, RIGHT và TRAILING