2013-07-09 3 views
3

Оба x_axis и line_axis располагают компоненты слева направо. Тогда в чем разница между ними?В чем разница между x_axis и line_axis boxlayout в java?

Этот вопрос задан менеджером макетов Java Swing boxlayout.

+0

Никогда не использовался 'BoxLayout', хотя AFAIU, бывший (LINE_AXIS), может быть ch в зависимости от «Ориентация компонента», если вы измените ориентацию компонента на правое налево, это тоже изменится, хотя последнее всегда будет добавлять компоненты от «Слева направо», не имеет значения, что ... –

ответ

5

Из официального documentation:

X_Axis - компоненты располагаются горизонтально слева направо.

LINE_AXIS - компоненты раскладывают путь слово выложенные в линии, основанный на собственности ComponentOrientation контейнера. Если компонент ComponentOrientation является горизонтальным, то компоненты равны , расположенным горизонтально, в противном случае они располагаются вертикально. Для горизонтальных ориентаций , если компонент ComponentOrientation составляет слева направо, тогда компоненты располагаются слева направо, в противном случае выложены справа налево. Для вертикальных ориентаций компоненты всегда располагаются сверху донизу.

2

Я надеюсь, что следующий пример кода, может быть в состоянии обеспечить более глубокое представление о том, что Java Docs должен сказать о BoxLayout.LINE_AXIS:


LINE_AXIS - Components are laid out the way words are laid out in a line, 
based on the container's ComponentOrientation property. If the container's 
ComponentOrientation is horizontal then components are laid out horizontally, 
otherwise they are laid out vertically. For horizontal orientations, if the 
container's ComponentOrientation is left to right then components are laid out 
left to right, otherwise they are laid out right to left. For vertical 
orientations components are always laid out from top to bottom. 

X_AXIS - Components are laid out horizontally from left to right. 

ли смотреть последние две строки, как 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); 
    } 
} 

ВЫХОД:

BoxLayoutExample

Смежные вопросы