//初始化窗体
private void init(String name) {
this.setExtendedState(MAXIMIZED_BOTH); //设置窗体最大化
this.setLocation(0,0); //设置窗体加载的初始位置
this.setResizable(false); //禁用窗体最大化按钮
this.jTextField1.requestFocus(); //设置文本框获焦
this.jTextField8.setEditable(false); //设置文本框是否可用
//设置窗体居中显示-------------------------------------------------------------------------------Start
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width - this.getWidth())/2, (screenSize.height - this.getHeight())/2 - 90);
//------------------------------------------------------------------------------------------------End
//设置窗体左上角图标(MyEclipse)----------------------------------------------------------------Start
窗体实例.setIconImage(new ImageIcon("img/tel.jpg").getImage());
//-------------------------------------------------------------------------------------------------End
// 设置面板的边框(TitledBorder)-----------------------------------------------------------------Start
pnlSearch.setBorder(BorderFactory.createTitledBorder(null, "查询条件", TitledBorder.LEADING, TitledBorder.TOP, new java.awt.Font("宋体",0,14), new java.awt.Color(0,0,0)));
//--------------------------------------------------------------------------------------------------End
}
private void initTable() { //初始化Jtable
//用DefaultTableCellRenderer设置表中的信息居中显示(MyEclipse)————————————Start
DefaultTableCellRenderer render = (DefaultTableCellRenderer)this.table.getTableHeader().getDefaultRenderer();
render.setHorizontalAlignment(SwingConstants.CENTER); //表头数据居中显示
render = new DefaultTableCellRenderer();
render.setHorizontalAlignment(SwingConstants.CENTER); //表中信息居中显示
this.table.setDefaultRenderer(Object.class, render);
//———————————————————————————————————————————End
//设置列宽———————————————————————————————————————Start
for(int i=0; i<9; i++){
this.jTable1.getColumnModel().getColumn(i).setMaxWidth(100);
this.jTable1.getColumnModel().getColumn(i).setMinWidth(100);
}
this.jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//———————————————————————————————————————————End
//隐藏表中的某列————————————————————————————————————Start
/**
* 隐藏表的指定列,该方法只是将指定列的列宽设定为0,不影响取隐藏列的value和排序等操作
* @param columnIndex 列索引
*/
private void hiddenColumn(int columnIndex){
TableColumn tc = this.jTable1.getColumnModel().getColumn(columnIndex);
TableColumn tch = this.jTable1.getTableHeader().getColumnModel().getColumn(columnIndex);
tc.setWidth(0);
tc.setMinWidth(0);
tc.setMaxWidth(0);
tch.setWidth(0);
tch.setMinWidth(0);
tch.setMaxWidth(0);
}
//———————————————————————————————————————————End
DefaultTableModel model = new DefaultTableModel(getData("%"),getColName("办理油卡")); //实例化Jtable模型
TableModel model = (TableModel)this.jTable1.getModel(); //获取JTable模型
model.setRowCount(15); //设置model行数
this.jTable1.setModel(model); //设置Jtable模型
this.jTable1.setRowHeight(25); //设置Jtable行高
this.jTable1.setEnabled(false); //设置Jtable是否禁用
this.jTable1.getTableHeader().setFont(new java.awt.Font("宋体",1,18)); //设置表头字体
this.jTable1.getTableHeader().setReorderingAllowed(false); //设置表头不能重新排序
this.jTable1.getTableHeader().setResizingAllowed(false); //设置表头不能调整大小
this.jTable1.setRowSelectionInterval(0, 0); //让Jable默认选中第一行
this.jTable1.requestFocus(); //让Jtable获取焦点
this.jTable1.setColumnSelectionAllowed(false); //设置只能选行,不能选列
this.jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //设置只选单行
//this.jTable1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //设置只能选择单行
//添加选中行事件
this.jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) { //当选中的值发生变化的时候,才执行下面代码
int row = jTable1.getSelectedRow();
if(row >= 0) {
jTextField1.setText((String)jTable1.getValueAt(row, 0));
jTextField3.setText((String)jTable1.getValueAt(row, 1));
jTextField2.setText((String)jTable1.getValueAt(row, 2));
if(((String)jTable1.getValueAt(row, 3)).replaceAll(" ", "").equals("男")){
jComboBox1.setSelectedIndex(1);
}else if(((String)jTable1.getValueAt(row, 3)).replaceAll(" ", "").equals("女")){
jComboBox1.setSelectedIndex(2);
}
}
}
}
});
}
//得到Jtable中的每行信息
private Vector getData(String cid) {
Vector vectorData = new Vector();
Vector vector = null;
if(cid.equals("%")) {
List list1 = search.SearchCardAndTaxi(cid);
if(list1.size() > 0) {
for(int i=0; i<list1.size(); i++){
if(i % 2 == 0){
vector = new Vector();
vector.addElement((Taxi)list1.get(i).getTid());
} else if(i % 2 == 1){
vector.addElement( (Card)list1.get(i).getCid());
vectorData.add(vector);
}
}
}
} else if(cid.equals("%%")) {
List list2 = search.SearchCZXL(cid.substring(0,1));
if(list2.size() > 0) {
for(int i=0; i<list2.size(); i++){
vector = new Vector();
vector.addElement((Czxf)list2.get(i).getCid());
vectorData.add(vector);
}
}
}
return vectorData;
}
//得到Jtable中的列标题
private Vector getColName(String flag){
Vector vectorColName = new Vector();
if(flag.equals("办理油卡")){
vectorColName.addElement("初始储值");
vectorColName.addElement("油卡状态");
} else if(flag.equals("查询信息")){
vectorColName.addElement("消费金额");
}
return vectorColName;
}