📄 stumagdialog.java
字号:
+ studentId;
conn = JdbcConnct.fetchConnection();
System.out.println(conn);
Statement stmt = conn.createStatement();
System.out.println(stmt);
System.out.println(sql);
int updateLine = stmt.executeUpdate(sql);
System.out.println(updateLine);
jTable1Model.removeRow(row);
stuTable.setModel(jTable1Model);
stuTable.setRowHeight(20);
JOptionPane.showMessageDialog(this, "删除学生信息成功!!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(sql);
} else {
// 如果选则的是否时,则返回。
return;
}
} else {
// 如果没有选定行,则提示没有选定
JOptionPane.showMessageDialog(this, "没有选定行!!");
}
}
private void selectComboBoxActionPerformed(ActionEvent evt) {
// System.out.println("selectComboBox.actionPerformed, event=" + evt);
// TODO add your code for selectComboBox.actionPerformed
if (this.selectComboBox.getSelectedItem().equals("全部")) {
this.commentTextField.setText("查询全部");
// this.commentTextField.setEditable(false);
} else if (this.selectComboBox.getSelectedItem().equals("学号")) {
// this.commentTextField.setEditable(true);
this.commentTextField.setText("学号");
} else if (this.selectComboBox.getSelectedItem().equals("姓名")) {
// this.commentTextField.setEditable(true);
this.commentTextField.setText("姓名");
}
}
/**
* 根据用户输入 返回SQL语句
*
* @return String SQL 不成功则返回null
*/
private String getSQL() {
// 定义学号正则表达式
String REG_STUID = "\\d{0,10}";
// 定义年龄正则表达式
String REG_STUAGE = "\\d{0,2}";
// 定义boolean型标志位bisRight ,用户输入是否正确,初始为false
boolean bisRight = false;
// 定义SQL语句
String strSQL = "";
// 定义临时字符串
String strTemp = "";
// 获得用户查询字段
strTemp = this.commentTextField.getText();
strSQL = "select * from stu_class_major ";
if (this.selectComboBox.getSelectedItem().equals("全部")) {
return strSQL;
} else if (this.selectComboBox.getSelectedItem().equals("学号")) {
if (strTemp.matches(REG_STUID)) {
bisRight = true;
} else {
JOptionPane.showMessageDialog(this, "学号为10位内数字,请重新输入。");
bisRight = false;
}
if (bisRight) {
strSQL += " where studentid = ";
strSQL += strTemp;
return strSQL;
} else {
return null;
}
} else if (this.selectComboBox.getSelectedItem().equals("姓名")) {
strSQL += "where stuname = '";
strSQL += strTemp + "'";
return strSQL;
} else if (this.selectComboBox.getSelectedItem().equals("年龄")) {
if (strTemp.matches(REG_STUAGE)) {
bisRight = true;
} else {
JOptionPane.showMessageDialog(this, "年龄为2位数字,请重新输入。");
bisRight = false;
}
if (bisRight) {
strSQL += " where age = ";
strSQL += strTemp;
return strSQL;
} else {
return null;
}
}
return "SQL";
}
/**
* 查看详细信息
*/
private void viewButton1ActionPerformed(ActionEvent evt) {
System.out.println("viewButton1.actionPerformed, event=" + evt);
// TODO add your code for viewButton1.actionPerformed
int row = this.stuTable.getSelectedRow();
if (row != -1) {
String stuId = (String) this.stuTable.getValueAt(row, 0);
String studentName = (String) this.stuTable.getValueAt(row, 1);
String gender = (String) this.stuTable.getValueAt(row, 2);
String age = (String) this.stuTable.getValueAt(row, 3);
String polistatus = (String) this.stuTable.getValueAt(row, 4);
String birthday = (String) this.stuTable.getValueAt(row, 5);
String contactway = (String) this.stuTable.getValueAt(row, 6);
String native1 = (String) this.stuTable.getValueAt(row, 7);
String entertime = (String) this.stuTable.getValueAt(row, 8);
String status = (String) this.stuTable.getValueAt(row, 9);
String className = (String) this.stuTable.getValueAt(row, 10);
String stuInfo = (String) this.stuTable.getValueAt(row, 11);
String remark = (String) this.stuTable.getValueAt(row, 12);
String photoPath = (String) this.stuTable.getValueAt(row, 13);
StudentAddDialog studentAddDialog = new StudentAddDialog(null);
studentAddDialog.setTitle("查看学生信息");
studentAddDialog.setVisible(true);
studentAddDialog.addButton.setEnabled(false);
studentAddDialog.submitButton.setEnabled(false);
studentAddDialog.studentIdTextField.setText(stuId);
studentAddDialog.studentIdTextField.setEditable(false);
studentAddDialog.stuNameTextField.setText(studentName);
if (null == gender) {
studentAddDialog.manRadioButton.setSelected(false);
studentAddDialog.famelRadioButton.setSelected(false);
} else if (gender.equals("男"))
studentAddDialog.manRadioButton.setSelected(true);
else if (gender.equals("女"))
studentAddDialog.famelRadioButton.setSelected(true);
studentAddDialog.ageTextField.setText(age);
studentAddDialog.poliStatusComboBox.setSelectedItem(polistatus);
studentAddDialog.birthDayTextField.setText(birthday);
studentAddDialog.contactWayTextField.setText(contactway);
studentAddDialog.enterTimeTextField.setText(entertime);
studentAddDialog.nativeTextField.setText(native1);
studentAddDialog.classIdTextField.setText(className);
studentAddDialog.infoTextArea.setText(stuInfo);
studentAddDialog.remarkTextArea.setText(remark);
studentAddDialog.pathTextField.setText(photoPath);
if (null != photoPath) {
ImageIcon icon = new ImageIcon(photoPath);
studentAddDialog.photoLabel.setIcon(icon);
}
// studentAddDialog.photoLabel.
} else {
JOptionPane.showMessageDialog(this, "没有选定行!");
}
}
/**
* 用户点击学生表,显示学生详细信息
* @param evt
*/
private void stuTableMouseClicked(MouseEvent evt) {
//显示学生信息
/*定义学生信息 学号、姓名、性别、年龄、政治面貌、出生日期、联系方式、籍贯、
* 入学时间、状态、班级名称、个人信息、备注
*/
String strstudentId = "";
String strstuName = "";
String strgender = "";
String strage = "";
String strpoliStatus = "";
String strbirthday = "";
String strcontactWay = "";
String strnative = "";
String strenterTime = "";
String strstuStatus = "";
String strclassName = "";
String strpersonInfo = "";
String strremark = "";
//定义临时字符串缓冲区
StringBuffer strBufTemp = new StringBuffer();
//学生表点击行数
int nrow = 0;
//如果用户双击表格
if (evt.getClickCount() == 2) {
// 获得学生表点击行
nrow = this.stuTable.getSelectedRow();
// 获得选中行数据
strstudentId = "学号: " + this.stuTableModel.getValueAt(nrow, 0)
+ "\n";
strstuName = "姓名: " + this.stuTableModel.getValueAt(nrow, 1) + "\n";
strgender = "性别: " + this.stuTableModel.getValueAt(nrow, 2) + "\n";
strage = "年龄: " + this.stuTableModel.getValueAt(nrow, 3) + "\n";
strpoliStatus = "政治面貌: " + this.stuTableModel.getValueAt(nrow, 4)
+ "\n";
strbirthday = "出生日期: " + this.stuTableModel.getValueAt(nrow, 5)
+ "\n";
strcontactWay = "联系方式: " + this.stuTableModel.getValueAt(nrow, 6)
+ "\n";
strnative = "籍贯: " + this.stuTableModel.getValueAt(nrow, 7) + "\n";
strenterTime = "入学时间: " + this.stuTableModel.getValueAt(nrow, 8)
+ "\n";
strstuStatus = "状态: " + this.stuTableModel.getValueAt(nrow, 9)
+ "\n";
strclassName = "班级名称: " + this.stuTableModel.getValueAt(nrow, 10)
+ "\n";
strpersonInfo = "个人信息: " + this.stuTableModel.getValueAt(nrow, 11)
+ "\n";
strremark = "备注: " + this.stuTableModel.getValueAt(nrow, 12) + "\n";
// 为字符串缓冲区赋值
strBufTemp.append(strstudentId);
strBufTemp.append(strstuName);
strBufTemp.append(strgender);
strBufTemp.append(strage);
strBufTemp.append(strpoliStatus);
strBufTemp.append(strbirthday);
strBufTemp.append(strcontactWay);
strBufTemp.append(strnative);
strBufTemp.append(strenterTime);
strBufTemp.append(strstuStatus);
strBufTemp.append(strclassName);
strBufTemp.append(strpersonInfo);
strBufTemp.append(strremark);
// 显示学生详细信息
JOptionPane.showMessageDialog(this, strBufTemp, "学生详细信息",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
class SelfMouseAdapter extends java.awt.event.MouseAdapter {
public void mouseClicked(java.awt.event.MouseEvent e) {
if (e.getClickCount() == 2) {
}
}
}
/**
* 照片选择类
* @author Administrator
*
*/
class PicChooser {
private static JFileChooser fileChooser;
public static FileFilter filter = new FileFilter();
/**
* Auto-generated main method to display this JDialog
*/
public String getString() {
JFrame frame = new JFrame();
fileChooser = new JFileChooser(
"D:\\eclipse\\workspace\\JavaSwingExample\\com\\cloudgarden\\layout");
fileChooser.addChoosableFileFilter(filter);
int reg = fileChooser.showDialog(frame, "打开");
String path = null;
String name = null;
if (reg == fileChooser.APPROVE_OPTION)// 如果为OPEN
{
System.out.println(fileChooser.getSelectedFile());
// 得到被选中的文件的路径
// String fPath =
// fileChooser.getSelectedFile().getPath();//得到被选择文件路径
path = fileChooser.getSelectedFile().getPath();// 得到被选择文件名字
name = fileChooser.getSelectedFile().getName();
// 定义要拷贝到的目的目录
// String tDirectory =//this.getClass().getResource("/").getPath()+
// "d:\\imge";
// File file = new File(tDirectory);//新建一个文件的对象,它的路径为指定路径.
// 如果目录不存在
// if(!file.exists()){
// file.mkdirs();//创建目录以及子目录
// }
// 如果该文件不存在
// 定义拷贝后的文件路径
// String tPath = tDirectory +"\\"+fName;
// 设置一个图标文件
// javax.swing.ImageIcon image = new javax.swing.ImageIcon(fPath);
// image = new
// javax.swing.ImageIcon(getScaledImage(image.getImage(), 150,
// 100));
// 用Lable显示
// this.photoLabel.setIcon(image);
// 把路径给予st_photo以便存于数据库
// st_photo = tPath;
// this.detailedInfoTableModel.setValueAt(st_photo,6,1);
// System.out.println(st_photo);
// try{
// 如果为真那么就复制
// if(hasImgeFile( file ,fName)){
// copyImge(fPath,tPath);
// }
// }//end try
// catch(Exception e){
// e.printStackTrace();
// }
}
return path;
}
}
/**
* 过滤图片文件类。
* @author Administrator
*
*/
class FileFilter extends javax.swing.filechooser.FileFilter
{
/**
* 判断是否可以显示。
*/
public boolean accept(File f) {
boolean flag = true;
if (f.isDirectory())
flag = true;
else if (f.isFile()) {
String fileName = f.getName();
flag = fileName.endsWith(".jpg") || fileName.endsWith(".bmp")
|| fileName.endsWith(".gif") || fileName.endsWith(".png");
}
return flag;
}
/**
* 显示文件描述文件
*/
public String getDescription() {
return "jpg ,gif ,bmp,and png Images";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -