java layout_自己设计的JAVA布局管理器-LocationLayout

import java.awt.*;

import javax.swing.*;

/**

* 控件位置布局管理器

* @author caiqq

*

*/

public class LocationLayout {

/** X坐标的位置 */

private int startX=25;

/** Y坐标的位置 */

private int startY=25;

/** 上一行控件的高度 */

private int lastRowHeight=50;

/** 上一列控件的宽度 */

private int lastColumnWidth=50;

/** 上一列的编号 */

private int lastRowNumber=0;

/** 左间距 */

static public int leftDistance=25;

/** 上间距 */

static public int upDistance=25;

/** 控件的宽 */

private int objectWidth=25;

/** 控件的高 */

private int objectHeight=25;

/**

* 默认构造函数

*/

public LocationLayout()

{

}

/**

* 控件居中

* @param component

* 需要居中的控件

*/

static public void setCenter(Component component)

{

int windowWidth = component.getWidth(); //获得窗口宽

int windowHeight = component.getHeight(); //获得窗口高

Toolkit kit = Toolkit.getDefaultToolkit();         //定义工具包

Dimension screenSize = kit.getScreenSize();         //获取屏幕的尺寸

int screenWidth = screenSize.width;         //获取屏幕的宽

int screenHeight = screenSize.height;         //获取屏幕的高

component.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);//设置窗口居中显示

}

/**

* 设置控件左间距

* @param left

* 左间距

*/

public void setLeftDistance(int left)

{

leftDistance=left;

}

/**

* 设置控件上间距

* @param up

* 上间距

*/

public void setUpDistance(int up)

{

upDistance=up;

}

/**

* 设置控件的大小

* @param width

* 控件的宽

* @param height

* 控件的高

*/

public void setObjectSize(int width,int height)

{

objectWidth=width;

objectHeight=height;

}

/**

* 根据控件的行号,列号,左边距,上边距,控件的宽,高实现控件的布局

* @param object

* 控件--支持各种控件

* @param rowLine

* 控件所在的行号

* @param columnLine

* 控件所在的列号

* @param objectWidth

* 控件的宽

* @param objectHeight

* 控件的高

*/

public void setPosition(Object object,int rowLine, int columnLine,int objectWidth,int objectHeight)

{

//调整Y坐标位置;

if(rowLine==0)

{

startY=upDistance;

lastRowHeight=0;

}

else if((lastRowNumber+1)==rowLine)//如果行号增加了,距离也随之增加

{

startY+=(upDistance+lastRowHeight);

}

//调整X坐标位置;

if(columnLine==0)

{

startX=leftDistance;

lastColumnWidth=0;

}

else

{

startX+=leftDistance+lastColumnWidth;

}

lastColumnWidth=objectWidth;

lastRowHeight=objectHeight;

lastRowNumber=rowLine;

if(object==null)

return;

//JButton

if(object.toString().startsWith("javax.swing.JButton"))

{

JButton JButton=(JButton)object ;

JButton.setLocation(new Point(startX, startY));

JButton.setSize(new Dimension(objectWidth, objectHeight));

}

//JLabel

else if(object.toString().startsWith("javax.swing.JLabel"))

{

JLabel JLabel=(JLabel)object ;

JLabel.setLocation(new Point(startX, startY));

JLabel.setSize(new Dimension(objectWidth, objectHeight));

}

//JTextField

else if(object.toString().startsWith("javax.swing.JTextField"))

{

JTextField JTextField=(JTextField)object ;

JTextField.setLocation(new Point(startX, startY));

JTextField.setSize(new Dimension(objectWidth, objectHeight));

}

//JPasswordField

else if(object.toString().startsWith("javax.swing.JPasswordField"))

{

JPasswordField JPasswordField=(JPasswordField)object ;

JPasswordField.setLocation(new Point(startX, startY));

JPasswordField.setSize(new Dimension(objectWidth, objectHeight));

}

//JComboBox

else if(object.toString().startsWith("javax.swing.JComboBox"))

{

JComboBox JComboBox=(JComboBox)object ;

JComboBox.setLocation(new Point(startX, startY));

JComboBox.setSize(new Dimension(objectWidth, objectHeight));

}

//JList

else if(object.toString().startsWith("javax.swing.JList"))

{

JList JList=(JList)object ;

JList.setLocation(new Point(startX, startY));

JList.setSize(new Dimension(objectWidth, objectHeight));

}

//JRadioButton

else if(object.toString().startsWith("javax.swing.JRadioButton"))

{

JRadioButton JRadioButton=(JRadioButton)object ;

JRadioButton.setLocation(new Point(startX, startY));

JRadioButton.setSize(new Dimension(objectWidth, objectHeight));

}

//JCheckBox

else if(object.toString().startsWith("javax.swing.JCheckBox"))

{

JCheckBox JCheckBox=(JCheckBox)object ;

JCheckBox.setLocation(new Point(startX, startY));

JCheckBox.setSize(new Dimension(objectWidth, objectHeight));

}

//JTree

else if(object.toString().startsWith("javax.swing.JTree"))

{

JTree JTree=(JTree)object ;

JTree.setLocation(new Point(startX, startY));

JTree.setSize(new Dimension(objectWidth, objectHeight));

}

//JTextArea

else if(object.toString().startsWith("javax.swing.JTextArea"))

{

JTextArea JTextArea=(JTextArea)object ;

JTextArea.setLocation(new Point(startX, startY));

JTextArea.setSize(new Dimension(objectWidth, objectHeight));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值