📄 configdialog.java
字号:
//ConfigDialog.java
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
public class ConfigDialog extends JDialog{
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel2 = new JPanel();
GridLayout gridLayout1 = new GridLayout(0,1,10,10);
JPanel jPanel3 = new JPanel();
BorderLayout borderLayout3 = new BorderLayout();
JPanel jPanel4 = new JPanel();
FlowLayout flowLayout1 = new FlowLayout(FlowLayout.CENTER);
JButton btAdd = new JButton();
JButton btDelete = new JButton();
JButton btEdit = new JButton();
JButton btOk = new JButton();
JButton btCancel= new JButton();
JScrollPane jScrollPane = new JScrollPane();
DefaultTableModel tableModel = new DefaultTableModel();
JTable configTable = new JTable();
final static String TITLES ="配置属性";
Vector dialogData = new Vector();
String sSele = "select * from Config";
Vector config_COLUMN_NAMES;
ConfigFloor floor= new ConfigFloor();
boolean isOk=false;
public ConfigDialog(Frame frame,String title,boolean modal){
super(frame,title,modal);
try{
ConfigInit();
pack();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public ConfigDialog(){
this(null,TITLES,false);
}
void ConfigInit()throws Exception{
this.setSize(new Dimension(80,60));
panel1.setLayout(borderLayout1);
jPanel1.setLayout(borderLayout2);
jPanel2.setLayout(borderLayout3);
jPanel3.setLayout(flowLayout1);
jPanel4.setLayout(gridLayout1);
btAdd.setText("增加");
btAdd.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
btAdd_actionPerformed(e);
}
});
btDelete.setText("删除");
btDelete.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
btDelete_actionPerformed(e);
}
});
btEdit.setText("编辑");
btEdit.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
btEdit_actionPerformed(e);
}
});
btOk.setText("确定");
btOk.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
btOk_actionPerformed(e);
}
});
btCancel.setText("取消");
btCancel.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
btCancel_actionPerformed(e);
}
});
getContentPane().add(panel1);
panel1.add(jPanel1,BorderLayout.CENTER);
jPanel1.add(jScrollPane,BorderLayout.CENTER);
jScrollPane.getViewport().add(configTable,null);
panel1.add(jPanel3,BorderLayout.EAST);
jPanel3.add(jPanel2,null);
jPanel2.add(jPanel4,BorderLayout.NORTH);
jPanel4.add(btAdd,null);
jPanel4.add(btEdit,null);
jPanel4.add(btDelete,null);
jPanel4.add(btOk,null);
jPanel4.add(btCancel,null);
initDialog();
}
void initDialog(){
DataBaseSQL look = new DataBaseSQL();
dialogData=look.setSelect(sSele,2);
config_COLUMN_NAMES=floor.getConfigDialongColumnNameVector();
tableModel.setDataVector(dialogData,config_COLUMN_NAMES);
configTable.setModel(tableModel);
configTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
void showDialog(JDialog dlg){
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width-dlgSize.width)/2+loc.x,(frmSize.height-dlgSize.height)/2+loc.y);
dlg.setModal(true);//
dlg.show();
}
protected void btAdd_actionPerformed(ActionEvent e){
DataBaseSQL ff = new DataBaseSQL();
String sInsert;
//int scanType=0;
ConfigEditDialog dlg = new ConfigEditDialog();
dlg.initDialog(ConfigEditDialog.NEW_OPERATION,null);
showDialog(dlg);
if(dlg.isOk){
addRecord(dlg.getUpdatedFloor());
configTable.revalidate();
configTable.repaint();
// if(dlg.getUpdatedFloor().getPort()==0){
// scanType=1;
// }
sInsert="Insert into Config values('"+dlg.getUpdatedFloor().getIP()+"',"+dlg.getUpdatedFloor().getPort()+",0,'',0)";
ff.setInsert(sInsert);
ff.conClose();
}
}
protected void btDelete_actionPerformed(ActionEvent e){
DataBaseSQL ff = new DataBaseSQL();
int row = configTable.getSelectedRow();
int column=configTable.getSelectedColumn();
if(row==-1||column==-1){
JOptionPane.showMessageDialog(this,"请选择记录!","提示",JOptionPane.WARNING_MESSAGE);
return;
}
Vector v=(Vector)dialogData.elementAt(row);
ConfigFloor floorObj=ConfigFloor.parseConfigFloor(v);
ConfigEditDialog dlg = new ConfigEditDialog();
dlg.initDialog(ConfigEditDialog.DELETE_OPERATION,floorObj);
showDialog(dlg);
if(dlg.isOk){
deleteRecord(row);
configTable.revalidate();
configTable.repaint();
String sDelete="Delete from Config where IP='"+dlg.getUpdatedFloor().getIP()+"'";
ff.setDelete(sDelete);
ff.conClose();
}
}
protected void btEdit_actionPerformed(ActionEvent e){
DataBaseSQL ff = new DataBaseSQL();
int row = configTable.getSelectedRow();
int column=configTable.getSelectedColumn();
if(row==-1||column==-1){
JOptionPane.showMessageDialog(this,"请选择记录!","提示",JOptionPane.WARNING_MESSAGE);
return;
}
Vector v = (Vector)dialogData.elementAt(row);
ConfigFloor floorObj=ConfigFloor.parseConfigFloor(v);
String condation=(String)v.elementAt(0);
ConfigEditDialog dlg = new ConfigEditDialog();
dlg.initDialog(ConfigEditDialog.EDIT_OPERATION,floorObj);
showDialog(dlg);
if(dlg.isOk){
editRecord(dlg.getUpdatedFloor(),row);
configTable.revalidate();
configTable.repaint();
String sUpdate="Update Config set IP='"+dlg.getUpdatedFloor().getIP()+"',port="+dlg.getUpdatedFloor().getPort()+",wrongTimes=0 where IP='"+condation+"'";
ff.setUpdate(sUpdate);
ff.conClose();
}
}
protected void btCancel_actionPerformed(ActionEvent e){
this.dispose();
}
protected void btOk_actionPerformed(ActionEvent e){
isOk=true;
this.dispose();
}
/**
*ConfigFloor对象加入表格模型内,在实际过程中需要加入对数据库的存盘
*@param floor- 需要添加入表格模型内的Floor对象
*/
void addRecord(ConfigFloor floor){
dialogData.add(floor.getDataVector());
}
void editRecord(ConfigFloor floor,int index){
Vector v = floor.getDataVector();
dialogData.setElementAt(v,index);
}
/**
*删除数据模型里的指定行,在现实中除了显示的数据后台外,还需要更新数据库
*@param index-需要删除的行索引
*/
void deleteRecord(int index){
dialogData.removeElementAt(index);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -