📄 gypanel.java
字号:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import edu.njust.cs.*;
public class GYPanel extends CommonPanel{
public GYPanel(MainApp f,Connection con){
super(f,con);
f.labStatusContent.setText(" 您正在进行雇员资料维护");
columnNames=new String[] {"雇员编号","雇员姓名",
"所在部门","出生日期","薪水"};
dataType=new Class[] {String.class,String.class,
String.class,Timestamp.class,Double.class};
createTable();
//按需要设置每列宽度
this.setTableColumnWidth(0,100);
this.setTableColumnWidth(1,100);
this.setTableColumnWidth(2,200);
this.setTableColumnWidth(3,150);
this.setTableColumnWidth(4,100);
//使用DoubleRender渲染器,使得数值保持两位小数
TableColumn col=table.getColumn(columnNames[4]);
col.setCellRenderer(new DoubleRender());
}
public Object []constructLineForDBAdd(GYEditor d){
Object []lineForDBAdd=new Object[model.getColumnCount()];
lineForDBAdd[0]=d.getGYID();
lineForDBAdd[1]=d.getGYName();
lineForDBAdd[2]=d.getDept();
lineForDBAdd[3]=d.getBirth();
lineForDBAdd[4]=d.getSalary();;
return lineForDBAdd;
}
public Object []constructLineForDBUpdate(GYEditor d){
Object []lineForDBUpdate=new Object[model.getColumnCount()];
lineForDBUpdate[0]=d.getGYName();
lineForDBUpdate[1]=d.getDept();
lineForDBUpdate[2]=d.getBirth();
lineForDBUpdate[3]=d.getSalary();
lineForDBUpdate[4]=d.getGYID();
return lineForDBUpdate;
}
public void add(){
GYEditor d=new GYEditor(father,"新增",true);
d.show();
if(d.getActionCode()==EditorAction.OK){
String querySql="select * from GYT where GYID=?";
Object []keys={d.getGYID()};
if(SqlUtil.isRecordExist(con,querySql,keys)){
JOptionPane.showMessageDialog(this,
"该雇员编号已经存在!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insertSql="insert into GYT VALUES (?,?,?,?,?)";
Object []lineForDBAdd=constructLineForDBAdd(d);
if(SqlUtil.addRowToDB(con,insertSql,lineForDBAdd)){
Object []lineForTable=SqlUtil.getLineForTableFromLineForDB(
lineForDBAdd,dataType);
model.addRow(lineForTable);
table.changeSelection(model.getRowCount()-1,0,false,false);
}
else
JOptionPane.showMessageDialog(this,"新增时出错!","提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
public void modify(){
int selected=table.getSelectedRow();
if(selected>=0&&selected<model.getRowCount()){
GYEditor d=new GYEditor(father,"修改",true);
d.show();
if(d.getActionCode()==EditorAction.OK){
String updateSql="update GYT set GYNAME=?,GYDEPT=?,"+
" GYBIRTH=?, GYSALARY=? WHERE GYID=?";
Object [] lineForDBUpdate=constructLineForDBUpdate(d);
if(SqlUtil.updateRowInDB(con,updateSql,lineForDBUpdate))
{
selected=table.getSelectedRow();
model.removeRow(selected);
Object []lineForTable=SqlUtil.getLineForTableFromLineForDB(
constructLineForDBAdd(d),dataType);
model.insertRow(selected,lineForTable);
table.changeSelection(selected,0,false,false);
}
else
JOptionPane.showMessageDialog(this,"修改属性时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(this,"请先选定需修改的记录!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//查询
public void search(){
//创建查询条件面板
final JTextField txtName=new JTextField();
txtName.addActionListener(tfl);
JTextField txtDept=new JTextField();
txtDept.addActionListener(tfl);
JPanel sp=new JPanel();
sp.setLayout(new GridBagLayout());
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,0,1,1,new JLabel("雇员姓名"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,1,1,1,txtName);
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,2,1,1,new JLabel("所在部门"));
LayoutUtil.add(sp,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,3,1,1,txtDept);
//txtName获得输入焦点
SwingUtilities.invokeLater(new Runnable(){
public void run(){
txtName.requestFocus();
}
});
//创建查询对话框
String[] options = { "确定", "取消"};
int result = JOptionPane.showOptionDialog(
father, // 父组件
sp, // 查询条件面板
"查询条件", // 对话框标题
JOptionPane.DEFAULT_OPTION, // 选项类型
JOptionPane.QUESTION_MESSAGE, // 消息类型
null, //图标
options, // 按钮
options[0] // 缺省按钮
);
if(result==0){ //用户按下确定按钮
String querySql="select * from GYT where GYName like '%"+
txtName.getText()+"%' and "+
"GYDept like '%"+txtDept.getText()+"%'";
SqlUtil.readDBToTable(con,querySql,model,dataType);
}
}
//删除指定记录
public void delete(){
int selected=table.getSelectedRow();
if(selected>=0&&selected<model.getRowCount()){
int selection=JOptionPane.showConfirmDialog (this,
"您确定删除预该条记录?",
"确认",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (selection==JOptionPane.YES_OPTION){
String strIDToDelete=model.getValueAt(selected,0).toString().trim();
String deleteSql="delete from GYT where GYID=?";
Object []keys={strIDToDelete};
if(SqlUtil.deleteFromDB(con,deleteSql,keys))
model.removeRow(selected);
else
JOptionPane.showMessageDialog(this,"删除时出错!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
}
else
JOptionPane.showMessageDialog(this,"请选定一行,然后再删!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
}
//内部类,用于显示/编辑雇员信息的一个模式对话框
class GYEditor extends CommonEditor {
private JLabel labGYID=new JLabel(" 雇员编号 ");
private JTextField txtGYID=new JTextField();
private JLabel labGYName=new JLabel(" 雇员姓名 ");
private JTextField txtGYName=new JTextField();
private JLabel labDept=new JLabel(" 所在部门 ");
private JTextField txtDept=new JTextField();
private JLabel labBirth=new JLabel(" 出生日期 ");
private JTextField txtBirth=new JTextField();
private JLabel labSalary=new JLabel(" 薪 水 ");
private JTextField txtSalary=new JTextField();
public GYEditor(MainApp f,String s,boolean b){
super(f,s,b,GYPanel.this.table);
//面板p中显示雇员信息
JPanel p=new JPanel();
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.setLayout(new GridBagLayout());
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,0,1,1,labGYID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,0,1,1,txtGYID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,labGYName,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,0,1,1,txtGYName,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,1,1,1,labDept,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,1,1,1,txtDept,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,1,1,1,labBirth,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,1,1,1,txtBirth,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,2,1,1,labSalary,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,2,1,1,txtSalary,insets);
txtGYID.addActionListener(tfl);
txtGYName.addActionListener(tfl);
txtDept.addActionListener(tfl);
txtBirth.addActionListener(tfl);
txtSalary.addActionListener(tfl);
getContentPane().add(p,BorderLayout.CENTER);
//将对话框窗口定位在父窗口的居中位置
setSizeAndPosition(this,450,150);
//依据增加或是修改,设置pre next按钮是否可用
setPreNextButton(s);
if(!s.equals("新增"))
this.txtGYID.setEditable(false);
}
//将表格中选中的记录读入编辑框
public void initEditor(){
int selected=table.getSelectedRow();
this.txtGYID.setText((String)model.getValueAt(selected,0));
this.txtGYName.setText((String)model.getValueAt(selected,1));
this.txtDept.setText((String)model.getValueAt(selected,2));
this.txtBirth.setText(DateUtil.getYMDFromTS(
(Timestamp)model.getValueAt(selected,3)));
this.txtSalary.setText(df.format(model.getValueAt(selected,4)));
}
public String getGYID(){
return txtGYID.getText().trim();
}
public String getGYName(){
return txtGYName.getText().trim();
}
public String getDept(){
return txtDept.getText().trim();
}
//将薪水由字符串类型转化为Double类型
//如果不是合法的数值字符串
//取值Double.NEGATIVE_INFINITY
public Double getSalary(){
double result=Double.NEGATIVE_INFINITY;
try{
result=Double.parseDouble(txtSalary.getText().trim());
}catch(NumberFormatException e){
result=Double.NEGATIVE_INFINITY;
}
return new Double(result);
}
public Timestamp getBirth(){
return DateUtil.getTSFromYMD(txtBirth.getText());
}
public int getActionCode(){
return this.actionCode;
}
public void okClicked(){
//雇员编号不为空
if(this.txtGYID.getText().trim().equals("")||
this.txtGYID.getText().trim().length()!=4){
JOptionPane.showMessageDialog(father,
"请设定4位雇员编号!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//薪水为数值类型
try{
Double.parseDouble(this.txtSalary.getText().trim());
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(father,
"薪水为非数值类型!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return ;
}
//出生日期合法
if(!DateUtil.isValidYMDFormat(txtBirth.getText().trim())){
JOptionPane.showMessageDialog(father,
"出生日期不合法!\n格式为1999-10-29",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//隐藏对话框
this.actionCode=EditorAction.OK;
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -