java中的boxlayout的x_axis和line_axis有什么区别?

问题描述 投票:3回答:3

x_axis和line_axis都是从左到右排列组件。那么它们之间的区别是什么?

这个问题来自Java Swing boxlayout布局管理器。

java swing jpanel boxlayout
3个回答
5
投票

从官方documentation

X_AXIS - 组件从左到右水平布局。

LINE_AXIS - 基于容器的ComponentOrientation属性,组件按照行排列的方式布局。如果容器的ComponentOrientation是水平的,那么组件将水平布置,否则它们将垂直布局。对于水平方向,如果容器的ComponentOrientation从左到右,则组件从左到右排列,否则它们从右到左排列。对于垂直方向,组件始终从顶部到底部布置。


3
投票

我希望以下代码示例能够提供有关Java Docs在BoxLayout.LINE_AXIS上所说的内容的更多信息:


LINE_AXIS - 基于容器的ComponentOrientation属性,组件按照行排列的方式布局。如果容器的ComponentOrientation是水平的,那么组件将水平布置,否则它们将垂直布局。对于水平方向,如果容器的ComponentOrientation从左到右,则组件从左到右排列,否则它们从右到左排列。对于垂直方向,组件始终从顶部到底部布置。


X_AXIS - 组件从左到右水平布局。


请注意最后两行,Buttons如何被添加到第二个最后一个JPanel,从右到右,从__LEFT到RIGHT_到最后一个JPanel

import java.awt.*;
import java.util.Locale;
import javax.swing.*;

public class BoxLayoutExample
{
    private JPanel topPanel;
    private JPanel middlePanel;
    private JPanel bottomPanel;
    private JPanel addedBottomPanel;
    private JButton[] button;

    public BoxLayoutExample()
    {
        button = new JButton[12];
    }

    private void displayGUI()
    {
        JFrame frame = new JFrame("Box Layout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(4, 1, 5, 5));

        topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(
                            topPanel, BoxLayout.X_AXIS));
        for (int i = 0; i < 3; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            topPanel.add(button[i]);
        }

        middlePanel = new JPanel();
        middlePanel.setLayout(new BoxLayout(
                            middlePanel, BoxLayout.LINE_AXIS));
        for (int i = 3; i < 6; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            middlePanel.add(button[i]);
        }

        bottomPanel = new JPanel();     
        bottomPanel.setComponentOrientation(
                        ComponentOrientation.RIGHT_TO_LEFT);        
        bottomPanel.setLayout(new BoxLayout(
                            bottomPanel, BoxLayout.LINE_AXIS));
        for (int i = 6; i < 9; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            bottomPanel.add(button[i]);
        }

        addedBottomPanel = new JPanel();        
        addedBottomPanel.setComponentOrientation(
                        ComponentOrientation.RIGHT_TO_LEFT);        
        addedBottomPanel.setLayout(new BoxLayout(
                            addedBottomPanel, BoxLayout.X_AXIS));
        for (int i = 9; i < 12; i++)
        {
            button[i] = new JButton(Integer.toString(i));
            addedBottomPanel.add(button[i]);
        }

        contentPane.add(topPanel);
        contentPane.add(middlePanel);
        contentPane.add(bottomPanel);
        contentPane.add(addedBottomPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                new BoxLayoutExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

输出:


1
投票

X_AXIS始终是水平的。 LINE_AXIS可以基于容器的ComponentOrientation属性。

资源:

http://download.java.net/jdk8/docs/api/javax/swing/BoxLayout.html

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.