Java Swing BoxLayout ignoring AlignmentX

烂漫一生 提交于 2019-12-08 21:25:57

问题


In the code below, by calling setAlignmentX with Component.LEFT_ALIGNMENT I expected to get a left aligned label over a centered slider. For some reason the label is also centered, seemingly regardless of what value is passed to setAlignmentX.

What value must I pass to setAlignmentX to get it left aligned?

package myjava;

import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class LayoutTest {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("BoxLayoutDemo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // create left aligned label over centered column
                Container contentPane = frame.getContentPane();
                contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
                JLabel label = new JLabel("test");
                label.setAlignmentX(Component.LEFT_ALIGNMENT);
                contentPane.add(label);
                contentPane.add(new JSlider());

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

回答1:


Basically, you can't have different alignments in BoxLayout, from How To Use BoxLayout

In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment.

Edit

Typically, it's not documented which default alignment a component type has (JSlider is centered by default, me incorrectly thought that a JLabel were centered while it is left-aligned ;-) One option is to keep a list somewhere (dooooh...), another is to simply force them all to the same alignment on adding.

Or use a third-party layoutManager, which doesn't have this rather unintuitve (for me) mix-in of layout and alignment.




回答2:


BoxLayout have strange behavior. Try to use GridBagLayout instead:

https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

public class Aligment {
  public static void main(String[] args) {

    final JPanel root = new JPanel(new GridBagLayout());
    root.setPreferredSize(new Dimension(500, 400));

    root.add(new JLabel("LEFT"), new GridBagConstraints() {{
        gridx = 0; 
        gridy = 0; 
        anchor = PAGE_START; 
    }});
    root.add(new JLabel("CENTER"), new GridBagConstraints() {{
        gridx = 1;
        gridy = 1; 
        anchor = CENTER; 
        weightx = 1.0; // fill Width
    }});
    root.add(new JLabel("RIGHT"),  new GridBagConstraints() {{ 
        gridx = 2; 
        gridy = 2; 
        anchor = LINE_END; 
    }});
    // hack: Push all rows to Top
    root.add(Box.createVerticalGlue(), new GridBagConstraints() {{
        gridx = 0; 
        gridy = 3; 
        weighty = 1.0; // fill Height
    }});

    new JFrame() {
      {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setContentPane(root);
        pack();
        setLocationRelativeTo(null);;
      }
    }.setVisible(true);
  }
}


来源:https://stackoverflow.com/questions/9228678/java-swing-boxlayout-ignoring-alignmentx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!