📄 viewstudentframe.java
字号:
package edu.xscj.business;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.table.*;
import com.borland.jbcl.layout.*;
import edu.xscj.conn.*;
import edu.xscj.action.*;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import edu.xscj.bean.Student;
public class ViewStudentFrame extends JFrame {
public ViewStudentFrame() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.studentInfo("select * from student");
this.getContentPane().setLayout(xYLayout1);
editPanel.setLayout(xYLayout3);
editPanel.setBackground(Color.white);
editPanel.setBorder(BorderFactory.createEtchedBorder());
jTable1.setBorder(null);
jTable1.setGridColor(Color.gray);
jTable1.setSelectionBackground(Color.white);
jTable1.addMouseListener(new ViewStudentFrame_jTable1_mouseAdapter(this));
jMenuItem1.setText("删除");
jMenuItem1.addActionListener(new
ViewStudentFrame_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("添加新学生");
jMenuItem2.addActionListener(new
ViewStudentFrame_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("刷新");
jMenuItem3.addActionListener(new
ViewStudentFrame_jMenuItem3_actionAdapter(this));
jLabel1.setText("学号");
jLabel2.setText("姓名");
jLabel3.setText("性别");
radioSexBoy.setActionCommand("男");
radioSexBoy.setText("男");
radioSexGirl.setActionCommand("女");
radioSexGirl.setText("女");
jLabel4.setText("生日");
btnUpdate.addActionListener(new
ViewStudentFrame_btnUpdate_actionAdapter(this));
btnUpdate.setEnabled(false);
sexGroup.add(radioSexBoy);
sexGroup.add(radioSexGirl);
btnUpdate.setText("更新");
editPanel.add(btnUpdate, new XYConstraints(264, 173, 60, 22));
editPanel.add(txtUsername, new XYConstraints(141, 101, 120, 27));
editPanel.add(txtBirth, new XYConstraints(417, 105, 120, 27));
editPanel.add(jLabel4, new XYConstraints(334, 110, -1, -1));
editPanel.add(jLabel3, new XYConstraints(333, 56, -1, -1));
editPanel.add(radioSexBoy, new XYConstraints(417, 51, -1, -1));
editPanel.add(radioSexGirl, new XYConstraints(502, 51, -1, -1));
editPanel.add(txtUserno, new XYConstraints(141, 55, 120, 27));
editPanel.add(jLabel2, new XYConstraints(56, 109, -1, -1));
editPanel.add(jLabel1, new XYConstraints(57, 58, -1, -1));
this.getContentPane().add(jScrollPane1,
new XYConstraints(5, 5, 606, 272));
this.getContentPane().add(editPanel, new XYConstraints(6, 282, 606, 238));
jScrollPane1.getViewport().add(jTable1);
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
this.setSize(650, 550);
this.setResizable(false);
this.setTitle("浏览学生信息");
}
XYLayout xYLayout1 = new XYLayout();
JPanel editPanel = new JPanel();
XYLayout xYLayout3 = new XYLayout();
DefaultTableModel tableModel = new DefaultTableModel();
JTable jTable1 = new JTable();
JScrollPane jScrollPane1 = new JScrollPane();
JPopupMenu jPopupMenu1 = new JPopupMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
int row = 0;
JMenuItem jMenuItem3 = new JMenuItem();
JLabel jLabel1 = new JLabel();
JTextField txtUserno = new JTextField();
JLabel jLabel2 = new JLabel();
JTextField txtUsername = new JTextField();
JLabel jLabel3 = new JLabel();
ButtonGroup sexGroup = new ButtonGroup();
JRadioButton radioSexBoy = new JRadioButton();
JRadioButton radioSexGirl = new JRadioButton();
JTextField txtBirth = new JTextField();
JLabel jLabel4 = new JLabel();
JButton btnUpdate = new JButton();
TitledBorder titledBorder1 = new TitledBorder("");
public void studentInfo(String sql) {
Connection conn = null;
Statement stmt = null;
ResultSet rst = null;
tableModel.setColumnCount(0);
tableModel.setRowCount(0);
tableModel.addColumn("学号");
tableModel.addColumn("姓名");
tableModel.addColumn("性别");
tableModel.addColumn("生日");
try {
conn = ConnectDataBase.getConn();
stmt = conn.createStatement();
rst = stmt.executeQuery(sql);
int i = 0;
while (rst.next()) {
tableModel.setNumRows(i + 1);
tableModel.setValueAt(rst.getString("stuNo"), i, 0);
tableModel.setValueAt(rst.getString("stuName"), i, 1);
tableModel.setValueAt(rst.getString("stuSex"), i, 2);
tableModel.setValueAt(rst.getDate("stuBirthday"), i, 3);
i++;
}
jTable1.setModel(tableModel);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (rst != null) {
rst.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void jTable1_mouseClicked(MouseEvent e) {
int i = e.getButton();
int y = e.getY();
int x = e.getX();
if (i == 3) {
row = e.getY() / jTable1.getRowHeight();
jTable1.setRowSelectionInterval(row, row);
jPopupMenu1.show(e.getComponent(), x, y);
} else {
row = jTable1.getSelectedRow();
}
jTable1.setSelectionBackground(Color.cyan);
jTable1.setSelectionForeground(Color.red);
txtUserno.setText((String) jTable1.getModel().getValueAt(row, 0));
txtUsername.setText((String) jTable1.getModel().getValueAt(row, 1));
txtBirth.setText(jTable1.getModel().getValueAt(row, 3).toString());
if (jTable1.getModel().getValueAt(row, 2).equals("男")) {
radioSexBoy.setSelected(true);
} else if (jTable1.getModel().getValueAt(row, 2).equals("女")) {
radioSexGirl.setSelected(true);
}
btnUpdate.setEnabled(true);
txtUserno.setEditable(false);
}
public void jMenuItem2_actionPerformed(ActionEvent e) {
AddStudentFrame studentFrame = new AddStudentFrame();
studentFrame.setVisible(true);
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
String userNo = (String) jTable1.getModel().getValueAt(row, 0);
DeleteStudentAction del = new DeleteStudentAction();
del.deleteStudent(userNo);
try {
//this.setVisible(false);
//this.dispose();
//new ViewStudentFrame().setVisible(true);
this.studentInfo("select * from student");
} catch (Exception ex) {
}
}
public void jMenuItem3_actionPerformed(ActionEvent e) {
try {
//this.repaint();
//this.jbInit();
this.studentInfo("select * from student");
} catch (Exception ex) {
}
}
public void btnUpdate_actionPerformed(ActionEvent e) {
String stuNo = txtUserno.getText();
String stuName = txtUsername.getText();
if (stuNo.equals("") || stuName.equals("")) {
JOptionPane.showMessageDialog(this, "学号和姓名不能为空!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
return;
}
if (sexGroup.getSelection() == null ||
this.sexGroup.getSelection().equals("")) {
JOptionPane.showMessageDialog(this, "请选择性别!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
return;
}
String sex = sexGroup.getSelection().getActionCommand();
String stuBirth = txtBirth.getText();
Student student = new Student();
student.setStuNo(stuNo);
student.setStuName(stuName);
student.setStuSex(sex);
student.setStuBirthday(java.sql.Date.valueOf(stuBirth));
UpdateStudentAction upstudent = new UpdateStudentAction();
String str = upstudent.updateStudent(student);
if (str.equals("sucess")) {
JOptionPane.showMessageDialog(this, "更新成功!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
//this.setVisible(false);
//this.dispose();
try {
this.studentInfo("select * from student");
} catch (Exception ex) {
}
//new ViewStudentFrame().setVisible(true);
} else {
JOptionPane.showMessageDialog(this, "更新失败!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
class ViewStudentFrame_btnUpdate_actionAdapter implements ActionListener {
private ViewStudentFrame adaptee;
ViewStudentFrame_btnUpdate_actionAdapter(ViewStudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnUpdate_actionPerformed(e);
}
}
class ViewStudentFrame_jMenuItem3_actionAdapter implements ActionListener {
private ViewStudentFrame adaptee;
ViewStudentFrame_jMenuItem3_actionAdapter(ViewStudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem3_actionPerformed(e);
}
}
class ViewStudentFrame_jMenuItem1_actionAdapter implements ActionListener {
private ViewStudentFrame adaptee;
ViewStudentFrame_jMenuItem1_actionAdapter(ViewStudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class ViewStudentFrame_jMenuItem2_actionAdapter implements ActionListener {
private ViewStudentFrame adaptee;
ViewStudentFrame_jMenuItem2_actionAdapter(ViewStudentFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem2_actionPerformed(e);
}
}
class ViewStudentFrame_jTable1_mouseAdapter extends MouseAdapter {
private ViewStudentFrame adaptee;
ViewStudentFrame_jTable1_mouseAdapter(ViewStudentFrame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jTable1_mouseClicked(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -