📄 viewchengjiframe.java
字号:
package edu.xscj.business;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.BorderFactory;
import java.awt.Color;
import edu.xscj.conn.ConnectDataBase;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import edu.xscj.action.*;
import java.util.*;
import java.io.*;
import edu.xscj.bean.*;
public class ViewChengjiFrame extends JFrame {
public ViewChengjiFrame() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.chengji("select * from v_chengji");
this.getContentPane().setLayout(xYLayout1);
jMenuItem1.setText("刷新");
jMenuItem1.addActionListener(new
ViewChengjiFrame_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("添加成绩");
jMenuItem2.addActionListener(new
ViewChengjiFrame_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("删除成绩");
jMenuItem3.addActionListener(new
ViewChengjiFrame_jMenuItem3_actionAdapter(this));
jMenuItem4.setText("复制全部到Excel");
jMenuItem4.addActionListener(new
ViewChengjiFrame_jMenuItem4_actionAdapter(this));
jMenuItem5.setText("导出到记事本");
jMenuItem5.addActionListener(new
ViewChengjiFrame_jMenuItem5_actionAdapter(this));
jTable1.addMouseListener(new ViewChengjiFrame_jTable1_mouseAdapter(this));
jMenuItem6.setText("复制本行到Excel");
jMenuItem6.addActionListener(new
ViewChengjiFrame_jMenuItem6_actionAdapter(this));
lbStuNo.setText("学号");
lbCouNo.setText("课程编号");
lbChengji.setText("成绩");
btnOK.setText("确定");
btnOK.addActionListener(new ViewChengjiFrame_btnOK_actionAdapter(this));
this.getContentPane().add(jScrollPane1,
new XYConstraints(4, 7, 584, 256));
jScrollPane1.getViewport().add(jTable1);
jPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel1.setLayout(xYLayout2);
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
jPopupMenu1.add(jMenuItem6);
jPopupMenu1.add(jMenuItem4);
jPopupMenu1.add(jMenuItem5);
jPanel1.add(txtCourNo, new XYConstraints(243, 79, 124, 29));
jPanel1.add(txtStuNo, new XYConstraints(243, 24, 124, 29));
jPanel1.add(lbStuNo, new XYConstraints(170, 23, 33, 27));
jPanel1.add(lbChengji, new XYConstraints(168, 144, -1, -1));
jPanel1.add(txtChengji, new XYConstraints(242, 133, 125, 28));
jPanel1.add(btnOK, new XYConstraints(236, 188, 72, 28));
this.getContentPane().add(jPanel1, new XYConstraints(5, 270, 583, 241));
jPanel1.add(lbCouNo, new XYConstraints(157, 84, 56, -1));
this.setSize(620, 550);
this.setResizable(false);
this.setTitle("浏览成绩!!");
btnOK.setEnabled(false);
}
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JTable jTable1 = new JTable();
DefaultTableModel tableModel = new DefaultTableModel();
JPopupMenu jPopupMenu1 = new JPopupMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenuItem jMenuItem5 = new JMenuItem();
int row = 0;
JMenuItem jMenuItem6 = new JMenuItem();
JLabel lbStuNo = new JLabel();
JLabel lbCouNo = new JLabel();
JLabel lbChengji = new JLabel();
JTextField txtStuNo = new JTextField();
JTextField txtCourNo = new JTextField();
JTextField txtChengji = new JTextField();
JButton btnOK = new JButton();
public void chengji(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("课程名");
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("courNo"), i, 2);
tableModel.setValueAt(rst.getString("courName"), i, 3);
tableModel.setValueAt(rst.getString("stuCourGrade"), i, 4);
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 String tableStr() {
StringBuffer sbf = new StringBuffer();
int row = jTable1.getRowCount();
int col = jTable1.getColumnCount();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
sbf.append(jTable1.getModel().getValueAt(i, j));
if (j < col - 1) {
sbf.append("\t");
}
}
sbf.append("\r\n");
}
return sbf.toString();
}
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);
String stuNo = (String) jTable1.getModel().getValueAt(row, 0);
String courseNo = (String) jTable1.getModel().getValueAt(row, 2);
String chengji = (String) jTable1.getModel().getValueAt(row, 4);
txtStuNo.setText(stuNo);
txtCourNo.setText(courseNo);
txtChengji.setText(chengji);
txtStuNo.setEditable(false);
txtCourNo.setEditable(false);
btnOK.setEnabled(true);
}
public void jMenuItem1_actionPerformed(ActionEvent e) {
this.chengji("select * from v_chengji");
}
public void jMenuItem2_actionPerformed(ActionEvent e) {
AddChengjiFrame addChengji = new AddChengjiFrame();
addChengji.setVisible(true);
}
public void jMenuItem3_actionPerformed(ActionEvent e) {
String stuNo = (String) jTable1.getModel().getValueAt(row, 0);
String courseNo = (String) jTable1.getModel().getValueAt(row, 2);
DeleteChengjiAction deleteChengji = new DeleteChengjiAction();
deleteChengji.deleteChengji(stuNo, courseNo);
this.chengji("select * from v_chengji");
}
public void jMenuItem4_actionPerformed(ActionEvent e) {
CopyDataToExcel copy = new CopyDataToExcel();
copy.copy(jTable1);
}
public void jMenuItem6_actionPerformed(ActionEvent e) {
int col = jTable1.getColumnCount();
Vector vector = new Vector();
for (int i = 0; i < col; i++) {
vector.addElement(jTable1.getModel().getValueAt(row, i));
}
CopyDataToExcel copy = new CopyDataToExcel();
copy.copyRow(vector);
}
public void jMenuItem5_actionPerformed(ActionEvent e) {
JFileChooser jFileChooser1 = new JFileChooser();
int result = jFileChooser1.showSaveDialog(this);
if (result == jFileChooser1.CANCEL_OPTION) {
return; //取消就退出
}
File filename = jFileChooser1.getSelectedFile();
if (filename == null || filename.getName().equals("")) {
JOptionPane.showMessageDialog(this, "文件名不能为空!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
return;
}
String name = filename.getName();
String path = filename.getAbsolutePath();
if (path.lastIndexOf(".txt") == -1) {
path = path + ".txt";
}
try {
FileOutputStream fo = new FileOutputStream(new File(path));
OutputStreamWriter out = new OutputStreamWriter(fo);
String str = tableStr();
out.write(str);
out.flush();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
/** @todo Handle this exception */
}
}
public void btnOK_actionPerformed(ActionEvent e) {
String stuNo = txtStuNo.getText();
String courNo = txtCourNo.getText();
if (txtChengji.getText() == null || txtChengji.getText().equals("")) {
JOptionPane.showMessageDialog(this, "成绩不能为空!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
return;
}
int chji = Integer.parseInt(txtChengji.getText());
Chengji chengji = new Chengji();
chengji.setStuNo(stuNo);
chengji.setCourNo(courNo);
chengji.setStuCourGrade(chji);
UpdateChengjiAction updateChengji = new UpdateChengjiAction();
String str = updateChengji.updateChengji(chengji);
if (str.equals("sucess")) {
JOptionPane.showMessageDialog(this, "更新成功!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
try {
this.chengji("select * from v_chengji");
} catch (Exception ex) {
}
} else {
JOptionPane.showMessageDialog(this, "更新失败!", "温心提示",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
class ViewChengjiFrame_btnOK_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_btnOK_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnOK_actionPerformed(e);
}
}
class ViewChengjiFrame_jMenuItem5_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jMenuItem5_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem5_actionPerformed(e);
}
}
class ViewChengjiFrame_jMenuItem3_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jMenuItem3_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem3_actionPerformed(e);
}
}
class ViewChengjiFrame_jMenuItem4_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jMenuItem4_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem4_actionPerformed(e);
}
}
class ViewChengjiFrame_jMenuItem6_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jMenuItem6_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem6_actionPerformed(e);
}
}
class ViewChengjiFrame_jMenuItem2_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jMenuItem2_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem2_actionPerformed(e);
}
}
class ViewChengjiFrame_jMenuItem1_actionAdapter implements ActionListener {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jMenuItem1_actionAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuItem1_actionPerformed(e);
}
}
class ViewChengjiFrame_jTable1_mouseAdapter extends MouseAdapter {
private ViewChengjiFrame adaptee;
ViewChengjiFrame_jTable1_mouseAdapter(ViewChengjiFrame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jTable1_mouseClicked(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -