📄 managesystem.java
字号:
//设置按钮是否禁用
private void setButton(boolean add,boolean save,boolean reset,boolean quit)
{
btAdd.setEnabled(add);
btSave.setEnabled(save);
btReset.setEnabled(reset);
btQuit.setEnabled(quit);
}
//将文本框清空
private void setNull()
{
tfNum.setText(null);
tfName.setText(null);
tfSex.setText(null);
tfPlace.setText(null);
tfBirthday.setText(null);
tfDepartment.setText(null);
}
}
class add extends JFrame implements ActionListener
{
DBManage db=new DBManage(); //定义数据库操作对象
ResultSet rs;
String Sql; //定义字符串对象,代表sql语句
JFrame f=new JFrame("添加成绩");
Container cp=f.getContentPane();
JPanel pa1=new JPanel(new GridLayout(6,2,5,5)); //创建面板pa1并设置布局为6行2列
JPanel pa2=new JPanel(new GridLayout(1,4,5,5)); //创建面板pa2并设置布局为1行4列
//创建标签对象
JLabel lb1=new JLabel("学号");
JLabel lb2=new JLabel("姓名");
JLabel lb3=new JLabel("语文");
JLabel lb4=new JLabel("数学");
JLabel lb5=new JLabel("英语");
JLabel lb6=new JLabel("总分");
//创建文本框对象
JTextField tfNum=new JTextField(10);
JTextField tfName=new JTextField(10);
JTextField tfChinese=new JTextField(10);
JTextField tfMath=new JTextField(10);
JTextField tfEnglish=new JTextField(10);
JTextField tfTotal=new JTextField(10);
//创建按钮对象
JButton btAdd=new JButton("添加");
JButton btSave=new JButton("保存");
JButton btReset=new JButton("重置");
JButton btQuit=new JButton("退出");
Dimension dim=new Dimension(60,20);
public add()
{
cp.setLayout(new GridLayout(2,1,5,5));
cp.add(pa1); //将pa1面板放入容器中
pa1.add(lb1);
pa1.add(tfNum);
pa1.add(lb2);
pa1.add(tfName);
pa1.add(lb3);
pa1.add(tfChinese);
pa1.add(lb4);
pa1.add(tfMath);
pa1.add(lb5);
pa1.add(tfEnglish);
pa1.add(lb6);
pa1.add(tfTotal);
cp.add(pa2); //添加面板pa2
btAdd.setMaximumSize(dim);
btAdd.setMinimumSize(dim);
btAdd.setPreferredSize(dim);
pa2.add(btAdd);
btSave.setMaximumSize(dim);
btSave.setMinimumSize(dim);
btSave.setPreferredSize(dim);
pa2.add(btSave);
btReset.setMaximumSize(dim);
btReset.setMinimumSize(dim);
btReset.setPreferredSize(dim);
pa2.add(btReset);
btQuit.setMaximumSize(dim);
btQuit.setMinimumSize(dim);
btQuit.setPreferredSize(dim);
pa2.add(btQuit);
//注册监听者对象
btAdd.addActionListener(this);
btSave.addActionListener(this);
btReset.addActionListener(this);
btQuit.addActionListener(this);
f.pack();
f.setVisible(true);
f.setSize(400,400);
//f.setBackground(Color.cyan); //设置背景颜色
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//初始化时设置按钮的可用性
setButton(true,false,false,true);
//初始化时设置文本框不可编辑
setTextField(false,false,false,false,false,false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btAdd)
{
setNull();
setTextField(true,true,true,true,true,true);
setButton(false,true,true,true);
}
if(e.getSource()==btSave)
{
//判断学号是否为空
if(tfNum.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(f,"学号不可为空!");
return;
}
//判断姓名是否为空
if(tfName.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(f,"姓名不可为空!");
return;
}
Sql="select * from student where 学号='"+tfNum.getText().trim() +"' ";
//首先查询新增的学号在student表中是否存在
try{
rs=db.query(Sql);
if(!rs.next())
{
JOptionPane.showMessageDialog(f,"学号不存在!");
return;
}
else
{
Sql=" select * from grade where 学号='"+tfNum.getText().trim() +"' ";
rs=db.query(Sql);
//查询当前学号,若不为空,说明grade表中此学号已存在
if(rs.next())
{
JOptionPane.showMessageDialog(f,"该学生的成绩已经存在,请重新输入!");
return;
}
else{
Sql=" insert into grade values(";
Sql=Sql+ " '" +tfNum.getText().trim()+ "', "; Sql=Sql+ " '" +tfName.getText().trim()+ "', ";
Sql=Sql+ " '" +tfChinese.getText().trim()+ "', ";
Sql=Sql+ " '" +tfMath.getText().trim()+ "', ";
Sql=Sql+ " '" +tfEnglish.getText().trim()+ "', ";
Sql=Sql+ " '" +tfTotal.getText().trim()+ "' ) ";
if(db.executeSQL(Sql))
{
JOptionPane.showMessageDialog(f,"添加成功!");
setNull();
setButton(true,false,false,true);
}
else
{
JOptionPane.showMessageDialog(f,"添加失败,请重新操作!");
}
}
}
}
catch(SQLException sqle)
{ System.out.println("SQLException异常:"+sqle.getMessage()); }
}
if(e.getSource()==btReset)
{
setNull();
}
if(e.getSource()==btQuit)
{
f.dispose();
}
}
//设置文本框的可编辑性
private void setTextField(boolean num,boolean name,boolean Chinese,boolean Math,boolean English,boolean Total)
{
tfNum.setEditable(num);
tfName.setEditable(name);
tfChinese.setEditable(Chinese);
tfMath.setEditable(Math);
tfEnglish.setEditable(English);
tfTotal.setEditable(Total);
}
//设置按钮是否禁用
private void setButton(boolean add,boolean save,boolean reset,boolean quit)
{
btAdd.setEnabled(add);
btSave.setEnabled(save);
btReset.setEnabled(reset);
btQuit.setEnabled(quit);
}
//将文本框清空
private void setNull()
{
tfNum.setText(null);
tfName.setText(null);
tfChinese.setText(null);
tfMath.setText(null);
tfEnglish.setText(null);
tfTotal.setText(null);
}
// public static void main(String args[])
// {
// new add();
// }
}
public class ManageSystem extends JFrame implements ActionListener
{
//JFrame.setDefaultLookAndFeelDecorated(true); //设置窗体图标显示为默认方式
JFrame f=new JFrame("学生成绩管理系统"); //创建JFrame对象
Container cp=f.getContentPane(); //创建JFrame的容器对象
JMenuBar bar=new JMenuBar(); //创建菜单栏对象
//创建menuSystem菜单及其菜单项
JMenu menuA=new JMenu("系统管理");
JMenuItem quit=new JMenuItem("退出");
//创建management菜单及其菜单项
JMenu menuB=new JMenu("成绩管理");
JMenuItem addInfo=new JMenuItem("添加");
JMenuItem delInfo=new JMenuItem("删除");
JMenuItem updInfo=new JMenuItem("修改");
//创建search菜单及其菜单项
JMenu menuC=new JMenu("查询");
JMenuItem searchInfo=new JMenuItem("成绩查询");
JMenuItem searchInfo1=new JMenuItem("学生信息查询");
JMenu menuD=new JMenu("学生管理");
JMenuItem addInfo1=new JMenuItem("添加");
JMenuItem delInfo1=new JMenuItem("删除");
JMenuItem updInfo1=new JMenuItem("修改");
public mainframe()
{
f.setJMenuBar(bar); //加入菜单栏bar到JFrame中
menuA.add(quit);
bar.add(menuA);
menuB.add(addInfo);
menuB.add(delInfo);
menuB.add(updInfo);
bar.add(menuB);
menuD.add(addInfo1);
menuD.add(delInfo1);
menuD.add(updInfo1);
bar.add(menuD);
menuC.add(searchInfo);
menuC.add(searchInfo1);
bar.add(menuC);
f.pack();
f.setVisible(true);
f.setSize(800,800);
//f.setResizable(false);
//f.setBackground(Color.cyan); //设置背景颜色
f.addWindowListener(new WinLis());
//注册JMenuItem对象给监听者对象
quit.addActionListener(this);
addInfo.addActionListener(this);
delInfo.addActionListener(this);
updInfo.addActionListener(this);
addInfo1.addActionListener(this);
delInfo1.addActionListener(this);
updInfo1.addActionListener(this);
searchInfo.addActionListener(this);
searchInfo1.addActionListener(this);
}
class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public void actionPerformed(ActionEvent e)
{//flag2 flag1=new flag2();
if(e.getSource()==quit)
{
System.exit(0);
}
else if(e.getSource()==addInfo)
{
add addgrade=new add();
//addgrade.setLocation(200,160);
//addgrade.pack();
//addgrade.show();
}
else if(e.getSource()==delInfo)
{
delete delgrade=new delete();
//delgrade.setLocation(200,160);
//delgrade.pack();
//delgrade.show();
}
else if(e.getSource()==updInfo)
{
update updgrade=new update();
//updgrade.setLocation(200,160);
//updgrade.pack();
//updgrade.show();
}
else if(e.getSource()==addInfo1)
{
add1 addgrade=new add1();
//addgrade.setLocation(200,160);
//addgrade.pack();
//addgrade.show();
}
else if(e.getSource()==delInfo1)
{
// flag1.flag=1;
delete1 delgrade=new delete1();
//delgrade.setLocation(200,160);
//delgrade.pack();
//delgrade.show();
}
else if(e.getSource()==updInfo1)
{
update1 updgrade=new update1();
//updgrade.setLocation(200,160);
//updgrade.pack();
//updgrade.show();
}
else if(e.getSource()==searchInfo)
{
search searchgrade=new search();
//searchgrade.setLocation(200,160);
//searchgrade.pack();
//searchgrade.show();
}
else if(e.getSource()==searchInfo1)
{
search1 searchgrade1=new search1();
//searchgrade.setLocation(200,160);
//searchgrade.pack();
//searchgrade.show();
}
}
public static void main(String args[])
{
new mainframe();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -