Java BoxLayout问题

时间:2014-03-22 01:35:45

标签: java swing boxlayout

我在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();
    }
}

在我的机器上(OSX Mountain Lion with Java 7),产生类似于this的结果。我想将Basic面板一直向左(我已经尝试basic.setAlignmentX(JComponent.LEFT_ALIGNMENT);无效(这行代码没有效果)。此外,我已经检查了诸如

之类的问题


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

如何正确地左对齐desc和基本jpanel(以及你在代码中看到的任何其他错误)。
最后,(在一点切线上):指定最小值,最大值,和首选尺寸正确的方法来在BoxLayout中调整JPanel的大小(看起来非常不优雅)?

1 个答案:

答案 0 :(得分:0)

使用容器将添加到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);

修改

我刚刚审核了您的链接,这已在 BOTH 中提及过了。那怎么对你不起作用呢?我想这个问题将/应该作为副本关闭。