Java BoxLayout 对齐问题

标签 java swing alignment boxlayout

谁能帮帮我。为什么标签“当前”没有在面板/框架中左对齐?

   public static void main(String[] args) {



            JFrame TFrame = new JFrame("Test DisplayLayout");
            TFrame.setResizable(true);
            TFrame.setSize(new Dimension(900, 840));
            TFrame.setLocationRelativeTo(null);
            TFrame.setTitle("DisplayLayout");   
            TFrame.setVisible(true); 


            JPanel P  = DisplayLayout2();

            P.setVisible(true);
            P.setOpaque(true);
            P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));

            TFrame.add(P);

            TFrame.revalidate();
            TFrame.repaint();


        }

       public static JPanel DisplayLayout2() {

            JPanel Panel=new JPanel();
            Panel.setVisible(true);
            Panel.setOpaque(true);
            Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
            Panel.setAlignmentX(Component.LEFT_ALIGNMENT);

            JLabel lab = new JLabel("Current");
            lab.setHorizontalAlignment(SwingConstants.LEFT);
            lab.setForeground(Color.WHITE);
            lab.setBackground(Color.PINK);
            lab.setOpaque(true);    
            Panel.add(lab,Component.LEFT_ALIGNMENT);

            JPanel posPanel = new JPanel();
            JScrollPane scrollPane = new JScrollPane(posPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setPreferredSize(new Dimension(290, 200));
            scrollPane.setOpaque(true);        
            posPanel.setBackground(Color.YELLOW);
            posPanel.setPreferredSize(new Dimension(290, 200));
            posPanel.setLayout(new BoxLayout(posPanel, BoxLayout.Y_AXIS));          
            posPanel.setAlignmentX(Component.LEFT_ALIGNMENT);   

            Panel.add(scrollPane);

            return Panel;

        }

enter image description here

最佳答案

这是 BoxLayout 的怪癖之一(好吧,对我来说怪怪的,但它是布局的已记录的预期行为),我完全忘记了它为什么这样做,但我确实这样做至少知道一种解决方法:将 JLabel 放入使用 FlowLayout.LEFT(或 LEADING)的 JPanel 中,并将其添加到使用 BoxLayout 的容器中:

JPanel labPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
labPanel.add(lab);
panel.add(labPanel, Component.LEFT_ALIGNMENT);

请注意,我认为这与 JLabel 不想扩展而封装它的 JPanel 想要扩展有关,但不要引用我的观点。

<小时/>

编辑
来自 BoxLayout Tutorial :

For a top-to-bottom box layout, the preferred width of the container is that of the maximum preferred width of the children. If the container is forced to be wider than that, BoxLayout attempts to size the width of each component to that of the container's width (minus insets). If the maximum size of a component is smaller than the width of the container, then X alignment comes into play.

您可以通过将 JLabel 的 和 JScrollPane 的 x 对齐设置为 Component.LEFT_ALIGNMENT 来解决此问题。您当前的代码忘记设置 JScrollPane 的 x 对齐方式,这就是您的问题所在:

scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
<小时/>

例如:

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

public class Foo2 {

   private static void createAndShowGui() {
      JLabel topLabel = new JLabel("Top Label", SwingConstants.LEFT);
      topLabel.setOpaque(true);
      topLabel.setBackground(Color.pink);

      JScrollPane scrollpane = new JScrollPane(
            Box.createRigidArea(new Dimension(400, 400)),
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

      JPanel mainPanel = new JPanel();
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

      topLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
      mainPanel.add(topLabel);
      scrollpane.setAlignmentX(Component.LEFT_ALIGNMENT);
      mainPanel.add(scrollpane);

      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
<小时/>

这会导致:

enter image description here

关于Java BoxLayout 对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19891678/

相关文章:

jquery - 如何在包裹在 li 元素上的 span 元素中放置相同的宽度

java - 接受字符串文本并返回已识别整数之和的方法

java - 使用 Android NDK 将 C++ 对象传递给 Java 的最有效方法是什么

java - 如何在Swing中绘制垂直线

java - 限制 PLAIN_MESSAGE JOptionPane 中输入文本的大小

html - 两个 CSS 网格在同一个父容器中不对齐

javascript - 使用 Ionic 框架将 ionic 按钮居中

java - Glassfish v3 "unable to find valid certification path to requested target"

Java 字符串到日期的转换

java - Google Apps 类到 JList