如何覆盖JPanel
后代的默认布局管理器?我有自己的课程,希望null
成为默认课程。
答案 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);