我会先细致的说一下我的界面设计和功能实现以及遇到的问题,然后代码会放到每块的后面展示。
所有界面除了主界面(因为主界面较简单就用了BorderLayout),其余都用的自由布局:
setLayout(null);//自由布局,然后可所以设置组件的位置
setBounds(1, 2, 3, 4);//四个参数分别为横坐标,纵坐标,所占空间的长度,所占空间的宽度
主界面:
主界面我用到了左右分屏(分屏的方式:左右是HORIZONTAL_SPLIT,上下是VERTICAL_SPLIT):
JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,A,B);
//分屏的方式:左右是HORIZONTAL_SPLIT,上下是VERTICAL_SPLIT
jSplitPane.setDividerLocation(300);//左边占的长度
jSplitPane.setDividerSize(0);//分界线的宽度 设置为0 即不显示出分界线
添加图片:
ImageIcon im =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\d.png");//图片地址
JLabel a=new JLabel(im);//添加到JLabel里,然后就可以放到你想放的地方
package homework;
import java.awt.BorderLayout;//省略咯!!
public class 界面 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public 界面()
{
setTitle("登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,470);
this.setLocation(300,120);
JPanel A=new JPanel();
A.setBackground(Color.WHITE);
ImageIcon im =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\d.png");
JLabel a=new JLabel(im);
A.add(a);
JPanel B=new JPanel(new BorderLayout());
B.setBackground(Color.PINK);
JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,A,B);//分屏的方式:左右HORIZONTAL_SPLIT,上下VERTICAL_SPLIT
jSplitPane.setDividerLocation(300);//左边占的长度
this.add(jSplitPane);
jSplitPane.setDividerSize(0);//分界线的宽度 设置为0 即不显示出分界线
A.setBorder(BorderFactory.createLineBorder(Color.BLUE));
B.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JLabel w=new JLabel(" 我 是 ");
JButton g=new JButton("管理员");
JPanel G=new JPanel();
G.setBackground(Color.PINK);
G.add(g);
g.setContentAreaFilled(false);
JButton x=new JButton("学生");
JPanel X=new JPanel();
X.add(x);
X.setBackground(Color.PINK);
x.setContentAreaFilled(false);
B.add(w,BorderLayout.NORTH);
w.setHorizontalAlignment(SwingConstants.CENTER);//居中
w.setPreferredSize(new Dimension(0,150));//宽度150
w.setFont(new Font("楷体",Font.PLAIN,25));//设置字体的字体,样子,大小
B.add(X,BorderLayout.CENTER);
B.add(G,BorderLayout.SOUTH);
G.setPreferredSize(new Dimension(0,200));//宽度200
x.addActionListener(new ActionListener() //监听学生按钮
{
public void actionPerformed(ActionEvent e)
{
new XX();
}
});
g.addActionListener(new ActionListener() //监听管理员按钮
{
public void actionPerformed(ActionEvent e)
{
new GG();
}
});
}
public static void main(String[] args)
{
new 界面().setVisible(true);
}
}
学生登陆界面和管理员登陆界面:
两个图标在这里可自取:
按钮透明化和背景颜色:
setContentAreaFilled(false);
setBackground(Color.PINK);
public class ValidCode extends JComponent implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private String code;
private int width, height = 40;
private int codeLength = 4;
private Random random = new Random();
public ValidCode() {
width = this.codeLength * 16 + (this.codeLength - 1) * 10;
setPreferredSize(new Dimension(width, height));
setSize(width, height);
this.addMouseListener(this);
setToolTipText("点击可以更换验证码");
}
public int getCodeLength() {
return codeLength;
}
//设置验证码文字的长度
public void setCodeLength(int codeLength) {
if(codeLength < 4) {
this.codeLength = 4;
} else {
this.codeLength = codeLength;
}
}
public String getCode() {
return code;
}
// 产生随机的颜色
public Color getRandColor(int min, int max) {
if (min > 255)
min = 255;
if (max > 255)
max = 255;
int red = random.nextInt(max - min) + min;
int green = random.nextInt(max - min) + min;
int blue = random.nextInt(max - min) + min;
return new Color(red, green, blue);
}
// 设置验证码具体的字母是什么
protected String generateCode() {
char[] codes = new char[this.codeLength];
for (int i = 0, len = codes.length; i < len; i++) {
if (random.nextBoolean()) {
codes[i] = (char) (random.nextInt(26) + 65);
} else {
codes[i] = (char) (random.nextInt(26) + 97);
}
}
this.code = new String(codes);
return this.code;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(this.code == null || this.code.length() != this.codeLength) {
this.code = generateCode();
}
width = this.codeLength * 16 + (this.codeLength - 1) * 10;
super.setSize(width, height);
super.setPreferredSize(new Dimension(width, height));
Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);
g.setFont(mFont);
//绘制出验证码的背景的矩形轮廓
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(getRandColor(200, 250));
g2d.fillRect(0, 0, width, height);
g2d.setColor(getRandColor(180, 200));
g2d.drawRect(0, 0, width - 1, height - 1);
//绘制出验证码背景的线
int i = 0, len = 150;
for (; i < len; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int x1 = random.nextInt(width - 10) + 10;
int y1 = random.nextInt(height - 4) + 4;
g2d.setColor(getRandColor(180, 200));
g2d.drawLine(x, y, x1, y1);
}
//绘制出验证码的具体字母
i = 0; len = this.codeLength;
FontMetrics fm = g2d.getFontMetrics();
int base = (height - fm.getHeight())/2 + fm.getAscent();
for(;i<len;i++) {
int b = random.nextBoolean() ? 1 : -1;
g2d.rotate(random.nextInt(10)*0.01*b);
g2d.setColor(getRandColor(20, 130));
g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);
}
}
//下一个验证码
public void nextCode() {
generateCode();
repaint();
}
因为两个界面基本一样,仅展示学生登陆界面代码:
package homework;
import java.awt.Color;//省略咯!!
public class XX extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private ValidCode vcode = new ValidCode();
JTextField co;
static JTextField user;
JPasswordField pass;
JButton ok,register;
public XX()
{
super("学生登陆");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(450,350);
this.setLocationRelativeTo(null);
setVisible(true);
//将整体设为粉色背景
JPanel frame=new JPanel();
frame.setBackground(Color.PINK);
this.add(frame);
//账号、密码所代表的图形
Icon login = new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\1.png");
JLabel l= new JLabel();
l.setIcon(login);
Icon password = new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\2.png");
JLabel p= new JLabel();
p.setIcon(password);
JLabel code=new JLabel("验证码");
code.setFont(new Font("楷体",Font.BOLD,17));
user=new JTextField();
pass=new JPasswordField();
co=new JTextField();
ok=new JButton("登录");
ok.setContentAreaFilled(false);
register=new JButton("注册");
register.setContentAreaFilled(false);
l.setBounds(80, 50, 60, 40);
p.setBounds(80, 100, 60, 40);
code.setBounds(70, 150, 60, 40);
user.setBounds(150, 50, 150, 30);
pass.setBounds(150, 100, 150, 30);
co.setBounds(150, 150, 150, 30);
ok.setBounds(120, 220, 70, 30);
register.setBounds(250, 220, 70, 30);
vcode.setBounds(310, 145, 100, 40);
frame.setLayout(null);
frame.add(l);
frame.add(p);
frame.add(code);
frame.add(user);
frame.add(pass);
frame.add(co);
frame.add(ok);
frame.add(register);
frame.add(vcode);
register.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new ZC();
closeThis();
}
});
ok.addActionListener(new ActionListener() //监听登录按钮
{
public void actionPerformed(ActionEvent e)
{
String jusername=user.getText();
char s[]=pass.getPassword();
String jpassword=new String(s);
String coo=co.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载对应的jdbc驱动
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";//配置连接字符串
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);//创建数据库连接对象
Statement st=conn.createStatement();//创建SQL语句执行对象
Md5 md5 = new Md5();
String newString = md5.EncoderByMd5(jpassword);
String strSQL="(Select * from dbo.PY where ID='"+jusername+"'And PAWD='"+newString+"' )";
ResultSet rs=st.executeQuery(strSQL);
if(coo.isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
}
else
{
if(!isValidCodeRight()) {
JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
}
else {
if(rs.next())
{
new PY();
closeThis();
}
else
{
JOptionPane.showMessageDialog(null,"用户名不存在或密码错误","错误!",JOptionPane.ERROR_MESSAGE);
}
conn.close();
//关闭数据库连接
}
}
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public boolean isValidCodeRight() {
if(co == null) {
return false;
}else if(vcode == null) {
return true;
}else if(vcode.getCode() .equals(co.getText())) {
return true;
}else
return false;
}
public void closeThis()//关闭当前界面
{
this.dispose();
}
}
学生注册界面:
上传照片功能,这里我遇到了一个问题就是关于转义字符的问题,因为我要获取选中的图片的路径并将照片添加到JLabel里展示出来,当我们需要在字符串中使用普通的反斜杠时,就需要使用双反斜杠\\来表示;java编译器会将\\\\ 解释为 \\ ,而正则表达式里,双斜杠 \\ 代表单斜杠 \ ,这也就是为什么要用 \\\\ 表示 \ ,\\\\\\\\表示\\。
JFileChooser fileChooser = new JFileChooser("C:\\\\Users\\\\小甘同学\\\\Pictures");
//打开文件时显示的位置
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = fileChooser.showOpenDialog(fileChooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File filePath = fileChooser.getSelectedFile();//获取图片路径
System.out.println(filePath);
String f=filePath.getPath();
String filePath1=f.replaceAll("\\\\", "\\\\\\\\");
//将\转义为\\
ImageIcon p = new ImageIcon(filePath1);
photo.setIcon(p);
}
然后这里用到了数据库中的表来判断是否已经存在这个人,如果存在注册失败,不存在并信息填入符合要求则成功(照片则保存照片的路径)。
我看到视频弹幕有人吐槽来着,这里我想的确实不太周到!!我本来想的是账号不能重复,但是账号名称它不一定是名字,但是我图方便就都用的名字,所以给人一种名字重名了就不可以了,我现在也觉得有点矛盾了,所以改成学号不能重复最好。
(哭唧唧,我这里放的是没改之前的)
CREATE TABLE PY
( ID CHAR(9) PRIMARY KEY,
/*列级完整性约束条件,ID是主码 */
PAWD CHAR(35),
Sno CHAR(10),
phone CHAR(12),
);
CREATE TABLE image
( Sno CHAR(10),
ima Varchar(50),
);
这里的数据都是通过注册添加的,密码用了MD5加密,储存的也是加密后的:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class Md5 {
/**利用MD5进行加密*/
public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
//确定计算方法
MessageDigest md5=MessageDigest.getInstance("MD5");
BASE64Encoder base64en = new BASE64Encoder();
//加密后的字符串
String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));
return newstr;
}
}
package homework;
import java.awt.Color;//省略咯!!
public class ZC extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private ValidCode vcode = new ValidCode();
JTextField user,pass,idd,ph,co;
JButton pho,register,exit;
public ZC()
{
super("注册");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setLocationRelativeTo(null); //此窗口将置于屏幕的中央
setVisible(true);
JPanel frame=new JPanel();
frame.setBackground(Color.PINK);
this.add(frame);
JLabel username=new JLabel("账号");
JLabel password=new JLabel("密码");
JLabel id=new JLabel("学号");
JLabel phone=new JLabel("手机号码");
JLabel code=new JLabel("验证码");
JLabel photo=new JLabel();
user=new JTextField();
pass=new JTextField();
idd=new JTextField();
ph=new JTextField();
co=new JTextField();
pho=new JButton("上传照片");
register=new JButton("注 册");
exit=new JButton("退 出");
username.setBounds(58, 46, 60, 40);
username.setFont(new Font("楷体",Font.BOLD,17));
user.setBounds(100,50,120,30);
password.setBounds(54,100,120,30);
password.setFont(new Font("楷体",Font.BOLD,17));
pass.setBounds(100,100,120,30);
id.setBounds(54,150,120,30);
id.setFont(new Font("楷体",Font.BOLD,17));
idd.setBounds(100,150,120,30);
phone.setBounds(22,200,120,30);
phone.setFont(new Font("楷体",Font.BOLD,17));
ph.setBounds(100,200,120,30);
code.setBounds(41,250,120,30);
code.setFont(new Font("楷体",Font.BOLD,17));
co.setBounds(100,250,120,30);
vcode.setBounds(112, 300, 100, 40);
photo.setBounds(280,100,150,150);
pho.setBounds(305,280,100,30);
pho.setContentAreaFilled(false);
register.setBounds(150,380,70,30);
exit.setBounds(250,380,70,30);
frame.setLayout(null);
frame.add(username);
frame.add(user);
frame.add(password);
frame.add(pass);
frame.add(id);
frame.add(idd);
frame.add(phone);
frame.add(ph);
frame.add(code);
frame.add(co);
frame.add(photo);
frame.add(pho);
frame.add(register);
frame.add(exit);
frame.add(vcode);
pho.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fileChooser = new JFileChooser("C:\\\\Users\\\\小甘同学\\\\Pictures");
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = fileChooser.showOpenDialog(fileChooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File filePath = fileChooser.getSelectedFile();//获取图片路径
System.out.println(filePath);
String f=filePath.getPath();
String filePath1=f.replaceAll("\\\\", "\\\\\\\\"); //将\转义为\\
ImageIcon p = new ImageIcon(filePath1);
photo.setIcon(p);
}
}
});
register.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String user1=user.getText().trim();
String pass1=pass.getText().trim();
String id1=idd.getText().trim();
String ph1=ph.getText().trim();
String co1=co.getText();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
String strSQL="(insert * from dbo.PY where ID='"+user1+"' )";
ResultSet rs=st.executeQuery(strSQL);
Md5 md5 = new Md5();
String newString = md5.EncoderByMd5(pass1);
if(co1.isEmpty()) {
JOptionPane.showMessageDialog(null, "请输入验证码!","提示消息",JOptionPane.WARNING_MESSAGE);
}
else
{
if(!isValidCodeRight()) {
JOptionPane.showMessageDialog(null, "验证码错误,请重新输入!","提示消息",JOptionPane.WARNING_MESSAGE);
}
else {
if(rs.next())
{
JOptionPane.showMessageDialog(null,"用户名已存在","错误!", JOptionPane.ERROR_MESSAGE);
}
else
{
String sql = "insert into dbo.PY(ID,PAWD,Sno,phone) values('"+user1+"','"+newString+"','"+id1+"','"+ph1+"') ";
PreparedStatement pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
pst.executeUpdate();
pst.close();
JOptionPane.showMessageDialog(null,"注册成功");
}
conn.close();
//关闭数据库连接
}
}
}
catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
closeThis();
new XX();
}
});
}
public boolean isValidCodeRight() {
if(co == null) {
return false;
}else if(vcode == null) {
return true;
}else if(vcode.getCode() .equals(co.getText())) {
return true;
}else
return false;
}
public void closeThis()//关闭当前界面
{
this.dispose();
}
}
学生登陆界面:
功能简单介绍一下:
个人信息展示自己的信息和选课信息;
选课,如果选择已经有的科目则提示,如果没有你选的科目也提示,符合条件选课成功则提示成功;
查成绩,查的如果是不存在的课程或没选过的课程则提示,查询成功则展示成绩和等级。
界面首先是菜单栏的创建,基本上用到的就是下面这些:
MenuBar bar = new MenuBar();// 创建菜单栏
Menu fileMenu = new Menu("FILE");// 创建“文件”菜单
MenuItem open = new MenuItem("OPEN");//创建FILE里面的子菜单
fileMenu.add(open);//将子菜单添加到主菜单里
fileMenu.addSeparator();// 设置菜单分隔符
bar.add(fileMenu);// 将文件添加到菜单栏上
setMenuBar(bar);// 设置菜单栏,使用这种方式设置菜单栏可以不占用布局空间
然后就是整体的布局是选项卡面板(即点击不同的选项卡展示不同的面板)我用到的是选项卡垂直排列(默认是水平排列):
JTabbedPane jtbp; //定义选项卡
JPanel jp1,jp2,jp3; //定义面板,在面板上添加组件即可
jtbp=new JTabbedPane(JTabbedPane.LEFT); //创建选项卡并使选项卡垂直排列
jtbp.add("个人信息",jp1);
jtbp.add("选课",jp2);
jtbp.add("成绩查询",jp3);
jtbp.setFont(new Font("楷体",Font.PLAIN,30));//字体样子大小设置
this.add(jtbp); //添加选项卡窗格到容器
package homework;
import java.awt.Color;//省略咯!!
public class PY extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTabbedPane jtbp; //定义选项卡
JPanel jp1,jp2,jp3; //定义面板
public PY() throws SQLException, ClassNotFoundException
{
super("学生登陆");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setLocationRelativeTo(null);
setVisible(true);
MenuBar bar = new MenuBar();// 创建菜单栏
bar.setFont(new Font("楷体",Font.PLAIN,30));
Menu fileMenu = new Menu("FILE");// 创建“文件”菜单
fileMenu.setFont(new Font("楷体",Font.PLAIN,17));
MenuItem open = new MenuItem("OPEN");
MenuItem exit = new MenuItem("EXIT");
Menu help = new Menu("HELP");// 创建“帮助"菜单
help.setFont(new Font("楷体",Font.PLAIN,17));
MenuItem print = new MenuItem("PRINT");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
new XX();
closeThis();
}
});
fileMenu.add(print);
fileMenu.add(open);
fileMenu.addSeparator();// 设置菜单分隔符
fileMenu.add(exit);
bar.add(fileMenu);// 将文件添加到菜单栏上
bar.add(help);// 将文件添加到菜单栏上
setMenuBar(bar);// 设置菜单栏,使用这种方式设置菜单栏可以不占用布局空间
//创建组件
jp1= new JPanel();
jp2= new JPanel();
jp3= new JPanel();
jp1.setBackground(Color.WHITE);
jp2.setBackground(Color.WHITE);
jp3.setBackground(Color.WHITE);
//jp1面板上上的内容
String[][] datas = {};
String[] titles = { "学号", "姓名","性别","年龄","专业" };
String[][] datas1 = {};
String[] titles1 = { "课程号", "课程名","学分"};
DefaultTableModel myModel = new DefaultTableModel(datas, titles);// myModel存放表格的数据
DefaultTableModel myModel1 = new DefaultTableModel(datas1, titles1);
JTable table = new JTable(myModel);// 表格对象table的数据来源是myModel对象
JTable table1 = new JTable(myModel1);
table.setPreferredScrollableViewportSize(new Dimension(550, 100));// 表格的显示尺寸
table1.setPreferredScrollableViewportSize(new Dimension(550, 100));
// 产生一个带滚动条的面板
JScrollPane scrollPane = new JScrollPane(table);
JScrollPane scrollPane1 = new JScrollPane(table1);
//行高
table.setRowHeight(20);
table1.setRowHeight(20);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
String strSQL="(Select * from dbo.student where Sname='"+ XX.user.getText()+"')";
ResultSet rs=st.executeQuery(strSQL);
if(rs.next())
{
Vector<String> ve = new Vector<String>();
ve.addElement(rs.getString(1));
ve.addElement(rs.getString(2));
ve.addElement(rs.getString(3));
ve.addElement(rs.getString(4));
ve.addElement(rs.getString(5));
myModel.addRow(ve);
}
String s1="(Select * from dbo.student,dbo.SC where Sname='"+XX.user.getText()+"' And student.Sno=SC.Sno)";
ResultSet r1=st.executeQuery(s1);
if(r1.next())
{
String s2="(Select * from dbo.Course,dbo.SC where Sno='"+r1.getString(7)+"' And Course.Cno=SC.Cno)";
ResultSet r2=st.executeQuery(s2);
while(r2.next())
{
Vector<String> ve1 = new Vector<String>();
ve1.addElement(r2.getString(1));
ve1.addElement(r2.getString(2));
ve1.addElement(r2.getString(4));
myModel1.addRow(ve1);
}
}
ImageIcon im1 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\Saved Pictures\\\\1.png");
JLabel j=new JLabel(im1);
ImageIcon im2 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\1.gif");
JLabel j1=new JLabel(im2);
JButton again=new JButton("刷 新~");
again.setContentAreaFilled(false);
again.setFont(new Font("楷体",Font.BOLD,14));
again.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="jiaganyu";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
while(myModel1.getRowCount()>0)
{
myModel1.removeRow(myModel1.getRowCount()-1);
}
String s1="(Select * from dbo.student,dbo.SC where Sname='"+XX.user.getText()+"' And student.Sno=SC.Sno)";
ResultSet r1=st.executeQuery(s1);
if(r1.next())
{
String s2="(Select * from dbo.Course,dbo.SC where Sno='"+r1.getString(7)+"' And Course.Cno=SC.Cno)";
ResultSet r2=st.executeQuery(s2);
while(r2.next())
{
Vector<String> ve1 = new Vector<String>();
ve1.addElement(r2.getString(1));
ve1.addElement(r2.getString(2));
ve1.addElement(r2.getString(4));
myModel1.addRow(ve1);
}
conn.close();
}
}catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
//jp2面板上的内容
String[][] datas2 = {};
String[] titles2 = { "课程号", "课程名","学分" };
DefaultTableModel myModel2 = new DefaultTableModel(datas2, titles2);
JTable table2 = new JTable(myModel2);
table2.setRowHeight(20);
table2.setPreferredScrollableViewportSize(new Dimension(550, 400));
JScrollPane scrollPane2 = new JScrollPane(table2);
String s2="(Select * from dbo.Course)";
ResultSet r2=st.executeQuery(s2);
while(r2.next())
{
Vector<String> ve2 = new Vector<String>();
ve2.addElement(r2.getString(1));
ve2.addElement(r2.getString(2));
ve2.addElement(r2.getString(4));
myModel2.addRow(ve2);
}
conn.close();
JLabel a=new JLabel("请输入你想选的课的课程号:");
a.setFont(new Font("楷体",Font.BOLD,18));
JTextField b=new JTextField(20);
JButton c=new JButton("确定");
c.setFont(new Font("楷体",Font.BOLD,20));
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
String ok=b.getText().trim();
String s="(Select * from dbo.Course where Cno='"+ok+"' )";
ResultSet r=st.executeQuery(s);
if(r.next())
{
String s1="(Select * from dbo.student,dbo.SC where Sname='"+XX.user.getText()+"' And SC.Sno=student.Sno )";
ResultSet r1=st.executeQuery(s1);
if(r1.next())
{
String s2="(Select * from dbo.SC where Sno='"+r1.getString(1)+"' And Cno='"+ok+"' )";
ResultSet r2=st.executeQuery(s2);
if(r2.next())
{
JOptionPane.showMessageDialog(null, "你已经选过该科目了~","提示消息",JOptionPane.WARNING_MESSAGE);
}
else
{
String ss="(Select * from dbo.student where Sname='"+XX.user.getText()+"')";
ResultSet rr=st.executeQuery(ss);
if(rr.next())
{
String strSQL="insert into dbo.SC(Sno,Cno) values('"+rr.getString(1)+"','"+ok+"')";
int rr1=st.executeUpdate(strSQL);
if(rr1==1)
{
JOptionPane.showMessageDialog(null, "选课成功","提示消息",JOptionPane.WARNING_MESSAGE);
}
}
}
}
}
else
{
JOptionPane.showMessageDialog(null, "没有这种科目哦~","提示消息",JOptionPane.WARNING_MESSAGE);
}
conn.close();
}catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
//jp3上的内容
ImageIcon im3 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\f.gif");
JLabel j2=new JLabel(im3);
JLabel ja1=new JLabel("你想查询的科目是:");
JLabel ja2=new JLabel("你的成绩是:");
JLabel ja3=new JLabel("你的等级是:");
JLabel ja4=new JLabel("(输入课程号哦~)");
ja1.setFont(new Font("楷体",Font.BOLD,20));
ja2.setFont(new Font("楷体",Font.BOLD,20));
ja3.setFont(new Font("楷体",Font.BOLD,20));
ja4.setFont(new Font("楷体",Font.BOLD,15));
JTextField b1=new JTextField(15);
JTextField b2=new JTextField(15);
JTextField b3=new JTextField(15);
JButton c1=new JButton("查 询");
c1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
String B1=b1.getText().trim();
String L="(Select * from dbo.student,dbo.SC where Sname='"+XX.user.getText()+"' And Cno='"+B1+"' And SC.Sno=student.Sno )";
ResultSet M=st.executeQuery(L);
if(M.next())
{
b2.setText(M.getString(9));
b3.setText(M.getString(10));
}
else
{
JOptionPane.showMessageDialog(null, "没有该科目的成绩哦~","提示消息",JOptionPane.WARNING_MESSAGE);
}
conn.close();
}catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
jp1.setLayout(null);//自由布局
jp2.setLayout(null);//自由布局
jp3.setLayout(null);//自由布局
//jp1中组件的位置
scrollPane.setBounds(50, 190, 550, 70);
scrollPane1.setBounds(50, 290, 550, 100);
j.setBounds(250, 20, 150, 150);
j1.setBounds(180, 370, 300, 150);
again.setBounds(490, 140, 80, 30);
//jp2中组件的位置
scrollPane2.setBounds(50, 20, 550, 400);
a.setBounds(50, 470, 270, 30);
b.setBounds(320, 470, 150, 25);
c.setBounds(500, 470, 80, 27);
//jp3中组件的位置
j2.setBounds(430, 330, 200, 200);
ja1.setBounds(50, 50, 200, 30);
ja2.setBounds(80, 220, 150, 30);
ja3.setBounds(80, 270, 150, 30);
ja4.setBounds(255, 80, 150, 30);
b1.setBounds(260, 50, 150, 25);
b2.setBounds(260, 220, 100, 25);
b3.setBounds(260, 270, 100, 25);
c1.setBounds(450, 50, 70, 30);
// 将组件添加入jp1窗口中
jp1.add(scrollPane);
jp1.add(scrollPane1);
jp1.add(j);
jp1.add(j1);
jp1.add(again);
// 将组件添加入jp2窗口中
jp2.add(scrollPane2);
jp2.add(a);
jp2.add(b);
jp2.add(c);
// 将组件添加入jp3窗口中
jp3.add(j2);
jp3.add(ja1);
jp3.add(ja2);
jp3.add(ja3);
jp3.add(ja4);
jp3.add(b1);
jp3.add(b2);
jp3.add(b3);
jp3.add(c1);
jtbp=new JTabbedPane(JTabbedPane.LEFT); //创建选项卡并使选项卡垂直排列
jtbp.add("个人信息",jp1);
jtbp.add("选课",jp2);
jtbp.add("成绩查询",jp3);
jtbp.setFont(new Font("楷体",Font.PLAIN,30));
this.add(jtbp); //添加选项卡窗格到容器
}
public void closeThis()//关闭当前界面
{
this.dispose();
}
}
管理员界面:
功能简单介绍一下:
有全部信息展示,也可以通过查询学号来展示,某一个人的所有信息;
然后就是增删改功能,增加:增加新学生或增加新课程(只能添加新的,如果添加已存在同学则会提示,学生信息所有必须填写或者课程信息所有必须填写);删除:删除某个学生的某些信息或删除一门课程(我不允许退学的事情发生哈哈哈哈);修改:修改某位学生的某个信息或课程信息。
界面也是用到了分屏这个是上下分屏,并且没有隐藏分屏线,并且它可以上下移动调节。
然后说一下表格如何创建:
String[][] datas = {};
String[] titles = { "学号", "姓名","性别","年龄","专业" }; //创建表头
DefaultTableModel myModel = new DefaultTableModel(datas, titles);// myModel存放表格的数据
JTable table = new JTable(myModel);// 表格对象table的数据来源是myModel对象
table.setPreferredScrollableViewportSize(new Dimension(550, 100));// 表格的显示尺寸
JScrollPane scrollPane = new JScrollPane(table); // 产生一个带滚动条的面板
table.setRowHeight(20);//行高
jp1.add(scrollPane);//然后添加到你要添加到的面板里即可
这里用到的表为SC,Student,Course表:
CREATE TABLE SC
(Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY(Sno,Cno),
/*主码由两个属性构成,必须作为表级完整性进行定义 */
FOREIGN KEY(Sno) REFERENCES Student(Sno),
/*表级完整性约束条件,Sno是外码,被参照表是Student */
FOREIGN KEY(Cno) REFERENCES Course(Cno)
/*表级完整性约束条件,Cno是外码,被参照表是Course */
);
CREATE TABLE Student
( Sno CHAR(9) PRIMARY KEY,
/*列级完整性约束条件,Sno是主码 */
Sname CHAR(20) UNIQUE,
/*Sname取唯一值 */
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
CREATE TABLE Course
(Cno CHAR(4) PRIMARY KEY,
/* 列级完整性约束条件,Cno是主码*/
Cname CHAR(40) NOT NULL,
/* 列级完整性约束条件,Cname不能取空值 */
Cpno CHAR(4),
/ * Cpno的含义是先修课 */
Ccredit SMALLINT,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
/*表级完整性约束条件,Cpno是外码,被参照表是Course,被参照列是Cno */
);
管理员界面主要就是表格的建立和信息展示,所以只展示全部信息展示那里的代码(别的也都是重复这些):
component8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
//清空之前的数据
while(myModel.getRowCount()>0)
{
myModel.removeRow(myModel.getRowCount()-1);
}
while(myModel1.getRowCount()>0)
{
myModel1.removeRow(myModel1.getRowCount()-1);
}
while(myModel2.getRowCount()>0)
{
myModel2.removeRow(myModel2.getRowCount()-1);
}
while(myModel3.getRowCount()>0)
{
myModel3.removeRow(myModel3.getRowCount()-1);
}
String strSQL="(Select * from dbo.student)";
ResultSet rs=st.executeQuery(strSQL);
while(rs.next())
{
Vector<String> v = new Vector<String>();
v.addElement(rs.getString(1));
v.addElement(rs.getString(2));
v.addElement(rs.getString(3));
v.addElement(rs.getString(4));
v.addElement(rs.getString(5));
myModel.addRow(v);
}
String strSQL1="(Select * from dbo.Course)";
ResultSet rs1=st.executeQuery(strSQL1);
while(rs1.next())
{
Vector<String> v1 = new Vector<String>();
v1.addElement(rs1.getString(1));
v1.addElement(rs1.getString(2));
v1.addElement(rs1.getString(4));
myModel1.addRow(v1);
}
String strSQL2="(Select * from dbo.SC)";
ResultSet rs2=st.executeQuery(strSQL2);
while(rs2.next())
{
Vector<String> v2 = new Vector<String>();
v2.addElement(rs2.getString(1));
v2.addElement(rs2.getString(2));
v2.addElement(rs2.getString(3));
v2.addElement(rs2.getString(4));
myModel2.addRow(v2);
}
String strSQL3="(Select * from dbo.PY)";
ResultSet rs3=st.executeQuery(strSQL3);
while(rs3.next())
{
Vector<String> v3 = new Vector<String>();
v3.addElement(rs3.getString(1));
v3.addElement(rs3.getString(2));
v3.addElement(rs3.getString(3));
v3.addElement(rs3.getString(4));
myModel3.addRow(v3);
}
conn.close();
}catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
增加信息:
package homework;
import java.awt.Color;//省略咯!!
public class add extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public add() {
super("添加信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(500,550);
this.setLocationRelativeTo(null);
setVisible(true);
JPanel frame=new JPanel();
frame.setBackground(Color.PINK);
this.add(frame);
JLabel j=new JLabel("学号:"); j.setFont(new Font("楷体",Font.PLAIN,20));//设置字体的字体,样子,大小
JLabel j1=new JLabel("姓名:");j1.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j2=new JLabel("性别:");j2.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j3=new JLabel("年龄:");j3.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j4=new JLabel("专业:");j4.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j5=new JLabel("课程:");j5.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j6=new JLabel("成绩:");j6.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j7=new JLabel("等级:");j7.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j11=new JLabel("课程号:");j11.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j12=new JLabel("课程名:");j12.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j13=new JLabel("学分:"); j13.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j8=new JLabel("(温馨提醒:添加学生信息学号必填哦~,右边一列为课程信息)");
j8.setFont(new Font("宋体",Font.PLAIN,11));
JButton aa=new JButton("确定");
aa.setFont(new Font("楷体",Font.PLAIN,20));
aa.setBackground(Color.GREEN);
JButton bb=new JButton("重置");
bb.setFont(new Font("楷体",Font.PLAIN,20));
bb.setBackground(Color.RED);
ImageIcon im1 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\a.gif");
JLabel i=new JLabel(im1);
ImageIcon im2 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\b.gif");
JLabel i1=new JLabel(im2);
JLabel i2=new JLabel(im2);
JLabel i3=new JLabel(im2);
JTextField c=new JTextField(15);//学号
JTextField c1=new JTextField(15);//姓名
JTextField c3=new JTextField(15);//年龄
JTextField c11=new JTextField(15);//课程号
JTextField c12=new JTextField(15);//课程名
JTextField c13=new JTextField(15);//学分
JTextField c2=new JTextField(15);//性别
JTextField c4=new JTextField(15);//专业
JTextField c7=new JTextField(15);//等级
JTextField c5=new JTextField(20);//课程
JTextField c6=new JTextField(20);//成绩
aa.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
String a=c.getText().trim(); String a1=c1.getText().trim();
String a2=c3.getText().trim(); String a3=c5.getText().trim();
String a4=c6.getText().trim(); String a11=c11.getText().trim();
String a12=c12.getText().trim();String a13=c13.getText().trim();
String a5=c2.getText().trim(); String a6=c4.getText().trim();
String a7=c7.getText().trim();
String s="(Select * from dbo.Student where Sno='"+a+"')";
ResultSet r=st.executeQuery(s);
if(r.next())
{
JOptionPane.showMessageDialog(null, "该同学已存在哦~","提示消息",JOptionPane.WARNING_MESSAGE);
}
else
{
if(a==null)
{
String s3="insert into dbo.Course(Cno,Cname,Ccredit) values('"+a11+"','"+a12+"','"+a13+"')";
int r3=st.executeUpdate(s3);
if(r3==1)
{
JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
}
}
else
{
String s1="insert into dbo.student(Sno,Sname,Ssex,Sage,Sdept) values('"+a+"','"+a1+"','"+a5+"','"+a2+"','"+a6+"')";
int r1=st.executeUpdate(s1);
String s2="insert into dbo.SC(Sno,Cno,Grade,LEVEL) values('"+a3+"','"+a4+"','"+a7+"')";
int r2=st.executeUpdate(s2);
if(r1==1&&r2==1)
{
JOptionPane.showMessageDialog(null, "添加成功","提示消息",JOptionPane.WARNING_MESSAGE);
}
}
}
conn.close();
}catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
//重置清零
bb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
c.setText("");
c1.setText("");
c2.setText("");
c3.setText("");
c4.setText("");
c5.setText("");
c6.setText("");
c11.setText("");
c12.setText("");
c13.setText("");
}
});
frame.setLayout(null);//自由布局
j.setBounds(20, 30, 50, 20); c.setBounds(80, 30, 120, 25);
j1.setBounds(20, 70, 50, 20);c1.setBounds(80, 70, 100, 25);
j2.setBounds(20, 110, 50, 30);c2.setBounds(80, 110, 100, 25);
j3.setBounds(20, 150, 50, 30);c3.setBounds(80, 150, 100, 25);
j4.setBounds(20, 190, 50, 30);c4.setBounds(80, 190, 100, 25);
j5.setBounds(20, 230, 50, 30);c5.setBounds(80, 230, 100, 25);
j6.setBounds(20, 270, 50, 30);c6.setBounds(80, 270, 100, 25);
j7.setBounds(20, 310, 50, 30); c7.setBounds(80, 310, 100, 25);
j11.setBounds(300, 120, 70, 30); c11.setBounds(380, 120, 100, 25);
j12.setBounds(300, 170, 70, 30); c12.setBounds(380, 170, 100, 25);
j13.setBounds(300, 230, 70, 30); c13.setBounds(380, 230, 100, 25);
aa.setBounds(100, 400, 100, 30); bb.setBounds(300, 400, 100, 30);
j8.setBounds(10, 480, 450, 15);
i.setBounds(220,330,60,60); i1.setBounds(220,250,60,60);
i2.setBounds(220,170,60,60);i3.setBounds(220,90,60,60);
frame.add(j); frame.add(j1); frame.add(j2);
frame.add(j3); frame.add(j4); frame.add(j5);
frame.add(j6); frame.add(j7); frame.add(j11);
frame.add(j12);frame.add(j13);frame.add(c11);
frame.add(c12);frame.add(c13);frame.add(c);
frame.add(c1); frame.add(c2); frame.add(c3);
frame.add(c4); frame.add(c5); frame.add(c6);
frame.add(c7); frame.add(aa); frame.add(bb);
frame.add(j8); frame.add(i); frame.add(i1);
frame.add(i2); frame.add(i3);
}
}
删除信息:
package homework;
import java.awt.Color;//省略咯!!
public class delete extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public delete()
{
super("删除信息");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(500,550);
this.setLocationRelativeTo(null);
setVisible(true);
JPanel frame=new JPanel();
frame.setBackground(Color.PINK);
this.add(frame);
JCheckBox optionA=new JCheckBox(" 姓名");
JCheckBox optionB=new JCheckBox(" 性别");
JCheckBox optionC=new JCheckBox(" 年龄");
JCheckBox optionD=new JCheckBox(" 专业");
JCheckBox optionE=new JCheckBox(" 成绩");
JCheckBox optionF=new JCheckBox(" 等级");
optionA.setContentAreaFilled(false); optionA.setFont(new Font("楷体",Font.PLAIN,20));
optionB.setContentAreaFilled(false); optionB.setFont(new Font("楷体",Font.PLAIN,20));
optionC.setContentAreaFilled(false); optionC.setFont(new Font("楷体",Font.PLAIN,20));
optionD.setContentAreaFilled(false); optionD.setFont(new Font("楷体",Font.PLAIN,20));
optionE.setContentAreaFilled(false); optionE.setFont(new Font("楷体",Font.PLAIN,20));
optionF.setContentAreaFilled(false); optionF.setFont(new Font("楷体",Font.PLAIN,20));
JLabel j=new JLabel("学号:");
j.setFont(new Font("楷体",Font.PLAIN,20));//设置字体的字体,样子,大小
JTextField c1=new JTextField(12);
JLabel cc=new JLabel("课程号:");cc.setFont(new Font("楷体",Font.PLAIN,20));
JTextField c=new JTextField(12);
JLabel j11=new JLabel("课程号:");j11.setFont(new Font("楷体",Font.PLAIN,20));
JTextField c11=new JTextField(10);
JLabel j12=new JLabel("课程名:");j12.setFont(new Font("楷体",Font.PLAIN,20));
JTextField c12=new JTextField(10);
JLabel j1=new JLabel("(温馨提醒:谨慎删除哦~)");j1.setFont(new Font("宋体",Font.PLAIN,12));
JButton aa=new JButton("确定"); aa.setFont(new Font("楷体",Font.PLAIN,20));
aa.setBackground(Color.GREEN);
JButton bb=new JButton("重置"); bb.setFont(new Font("楷体",Font.PLAIN,20));
bb.setBackground(Color.RED);
aa.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SCHOOL";
String user="sa";//sa超级管理员
String password="你的密码";//密码
Connection conn=DriverManager.getConnection(url,user,password);
Statement st=conn.createStatement();
String a=c1.getText().trim();
String a1=c.getText().trim();
String s="(Select * from dbo.Student where Sno='"+a+"')";
ResultSet r=st.executeQuery(s);
if(r.next())
{
if(optionA.isSelected())
{
String strSQL="update dbo.Student set Sname="+null+" where Sno='"+a+"'";
}
if(optionB.isSelected())
{
String strSQL="update dbo.Student set Ssex="+null+" where Sno='"+a+"'";
}
if(optionC.isSelected())
{
String strSQL="update dbo.Student set Sage="+null+" where Sno='"+a+"'";
}
if(optionD.isSelected())
{
String strSQL="update dbo.Student set Sdept="+null+" where Sno='"+a+"'";
}
if(optionE.isSelected())
{
String strSQL="update dbo.SC set Grade="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
}
if(optionF.isSelected())
{
String strSQL="update dbo.SC set LEVEL="+null+" where Sno='"+a+"' And Cno='"+a1+"'";
}
JOptionPane.showMessageDialog(null,"删除成功哦~");
}
else
{
String cc=c11.getText().trim();
String strSQL="delete from dbo.Course where Cno='"+cc+"' ";
int rr=st.executeUpdate(strSQL);
if(rr==1)
{
JOptionPane.showMessageDialog(null,"删除成功哦~");
}
else
{
JOptionPane.showMessageDialog(null,"课程不存在哦~");
}
}
conn.close();
}catch (ClassNotFoundException ex) {
System.out.println("没有找到对应的数据库驱动类");
}
catch (SQLException ex) {
System.out.println("数据库连接或者是数据库操作失败");
}
}
});
//重置清零
bb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
c1.setText("");
c11.setText("");
c12.setText("");
optionA.setSelected(false);
optionB.setSelected(false);
optionC.setSelected(false);
optionD.setSelected(false);
optionE.setSelected(false);
optionF.setSelected(false);
}
});
ImageIcon im1 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\c.gif");
JLabel i=new JLabel(im1);
ImageIcon im2 =new ImageIcon("C:\\\\Users\\\\小甘同学\\\\Pictures\\\\b.gif");
JLabel i1=new JLabel(im2);
JLabel i2=new JLabel(im2);
JLabel i3=new JLabel(im2);
frame.setLayout(null);//自由布局
//省略下面的组件放置位置以及添加组件的代码
}
}
写的时候还不觉得什么,当整体看起来就会发现很多重复的代码,尤其是我几乎每个字体都重新设置了,这就导致每设置一个就得写一遍重复的,我就在网上查找是否有全局字体设置,找到了一篇博客,大家可以参考:点我哦~
欧克,有很多不足,继续努力吧。
视频讲解(略有不同):哔哩哔哩~