📄 cbbpanel.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 CBBPanel extends CommonPanel{
public CBBPanel(MainApp f,Connection con){super(f,con);
f.labStatusContent.setText(" 您正在进行抄表班资料维护");
columnNames=new String[] {"抄表班编号","抄表班名称","备注"};
dataType=new Class[] {String.class,String.class,String.class};
createTable();
//按需要设置每列宽度
this.setTableColumnWidth(0,100);
this.setTableColumnWidth(1,100);
this.setTableColumnWidth(2,300);
}
public Object []constructLineForDBAdd(CBBEditor d){
Object []lineForDBAdd=new Object[model.getColumnCount()];
lineForDBAdd[0]=d.getCBBID();
lineForDBAdd[1]=d.getCBBName();
lineForDBAdd[2]=d.getMemo();
return lineForDBAdd;
}
public Object []constructLineForDBUpdate(CBBEditor d){
Object []lineForDBUpdate=new Object[model.getColumnCount()];
lineForDBUpdate[0]=d.getCBBName();
lineForDBUpdate[1]=d.getMemo();
lineForDBUpdate[2]=d.getCBBID();
return lineForDBUpdate;
}
public void add(){
CBBEditor d=new CBBEditor(father,"新增",true);
d.show();
if(d.getActionCode()==EditorAction.OK){
String querySql="select * from CBBT where CBBID=?";
Object []keys={d.getCBBID()};
if(SqlUtil.isRecordExist(con,querySql,keys)){
JOptionPane.showMessageDialog(this,"该抄表班编号已经存在!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String insertSql="insert into CBBT 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()){
CBBEditor d=new CBBEditor(father,"修改",true);
d.show();
if(d.getActionCode()==EditorAction.OK) {
String updateSql="update CBBT set CBBNAME=?,CBBMEMO=? WHERE CBBID=?";
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(){
//创建查询条件面板
JTextField txtID=new JTextField();
txtID.addActionListener(tfl);
final JTextField txtName=new JTextField();
txtName.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,txtID);
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,txtName);
//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 CBBT where CBBName like '%"+txtName.getText()+"%' and "+
"CBBID like '%"+txtID.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 CBBT where CBBID=?";
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 CBBEditor extends CommonEditor {
private JLabel labCBBID=new JLabel(" 抄表班编号 ");
private JTextField txtCBBID=new JTextField();
private JLabel labCBBName=new JLabel(" 抄表班名称 ");
private JTextField txtCBBName=new JTextField();
private JLabel labMemo=new JLabel(" 抄表班备注 ");
private JTextArea txtMemo=new JTextArea();
public CBBEditor(MainApp f,String s,boolean b){
super(f,s,b,CBBPanel.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,labCBBID,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,0,1,1,txtCBBID,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,labCBBName,insets);
LayoutUtil.add(p,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,0,1,1,txtCBBName,insets);
LayoutUtil.add(p,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,1,1,1,labMemo,insets);
LayoutUtil.add(p,GridBagConstraints.BOTH,
GridBagConstraints.CENTER,100,100,0,2,4,1,new JScrollPane(txtMemo),insets);
txtCBBID.addActionListener(tfl);
txtCBBName.addActionListener(tfl);
getContentPane().add(p,BorderLayout.CENTER);
//将对话框窗口定位在父窗口的居中位置
setSizeAndPosition(this,450,250);
//依据增加或是修改,设置pre next按钮是否可用
setPreNextButton(s);
if(!s.equals("新增")) txtCBBID.setEditable(false);
}
//将表格中选中的记录读入编辑框
public void initEditor(){
int selected=table.getSelectedRow();
this.txtCBBID.setText((String)model.getValueAt(selected,0));
this.txtCBBName.setText((String)model.getValueAt(selected,1));
this.txtMemo.setText((String)model.getValueAt(selected,2));
}
public String getCBBID(){
return txtCBBID.getText().trim();
}
public String getCBBName(){
return txtCBBName.getText().trim();
}
public String getMemo(){
return txtMemo.getText().trim();
}
public int getActionCode(){
return this.actionCode;
}
public void okClicked(){
//抄表班编号不为空
if(this.txtCBBID.getText().trim().equals("")
||this.txtCBBID.getText().trim().length()!=2){
JOptionPane.showMessageDialog(father,
"请设定2位抄表班编号!",
"提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//隐藏对话框
this.actionCode=EditorAction.OK;
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -