示例:
// 创建编辑控件组
LabelComp label_comp = new LabelComp(content, 230, 10, 15);
ClientServer server1 = new ClientServer(); // 特定的数据结构对象
server1.AddTo(label_comp); // 显示,在控件组中显示server1的信息
server1.GetData(label_comp); // 修改,从控件组中获取修改后的数据到server1
package com.scimence.component;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/** LabelComp.java:用于快速生成和管理JLabel和componet组合 ----- 2016-4-1 下午3:16:29 wangzhongyuan */
public class LabelComp
{
public ArrayList<String> Names = new ArrayList<String>(); // 标签和编辑框组的名称
public HashMap<String, JLabel> Labels = new HashMap<String, JLabel>(); // 标签数组
public HashMap<String, JTextField> TextFields = new HashMap<String, JTextField>(); // 编辑框数组
public HashMap<String, JComboBox> JComboBoxs = new HashMap<String, JComboBox>(); // 下拉列表框数组
public int LabelWidth = 50, LabelHeight = 15; // Label的尺寸
public int Width = 80, Height = 22; // TextField、JComboBoxs尺寸
public int Left = 0, Top = 0, GapH = 7, GapV = 0; // LabelTextField控件组的坐标,水平、垂直间距
public int maxRows = 100; // 控件组的最大行数
public JPanel parent;
// public LabelTextField(){};
public LabelComp(JPanel parent)
{
this.parent = parent;
};
public LabelComp(JPanel parent, int Left, int Top)
{
this.parent = parent;
this.Left = Left;
this.Top = Top;
};
public LabelComp(JPanel parent, int Left, int Top, int maxRows)
{
this(parent, Left, Top);
this.maxRows = maxRows;
};
/** 向parent中添加标签输入框组 */
public void AddIteam(String name)
{
AddIteam(name, name, "", "");
}
/** 向parent中添加标签输入框组 */
public void AddIteam(String name, String LabelText, Object filedText)
{
AddIteam(name, LabelText, filedText, null);
}
/** 向parent中添加标签输入框组 */
public void AddIteam(String name, String LabelText, Object filedText, String tipInfo)
{
if (!Names.contains(name))
{
int size = Names.size(); // 控件组的元素个数
int n1 = size / maxRows, n2 = size % maxRows; // 根据添加控件的顺序,设定行列位置
int left = Left + n1 * (LabelWidth + Width + GapH); // 生成Left坐标
int top = Top + n2 * (Height + GapV); // Top坐标
// 创建标签
JLabel label = new JLabel();
label.setText(LabelText);
label.setBounds(left, top, LabelWidth, LabelHeight);
parent.add(label);
// 创建输入框
JTextField field = new JTextField();
field.setText(Str(filedText));
if (tipInfo != null && !tipInfo.equals("")) field.setToolTipText(tipInfo);
field.setBounds(left + LabelWidth + 3, top, Width, Height);
parent.add(field);
parent.repaint(); // 刷新显示
// 添加标签输入框信息到数组
Names.add(name);
Labels.put(name, label);
TextFields.put(name, field);
// Labels.add(label);
// TextFields.add(field);
}
else
{
Label(name).setText(LabelText);
TextField(name).setText(filedText.toString());
}
}
/** 向parent中添加标签下拉列表框组 */
public void AddComboBox(String name, String Text, String[] values, String select, String tipInfo)
{
if (!Names.contains(name))
{
int size = Names.size(); // 控件组的元素个数
int n1 = size / maxRows, n2 = size % maxRows; // 根据添加控件的顺序,设定行列位置
int left = Left + n1 * (LabelWidth + Width + GapH); // 生成Left坐标
int top = Top + n2 * (Height + GapV); // Top坐标
// 创建标签
JLabel label = new JLabel();
label.setText(Text);
label.setBounds(left, top, LabelWidth, LabelHeight);
parent.add(label);
// 创建输入框
JComboBox combo = new JComboBox(values);
if (tipInfo != null && !tipInfo.equals("")) combo.setToolTipText(tipInfo);
combo.setBounds(left + LabelWidth + 3, top, Width, Height);
combo.setSelectedItem(select);
parent.add(combo);
parent.repaint(); // 刷新显示
// 添加标签输入框信息到数组
Names.add(name);
Labels.put(name, label);
JComboBoxs.put(name, combo);
}
else
{
Label(name).setText(Text);
JComboBox combo = ComboBox(name);
combo.removeAllItems();
for (String value : values)
combo.addItem(value);
combo.setSelectedItem(select);
}
}
public String Str(Object obj)
{
try
{
return obj.toString();
}
catch (Exception e)
{
return "";
}
}
// /** 从控件组中移除指定名称的控件组 */
// public void Remove(String name)
// {
// if (Names.contains(name))
// {
// int index = Names.indexOf(name);
// JLabel label = Labels.get(index);
// JTextField field = TextFields.get(index);
//
// // 从当前控件组中移除
// Labels.remove(index);
// TextFields.remove(index);
// Names.remove(index);
//
// // 从parent中移除
// parent.remove(label);
// parent.remove(field);
//
// }
// }
//
// /** 移除控件组中所有元素 */
// public void Clear()
// {
// for (String name : Names)
// Remove(name);
// }
/** 获取name名称对应的Label */
public JLabel Label(String name)
{
if (Names.contains(name)) { return Labels.get(name); }
return null;
}
/** 获取name名称对应的TextField */
public JTextField TextField(String name)
{
if (Names.contains(name)) { return TextFields.get(name); }
return null;
}
/** 获取name名称对应的JComboBox */
public JComboBox ComboBox(String name)
{
if (Names.contains(name)) { return JComboBoxs.get(name); }
return null;
}
// 辅助功能函数
// ------------------------
/** 从JTextField中获取整型数据 */
public int Int(JTextField field)
{
return Int(field, 0);
}
/** 从JTextField中获取整型数据 */
public int Int(JTextField field, int defValue)
{
try
{
return Integer.parseInt(field.getText().trim());
}
catch (NumberFormatException e)
{
return defValue;
}
}
/** 获取JComboBox选中的项 */
public int Int(JComboBox combo)
{
return combo.getSelectedIndex();
}
/** 获取JComboBox选中的项 */
public String Str(JComboBox combo)
{
return combo.getSelectedItem().toString();
}
/** 从JTextField中字符串数据 */
public String Str(JTextField field)
{
return field.getText().trim();
}
/** 从JTextField中获取byte型数据 */
public byte Byte(JTextField field)
{
return Byte(field, 0);
}
/** 从JTextField中获取byte型数据 */
public byte Byte(JTextField field, int defValue)
{
try
{
return Byte.parseByte(field.getText().trim());
}
catch (NumberFormatException e)
{
return (byte) defValue;
}
}
}
import java.util.Map;
import com.scimence.component.LabelComp;
/** 客户端 SERVER
*
* @author zhouyongjun */
public class ClientServer implements ClientDaoData
{
private int id; // ID
private String name;// NAME
private String ip; // IP
private int port; // 端口号
private int state; // 状态 服务器状态 0:维护 灰色,1:顺畅 绿色,2:爆满 暗红,4:上次登录 蓝色
private int sort; // 排序 有小变大
private String log250FilePath; // 日志250服务器路径
private String logOnlineFilePath; // 日志在线服务器路径
private int logOnlinePort; // 在线日志查询端口号
private int sshPort;
public static final String[] SERVER_STATUS = { "维护关闭 ", "顺畅状态", "爆满状态", "上次登录" };
/** 添加当前服务器信息到LabelComp控件组中 */
public void AddTo(LabelComp LT)
{
// LT.Clear(); // 先清空已有信息
LT.AddIteam("id", "id", id);
LT.AddIteam("name", "名称", name);
LT.AddIteam("ip", "IP", ip);
LT.AddIteam("port", "端口号", port);
LT.AddComboBox("state", "状态", SERVER_STATUS, SERVER_STATUS[state], "服务器状态 0 维护关闭 > 1顺畅状态 >2爆满状态 >3上次登录");
LT.AddIteam("sort", "排序", sort, "排序 优先级从小到大");
LT.AddIteam("log250FilePath", "250路径", log250FilePath, "日志250服务器路径");
LT.AddIteam("logOnlineFilePath", "在线路径", logOnlineFilePath, "日志在线服务器路径");
LT.AddIteam("logOnlinePort", "在线端口", logOnlinePort, "在线日志查询端口号");
LT.AddIteam("sshPort", "sshPort", sshPort, "sshPort");
}
/** 从LabelComp控件组中,获取修改后的服务器信息 */
public void GetData(LabelComp LT)
{
id = LT.Int(LT.TextField("id"));
name = LT.Str(LT.TextField("name"));
ip = LT.Str(LT.TextField("ip"));
port = LT.Int(LT.TextField("port"));
state = LT.Int(LT.ComboBox("state"));
sort = LT.Int(LT.TextField("sort"));
log250FilePath = LT.Str(LT.TextField("log250FilePath"));
logOnlineFilePath = LT.Str(LT.TextField("logOnlineFilePath"));
logOnlinePort = LT.Int(LT.TextField("logOnlinePort"));
sshPort = LT.Int(LT.TextField("sshPort"));
}
}