[JavaSwing] LEFT和领导的区别, RIGHT VA TRAILING
正如你已经知道如何对齐对象布局, 即在所有 的FlowLayout 提到它. 当您使用不区分 LEFT和领导的区别, RIGHT VA TRAILING. 在这篇文章中,我们将学习.
甚至我们的对象被布置在容器 (的ComponentOrientation) Là左到右/上到下, 具有FlowLayout是左到右. 我们会考虑 2 下面的例子:
KHI的ComponentOrientationLà左到右
下面的例子将创建 1 的JPanel (主面板) 含有 5 的JPanel (项目小组), 这个面板的每个项目将被设置 的FlowLayout 用不同的取向,并包含一个JLabel (用文本和图标) 展示的对准其.
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();
}
}

我得到的第一个图像. 你可以看到,我们还没有收到解释下完全相同的左边导致合影对齐问题, RIGHT无非尾随. 但是,这只是单纯的第一步, 请更改使用方法JPanel的布局 setComponentOrientation 我将进入RIGHT_TO_LEFT作为第二和答案透露.
我能理解左,右的方式按照左边距对齐, 绝对的权利, 开头和结尾也把左边界, 相对于依靠一组其保证金集装箱. 当它与装置的容器 (方向) 是全球领先的LEFT_TO_RIGHT留下, 结尾是正确的, 定位也是在领先的RIGHT_TO_LEFT是正确的, 尾随左.



0 在回应 [JavaSwing] LEFT和领导的区别, RIGHT VA TRAILING