📄 stdframe.java
字号:
import java.awt.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class StdFrame extends JFrame implements ActionListener,MouseListener{
JPanel jPanel;
JPanel jPanel1 =new JPanel();
JPanel jPanel2 =new JPanel();
JPanel jPanel3 =new JPanel();
JScrollPane jscro=new JScrollPane();
JButton jButton1 = new JButton(new ImageIcon("add.jpg"));//定义并初始化按钮
JButton jButton2 = new JButton(new ImageIcon("del1.jpg"));
JButton jButton3 = new JButton(new ImageIcon("photo.gif"));
JButton jButton4 = new JButton(new ImageIcon("order.jpg"));
JButton jButton5 = new JButton(new ImageIcon("correct.jpg"));
JButton jButton6 = new JButton(new ImageIcon("all.jpg"));
JButton jButton7 = new JButton(new ImageIcon("average.jpg"));
//ImageIcon
ImageIcon image= new ImageIcon("search.jpg");//创建对象
ImageIcon image1=new ImageIcon("welcome.gif");
ImageIcon image0=new ImageIcon("flag.jpg");
///label
JLabel jLabel1 = new JLabel("请输入要查询的学号:");//标签,实现查询
JLabel jLa= new JLabel(" ");
JLabel jLabel2 = new JLabel("",image,JLabel.LEFT);
JLabel jLabel0 = new JLabel("",image1,JLabel.LEFT);
JLabel Label3 = new JLabel("");
JTextField t1 = new JTextField(10);//创建文本域对象
JMenuBar menubar=new JMenuBar();//创建菜单条
JMenu file=new JMenu("文件管理");
JMenu edit=new JMenu("编辑");
JMenu view=new JMenu("饼状图显示");
JMenu author=new JMenu("帮助");
Kechengbiao1 kechengbiao1=new Kechengbiao1("显示课程表信息");
JMenuItem kebiao=new JMenuItem("课程表");
JMenuItem exit=new JMenuItem("退出系统");
JMenuItem e1=new JMenuItem("添加信息");
JMenuItem e2=new JMenuItem("删除信息");
JMenuItem e3=new JMenuItem("修改信息");
JMenuItem v1=new JMenuItem("平均成绩图");
JMenuItem v2=new JMenuItem("个人平均分");
JMenuItem v3=new JMenuItem("语文成绩图");
JMenuItem v4=new JMenuItem("数学成绩图");
JMenuItem v5=new JMenuItem("英语成绩图");
JMenuItem v6=new JMenuItem("物理成绩图");
JMenuItem v7=new JMenuItem("版权信息");
////jtoolbar
JToolBar toolBar=new JToolBar();//工具条
///jtable
JTable Jtb=new JTable();//创建表格
Vector tableData = new Vector();
Vector rowData=new Vector();
DefaultTableModel model=(DefaultTableModel) Jtb.getModel();
//data
Connection con;
Statement stmt;
ResultSet rs;
String Sql=new String("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=students.mdb");
String sql="select * from students";
String ordersql="select * from Students order by 平均分 desc";
public StdFrame(){
super("学生成绩管理系统");
setSize(580,500);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
jPanel=(JPanel)this.getContentPane();
///jmenubar
setJMenuBar(menubar);
menubar.add(file);
menubar.add(edit);
menubar.add(view);
menubar.add(author);
file.add(kebiao);
file.add(exit);
edit.add(e1);
edit.add(e2);
edit.add(e3);
view.add(v3);
view.add(v4);
view.add(v5);
view.add(v6);
view.add(v1);
view.add(v2);
author.add(v7);
////jtoolbar
toolBar.add(jButton1);//向工具条中添加按钮
toolBar.add(jButton2);
toolBar.add(jButton3);
toolBar.add(jButton4);
toolBar.add(jButton5);
toolBar.add(jButton6);
toolBar.add(jButton7);
//jtable
tableData.addElement("学号");//向表格中添加内容
tableData.addElement("姓名");
tableData.addElement("语文");
tableData.addElement("数学");
tableData.addElement("英语");
tableData.addElement("物理");
tableData.addElement("平均分");
model.setDataVector(rowData, tableData);
Jtb.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//data
datadisplay(sql);
//jpanel
jPanel3.add(jLabel0);
jPanel3.add(jLa);
jPanel3.add(jLabel1);
jPanel3.add(t1);
jPanel3.add(jLabel2);
jPanel3.setLayout(new FlowLayout(FlowLayout.RIGHT));
jPanel2.add(toolBar);
jPanel2.add(Label3);
jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel1.add(jPanel3);
jPanel1.add(jPanel2);
jPanel1.setLayout(new GridLayout(2,1));
jPanel2.setBackground(new Color(255, 255, 255));
jPanel3.setBackground(new Color(255, 255, 255));
jscro.getViewport().add(Jtb,null);
Jtb.setBackground(new Color(255, 255, 255));
jPanel.add(jscro,BorderLayout.CENTER);
jPanel.add(jPanel1,BorderLayout.NORTH);
jPanel.add(jscro);
//listenner
jButton1.addMouseListener(this);//添加监听器
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton2.addMouseListener(this);
jButton3.addActionListener(this);
jButton3.addMouseListener(this);
jButton4.addActionListener(this);
jButton4.addMouseListener(this);
jButton5.addActionListener(this);
jButton5.addMouseListener(this);
jButton6.addActionListener(this);
jButton6.addMouseListener(this);
jButton7.addActionListener(this);
jButton7.addMouseListener(this);
e1.addActionListener(this);
e2.addActionListener(this);
e3.addActionListener(this);
kebiao.addActionListener(this);
exit.addActionListener(this);
v1.addActionListener(this);
v2.addActionListener(this);
v3.addActionListener(this);
v4.addActionListener(this);
v5.addActionListener(this);
v6.addActionListener(this);
v7.addActionListener(this);
////jlabel
jLabel2.setOpaque(true);
jLabel2.addMouseListener(this);
}
///listenner
public void actionPerformed(ActionEvent e){
if(e.getSource()==kebiao){
kechengbiao1.setVisible(true);}
if(e.getSource()==exit){
System.exit(0);
}
if(e.getSource()==jButton1||e.getSource()==e1){
adddialog dlg=new adddialog();
dlg.setTitle("添加学生信息");
dlg.setBounds(260, 200,300, 200);
dlg.setVisible(true);
}
if(e.getSource()==jButton2||e.getSource()==e2){
try{//接收异
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce){
System.out.println("SQLException:"+ce.getMessage());
}
try{
con=DriverManager.getConnection(Sql);
stmt=con.createStatement();
int tablerow= Jtb.getSelectedRow();
if(tablerow>=0){
String str = String.valueOf(Jtb.getValueAt(tablerow, 0));
stmt.executeUpdate("delete from students where 学号='"+str+"'");
JOptionPane.showMessageDialog(null,"删除成功!","删除成功!",JOptionPane.INFORMATION_MESSAGE);
datadisplay(sql);
}
else JOptionPane.showMessageDialog(null,"没有选中选项!","没有选中选项!",JOptionPane.INFORMATION_MESSAGE);
}
catch(SQLException ee){
System.out.println("SQLException:"+ee.getMessage());
}
}
if(e.getSource()==jButton3||e.getSource()==v1){
displaydialog dlg2=new displaydialog(7);
dlg2.setTitle("平均成绩图和数据");
dlg2.setBounds(400, 300,400, 300);
dlg2.setVisible(true);
}
if(e.getSource()==jButton3||e.getSource()==v3){
displaydialog dlg2=new displaydialog(3);
dlg2.setTitle("语文成绩图和数据");
dlg2.setBounds(400, 300,400, 300);
dlg2.setVisible(true);
}
if(e.getSource()==jButton3||e.getSource()==v4){
displaydialog dlg2=new displaydialog(4);
dlg2.setTitle("数学成绩图和数据");
dlg2.setBounds(400, 300,400, 300);
dlg2.setVisible(true);
}
if(e.getSource()==jButton3||e.getSource()==v5){
displaydialog dlg2=new displaydialog(5);
dlg2.setTitle("英语成绩图和数据");
dlg2.setBounds(400, 300,400, 300);
dlg2.setVisible(true);
}
if(e.getSource()==jButton3||e.getSource()==v6){
displaydialog dlg2=new displaydialog(6);
dlg2.setTitle("物理成绩图和数据");
dlg2.setBounds(400, 300,400, 300);
dlg2.setVisible(true);
}
if(e.getSource()==jButton4){
datadisplay(ordersql);
}
if(e.getSource()==v7){
JOptionPane.showMessageDialog(null,"军军,版本1.5","版权信息",JOptionPane.PLAIN_MESSAGE);
}
if(e.getSource()==jButton5||e.getSource()==e3){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ce){
System.out.println("SQLException:"+ce.getMessage());
}
try{
con=DriverManager.getConnection(Sql);
stmt=con.createStatement();
int tablerow= Jtb.getSelectedRow();
int tablecolumn= Jtb.getSelectedColumn();
if(tablerow>=0||tablecolumn>=0){
String str = String.valueOf(Jtb.getValueAt(tablerow, 0));
String str1=String.valueOf(Jtb.getValueAt(tablerow, 1));
String str2=String.valueOf(Jtb.getValueAt(tablerow, 2));
String str3=String.valueOf(Jtb.getValueAt(tablerow, 3));
String str4=String.valueOf(Jtb.getValueAt(tablerow, 4));
String str5=String.valueOf(Jtb.getValueAt(tablerow, 5));
float f01= Float.parseFloat(str2);
float f02= Float.parseFloat(str3);
float f03= Float.parseFloat(str4);
float f04= Float.parseFloat(str5);
float f05= (f01+f02+f03+f04)/4;
//更新操作
stmt.executeUpdate("update students set 姓名='"+str1+"',语文='"+f01+"',数学='"+f02+"',英语='"+f03+"',物理='"+f04+"',平均分='"+f05+"' where 学号='"+str+"'");
JOptionPane.showMessageDialog(null,"修改成功!","修改成功!",JOptionPane.INFORMATION_MESSAGE);
datadisplay(sql);
}
else JOptionPane.showMessageDialog(null,"未修改表格内容!","未修改表格内容!",JOptionPane.INFORMATION_MESSAGE);
}
catch(SQLException ee){//捕捉异常
System.out.println("SQLException:"+ee.getMessage());
}
}
if(e.getSource()==jButton6){
datadisplay("select * from Students order by 学号");
}
if(e.getSource()==jButton7||e.getSource()==v2){
averagedialog dlg=new averagedialog();
dlg.setTitle("学生平均分");
dlg.setBounds(260, 200,350, 300);
dlg.setVisible(true);
}
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if(e.getSource()==jLabel2){
String sql2="select * from students where 学号='"+t1.getText()+"'";
datadisplay(sql2);
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e){
if(e.getSource()==jButton1){
Label3.setText("添加");
Label3.setBackground(Color.GRAY);
Label3.setVisible(true);
}
if(e.getSource()==jButton2){
Label3.setText("删除");
Label3.setBackground(Color.GRAY);
Label3.setVisible(true);
}
if(e.getSource()==jButton3){
Label3.setText("成绩分布图");
Label3.setBackground(Color.GRAY);
Label3.setVisible(true);
}
if(e.getSource()==jButton4){
Label3.setBounds(260,65,73,20);
Label3.setText("成绩排序显示");
Label3.setBackground(Color.GRAY);
Label3.setVisible(true);
}
if(e.getSource()==jButton5){
Label3.setText("修改所填内容");
Label3.setBackground(Color.GRAY);
Label3.setVisible(true);
}
if(e.getSource()==jButton6){
Label3.setText("显示全部");
Label3.setBackground(Color.GRAY);
Label3.setVisible(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -