This question already has answers here:
How can I align all elements to the left in JPanel?
                                
                                    (3个答案)
                                
                        
                                6年前关闭。
            
                    
在Java Swing应用程序中对首选项面板进行编程时,遇到了一个似乎无法解决的问题。

SSCCE:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;

/**
 * demonstrates the issue at hand
 */
public class Probs extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = -2320457631507860940L;
    JCheckBox one = new JCheckBox("Checkbox One");
    JCheckBox two = new JCheckBox("Checkbox Two");
    JCheckBox three = new JCheckBox("Checkbox Three");
    JCheckBox four = new JCheckBox("Checkbox Four");

    final static int BORDER = 10;
    final static int WIDTH = 440;
    final static int DESC_HEIGHT = 30;

    public Probs () {
        JTabbedPane options = new JTabbedPane();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //set up the main panel
        JPanel pan = new JPanel();
        pan.setBorder(BorderFactory.createEmptyBorder(Probs.BORDER, Probs.BORDER, Probs.BORDER, Probs.BORDER));
        pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));

        //construct the descriptor subpanel
        JPanel desc = new JPanel();
        desc.setLayout(new BorderLayout());
        desc.add(new CustomLab("Options Title Goes Here"));
        JSeparator sep1 = new JSeparator();
        desc.add(sep1, BorderLayout.SOUTH);
        desc.setPreferredSize(new Dimension(Probs.WIDTH, Probs.DESC_HEIGHT));
        desc.setMinimumSize(new Dimension(Probs.WIDTH, Probs.DESC_HEIGHT));
        desc.setMaximumSize(new Dimension(Probs.WIDTH, Probs.DESC_HEIGHT));
        pan.add(desc);

        //basic options
        JPanel basic = new JPanel();
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
        basic.setBorder(BorderFactory.createTitledBorder("Basic"));
        basic.add(one);
        basic.add(two);
        basic.add(three);
        basic.add(four);
        basic.setPreferredSize(new Dimension(Probs.WIDTH, basic.getPreferredSize().height));
        basic.setMinimumSize(new Dimension(Probs.WIDTH, basic.getPreferredSize().height));
        basic.setMaximumSize(new Dimension(Probs.WIDTH, basic.getPreferredSize().height));
        pan.add(basic);

        options.addTab("Panel One", pan);
        add(options);
        setSize(500, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Probs();
    }
}


在我的机器(带有Java 7的OSX Mountain Lion)上,产生的结果类似于this。我想将“基本”面板一直放到最左边(而且我已经尝试basic.setAlignmentX(JComponent.LEFT_ALIGNMENT);无济于事(那行代码无效)。此外,我已经检查了诸如


this one
and this one


也无济于事。
有趣的是,如果删除了desc面板(因此将pan.add(desc);注释掉了),则基本面板将正确对齐,如图所示。

如何正确地将desc和基本jpanel左对齐(以及在代码中看到的其他错误)最后,(在一点切线处):指定最小,最大和首选大小是正确的方法在BoxLayout中设置JPanel的大小(看起来非常不雅)?

最佳答案

使用容器将添加到BoxLayout的两个组件的路线x设置为Component.LEFT_ALIGNMENT:

    JPanel desc = new JPanel();

    // ....

    desc.setAlignmentX(Component.LEFT_ALIGNMENT);

    // ......

    pan.add(desc);

    JPanel basic = new JPanel();

    // ..........

    basic.setAlignmentX(Component.LEFT_ALIGNMENT);
    pan.add(basic);




编辑

我刚刚检查了您的链接,而这两个链接中均已提及。那么,这对您有什么用呢?我想这个问题将/应该被重复。

09-11 21:00
查看更多