📄 viewchengjiframe.java~56~
字号:
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.*;
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));
this.getContentPane().add(jScrollPane1,
new XYConstraints(4, 7, 584, 256));
jScrollPane1.getViewport().add(jTable1);
jPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
jPanel1.setLayout(xYLayout2);
this.getContentPane().add(jPanel1, new XYConstraints(4, 270, 583, 230));
jPopupMenu1.add(jMenuItem1);
jPopupMenu1.add(jMenuItem2);
jPopupMenu1.add(jMenuItem3);
jPopupMenu1.add(jMenuItem6);
jPopupMenu1.add(jMenuItem4);
jPopupMenu1.add(jMenuItem5);
this.setSize(620, 550);
this.setResizable(false);
this.setTitle("浏览课程信息");
}
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();
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 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);
}
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();
System.out.println(name);
System.out.println(path);
}
}
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 + -