我會先細緻的說一下我的界面設計和功能實現以及遇到的問題,然後代碼會放到每塊的後面展示。
所有界面除了主界面(因爲主界面較簡單就用了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);//自由佈局
//省略下面的組件放置位置以及添加組件的代碼
}
}
寫的時候還不覺得什麼,當整體看起來就會發現很多重複的代碼,尤其是我幾乎每個字體都重新設置了,這就導致每設置一個就得寫一遍重複的,我就在網上查找是否有全局字體設置,找到了一篇博客,大家可以參考:點我哦~
歐克,有很多不足,繼續努力吧。
視頻講解(略有不同):嗶哩嗶哩~