Java swing:在BoxLayout中对齐JPanel

Java swing:在BoxLayout中对齐JPanel,java,swing,Java,Swing,我有一个JPanel(面板),其布局设置为BoxLayout。我还有一个自定义类MapRow,它扩展了JPanel(在一个简单的FlowLayout中有一些组件),我希望以一种简单的、左对齐的、自顶向下的方式将MapRow的实例添加到面板。考虑以下方法: public void drawMappingsPanel(JPanel panel) { panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

我有一个JPanel(
面板
),其布局设置为BoxLayout。我还有一个自定义类
MapRow
,它扩展了JPanel(在一个简单的FlowLayout中有一些组件),我希望以一种简单的、左对齐的、自顶向下的方式将
MapRow
的实例添加到
面板
。考虑以下方法:

public void drawMappingsPanel(JPanel panel) {
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

        int s = /* aMethodCall() */;
        for (int i = 0; i < s; i++) {
            MapRow row = new MapRow();
            row.setAlignmentX(LEFT_ALIGNMENT);
            panel.add(row);
        }
    }
public void drawMappingsPanel(JPanel面板){
panel.setLayout(新的BoxLayout(panel,BoxLayout.PAGE_轴));
int s=/*aMethodCall()*/;
对于(int i=0;i
但是,当我运行代码时,所有
MapRow
面板都集中对齐,如下所示:

如何将
MapRow
面板向左对齐?
setAlignmentX(左对齐)
方法似乎不起作用


EDIT:我刚刚用dummy
JButton
s替换了
MapRow
的实例,它们左对齐得很好。因此,诸如
JButton
s之类的组件可以使用
setAlignmentX()
左对齐,但是JPanels不能左对齐。

您应该在MapRow中为FlowLayout使用左对齐。以下是一个小例子:

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestJPanels {

    protected void initUI() {
        final JFrame frame = new JFrame(TestJPanels.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        for (int i = 0; i < 5; i++) {
            JLabel label = new JLabel("Label-" + i);
            label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
            JPanel insidePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            insidePanel.add(label);
            insidePanel.setBorder(BorderFactory.createLineBorder(Color.RED));
            panel.add(insidePanel);
        }
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                new TestJPanels().initUI();
            }
        });
    }
}
导入java.awt.Color;
导入java.awt.FlowLayout;
导入javax.swing.BorderFactory;
导入javax.swing.BoxLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类TestJPanels{
受保护的void initUI(){
final JFrame=newjframe(TestJPanels.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel面板=新的JPanel();
panel.setLayout(新的BoxLayout(panel,BoxLayout.PAGE_轴));
对于(int i=0;i<5;i++){
JLabel标签=新的JLabel(“标签-”+i);
label.setboorder(BorderFactory.createLineBorder(Color.GREEN));
JPanel insidePanel=newjpanel(newflowlayout(FlowLayout.LEFT));
内部面板。添加(标签);
insidePanel.setBorder(BorderFactory.createLineBorder(Color.RED));
面板。添加(内部面板);
}
框架。添加(面板);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的TestJPanels().initUI();
}
});
}
}

您应该在MapRow中为FlowLayout使用左对齐。以下是一个小例子:

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestJPanels {

    protected void initUI() {
        final JFrame frame = new JFrame(TestJPanels.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        for (int i = 0; i < 5; i++) {
            JLabel label = new JLabel("Label-" + i);
            label.setBorder(BorderFactory.createLineBorder(Color.GREEN));
            JPanel insidePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            insidePanel.add(label);
            insidePanel.setBorder(BorderFactory.createLineBorder(Color.RED));
            panel.add(insidePanel);
        }
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                new TestJPanels().initUI();
            }
        });
    }
}
导入java.awt.Color;
导入java.awt.FlowLayout;
导入javax.swing.BorderFactory;
导入javax.swing.BoxLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类TestJPanels{
受保护的void initUI(){
final JFrame=newjframe(TestJPanels.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel面板=新的JPanel();
panel.setLayout(新的BoxLayout(panel,BoxLayout.PAGE_轴));
对于(int i=0;i<5;i++){
JLabel标签=新的JLabel(“标签-”+i);
label.setboorder(BorderFactory.createLineBorder(Color.GREEN));
JPanel insidePanel=newjpanel(newflowlayout(FlowLayout.LEFT));
内部面板。添加(标签);
insidePanel.setBorder(BorderFactory.createLineBorder(Color.RED));
面板。添加(内部面板);
}
框架。添加(面板);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的TestJPanels().initUI();
}
});
}
}

发布您的完整代码,以便有人可以帮助您使用不同的布局管理器,如@sunil请不要发布完整代码。。。。只是一个。@sunil-an是一个示例代码,它可以复制问题,但不需要复制问题的任何内容都已被删除。请在网站上阅读更多信息FlowLayout的对齐方式是什么?它在左边吗?默认情况下,FlowLayout是对齐的。另外,一个简单的技巧是在面板上设置一个边框(在你的例子中是MapRow),这样你就可以直观地看到面板的实际边界。发布你的完整代码,这样有人可以帮助你使用不同的布局管理器,比如@sunil,而不是完整的代码请。。。。只是一个。@sunil-an是一个示例代码,它可以复制问题,但不需要复制问题的任何内容都已被删除。请在网站上阅读更多信息FlowLayout的对齐方式是什么?它在左边吗?默认情况下,FlowLayout是对齐的。另外,一个简单的技巧是在面板上设置一个边框(在您的例子中是MapRow),以便可以可视化面板的实际边界。