交错的顶部对齐JPanels

时间:2013-06-24 23:05:56

标签: java jpanel vertical-alignment

我正在尝试使用FlowLayout让我的面板垂直对齐。我希望右下方对齐右上方面板的底部,而不是该行的底部。

这就是我所做的:

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



    public class BigPanel extends JPanel {

    @Override
    public Component.BaselineResizeBehavior getBaselineResizeBehavior() {
        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
    }

    @Override
    public int getBaseline(int width, int height) {
       return 0;
    }

public BigPanel() {
    FlowLayout layout = new FlowLayout();
    layout.setAlignOnBaseline(true);
    this.setLayout(layout);
    this.add(new Panel1()); // size: 340x, 160y
    this.add(new Panel2()); // size: 340x, 120y
    this.add(new Panel3()); // size: 340x, 160y
    this.add(new Panel4()); // size: 340x, 300y
}

}

如何简单地将面板和组件锚定到一组坐标?我上周已多次遇到这个问题,并为我的jlabels等使用荒谬的解决方法。

现在的样子:

enter image description here

1 个答案:

答案 0 :(得分:0)

可能有几种方法可以实现这一目标,但最简单的方法是使用复合布局。

使用两个面板作为列,然后将它们放在主容器上。然后,您可以将其他组件放入其中。

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class CompundLayout {

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

    public CompundLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JPanel left = new JPanel();
            JPanel right = new JPanel();

            setLayout(new GridLayout(0, 2, 2, 2));

            left.add(createPane(Color.RED));
            left.add(createPane(Color.GREEN));
            right.add(createPane(Color.BLUE));
            right.add(createPane(Color.MAGENTA));

            add(left);
            add(right);

        }

        protected JPanel createPane(Color color) {

            SimplePane pane = new SimplePane();
            pane.setBorder(new LineBorder(color));

            return pane;

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }
    }

    public class SimplePane extends JPanel {

        private int width;
        private int height;

        public SimplePane() {

            width = 100;
            height = 50 + (int) Math.round(Math.random() * 100);

        }

        @Override
        public Dimension getPreferredSize() {

            return new Dimension(width, height);

        }

    }

}