如何覆盖JPanel的默认布局管理器?

时间:2013-02-22 15:16:36

标签: java swing jpanel layout-manager

如何覆盖JPanel后代的默认布局管理器?我有自己的课程,希望null成为默认课程。

3 个答案:

答案 0 :(得分:5)

使用JPanel构造函数,它们接受LayoutManager

import java.awt.LayoutManager;

import javax.swing.JPanel;

public class MyPanel extends JPanel {
    public MyPanel() {
        this(true);
    }

    public MyPanel(boolean isDoubleBuffered) {
        super(null, isDoubleBuffered);
    }

    public MyPanel(LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
    }

    public MyPanel(LayoutManager layout) {
        this(layout, true);
    }
}

答案 1 :(得分:2)

您可以在子类中添加初始化程序块:

public class MyPanel extends JPanel {

    {
         setLayout(null);
    }

    // ... class implementation
}

答案 2 :(得分:1)

JPanel panel = new JPanel();
panel.setLayout(new MyLayout());

contentPane.setLayout(null);