布局管理器使用代码,
Class
{
super("Teradata DataPump 1.0"); //设置窗口标题
this.setResizable(false); //禁止改变窗口的大小
//实例化一个布局的类
LoginFrameLayout customLayout = new LoginFrameLayout();
getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
getContentPane().setLayout(customLayout); //设置容器
Icon splash=new ImageIcon("image/logo.jpg"); //加入图片、按钮、文本框等
label_1 = new JLabel(splash);
getContentPane().add(label_1);
pack();
setLocationRelativeTo(getOwner()); //将页面显示在屏幕最中间
/*
*
* 添加一个类,用于布局
*/
class LoginFrameLayout implements LayoutManager {
public LoginFrameLayout() {
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 423 + insets.left + insets.right;
dim.height = 469 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+16,insets.top+16,392,264);} //picture
c = parent.getComponent(1); //设置空间的位置大小
if (c.isVisible()) {c.setBounds(insets.left+16,insets.top+16,392,264);} //logo
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+120,insets.top+400,88,32);} //button logout
c = parent.getComponent(3);
}
}
}