在BorderLayout中向NORTH组件添加垂直间距

时间:2014-01-16 16:18:02

标签: java swing layout-manager border-layout null-layout-manager

我有一个BorderLayout的JFrame。我在JPanel的NORTH侧添加了JFrame。在这个面板中,我想以绝对定位为其添加组件。在JFrame的中心一侧,我添加了另一个JPanel,这应该占用很大的空间。但是,当我运行应用程序时,由于中心JPanel占据了JPanel的所有空间,我在北JFrame中看不到任何内容!如何为North JPanel提供垂直空间?

我真的需要使用北JPanel的绝对定位。

这是我的代码:

public class AAAA extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AAAA frame = new AAAA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AAAA() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1136, 520);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);
        panel.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(0, 0, 117, 29);
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
    }

}

2 个答案:

答案 0 :(得分:4)

更新1

我看到你已经选择了答案(我认为过早)。这是我想要实现的相信的第一次迭代。无需设置边界或首选尺寸..

Laid Out 1

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class AAAA extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    AAAA frame = new AAAA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AAAA() {
        super("Laid Out");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // OMG! If you can make a GUI break at 1336 px wide, it should be 
        // possible to make it break at ..much smaller!
        //setBounds(100, 100, 1136, 520);
        setBackground(Color.YELLOW);
        contentPane = new JPanel();
        contentPane.setBackground(Color.BLUE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        // make it a FlowLayout as FlowLayout.LEADING with no spacing to 
        // make the button snug up against the top left
        JPanel panel = new JPanel(
                new FlowLayout(FlowLayout.LEADING, 0, 0));
        panel.setBackground(Color.GREEN);
        contentPane.add(panel, BorderLayout.NORTH);
        //panel.setPreferredSize(new Dimension(1024,400));

        JButton btnNewButton = new JButton("New button");
        // we change the margin to make the button bigger than natural size.
        btnNewButton.setMargin(new Insets(6, 22, 6, 22));
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        // create a solic color image to both pad the GUI and 
        // provide visual indication of where it is.
        BufferedImage bi = new BufferedImage(
                400,200,BufferedImage.TYPE_INT_RGB);
        JLabel padder = new JLabel(new ImageIcon(bi));
        panel_1.add(padder);
        panel_1.setBackground(Color.RED);
        contentPane.add(panel_1, BorderLayout.CENTER);
        pack();
        setMinimumSize(getSize());
    }
}

答案 1 :(得分:3)

  

我真的需要使用北JPanel的绝对定位。

为什么呢?如果我们知道您认为需要这样做的原因,我们可能会提供更好的方法。

不要使用null布局。 Swing旨在与布局管理器一起使用。

BorderLayout将尊重添加到NORTH的组件的首选高度。首选高度为零,因此不显示任何内容。

注意:我不是建议你设置面板的首选高度,这是布局管理器的工作,这就是你应该总是使用布局管理器的原因。布局管理器所做的不仅仅是设置组件的大小/位置。