⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stuframe.java~214~

📁 学生管理系统
💻 JAVA~214~
📖 第 1 页 / 共 2 页
字号:
package xxglxt;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import sun.jdbc.odbc.OdbcDef;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.awt.Image;
//學生管理
public class StuFrame extends JPanel {
    //——————定義窗體裏的對象
    String Url=" ";
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JLabel jLabel5 = new JLabel();
    JLabel jLabel6 = new JLabel();
    JLabel jLabel7 = new JLabel();
    JLabel jLabel8 = new JLabel();
    JLabel jLabel9 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JComboBox jComboBox1 = new JComboBox();
    JComboBox jComboBox2 = new JComboBox();
    JComboBox jComboBox3 = new JComboBox();
    JTextField jTextField3 = new JTextField();
    JTextField jTextField4 = new JTextField();
    JTextField jTextField5 = new JTextField();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextArea jTextArea1 = new JTextArea();
    JLabel jLabel10 = new JLabel();
    JLabel jLabel11 = new JLabel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JLabel jLabel12 = new JLabel();
    TitledBorder titledBorder1 = new TitledBorder("");
    JButton jButton3 = new JButton();
    JButton jButton4 = new JButton();
    JButton jButton5 = new JButton();
    JButton jButton6 = new JButton();
    JComboBox jComboBox4 = new JComboBox();
    String strtemp = ""; //用來存放暫時的字符串
     String strtemp2 = ""; //用來存放暫時的字符串
     int inttemp = 0; //用來存放暫時的數字
     ResultSet result=null; //保存結果集
     Connection con = null; //連接
     PreparedStatement pre; //SQL語句處理
    String sql = ""; //SQL語句
    String xh="";
    JTextField jTextField2 = new JTextField();
//------------------------------------------------------------------------
    FileDialog fileDialog;//文件對話框
//學生窗體
    public StuFrame() {
        try {
            jbInit();//初始化窗體對象
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
//初始化窗體裏的對象
    private void jbInit() throws Exception {
        this.setLayout(null);
        jLabel1.setText("姓名:");
        jLabel1.setBounds(new Rectangle(21, 18, 64, 15));
        jLabel2.setText("學號:");
        jLabel2.setBounds(new Rectangle(181, 16, 69, 27));
        jLabel3.setText("班級:");
        jLabel3.setBounds(new Rectangle(21, 132, 70, 36));
        jLabel4.setText("學曆:");
        jLabel4.setBounds(new Rectangle(21, 48, 79, 45));
        jLabel5.setText("性別:");
        jLabel5.setBounds(new Rectangle(181, 53, 63, 26));
        jLabel6.setText("生日:");
        jLabel6.setBounds(new Rectangle(181, 149, 48, 20));
        jLabel7.setText("電話:");
        jLabel7.setBounds(new Rectangle(21, 95, 69, 33));
        jLabel8.setText("地址:");
        jLabel8.setBounds(new Rectangle(181, 100, 74, 23));
        jLabel9.setText("特點:");
        jLabel9.setBounds(new Rectangle(21, 178, 38, 24));
        jTextField1.setEnabled(false);
        jTextField1.setBounds(new Rectangle(77, 18, 77, 20));
        jComboBox1.setBounds(new Rectangle(79, 145, 81, 23));
        jComboBox1.addActionListener(new StuFrame_jComboBox1_actionAdapter(this));
        jComboBox1.addItem("");
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //驅動加載
           con = DriverManager.getConnection("jdbc:odbc:glxt"); //數據橋連名子為ODBC名為glxt
            sql = "select class_name from classtable";//查詢班級名稱
            pre = con.prepareStatement(sql);
            result = pre.executeQuery();
            jComboBox1.removeAllItems();//清空下拉列表
            jComboBox1.addItem(" ");//在下拉列表中加一個空的對象
            while (result.next()) {
                jComboBox1.addItem(result.getString(1));//將班級名加入到下拉列表中
            }
            result.close();
            pre.close();
            con.close();

        } catch (ClassNotFoundException ex) {
            //驅程不存在
            ex.printStackTrace();
             JOptionPane.showMessageDialog(null, "驅程不存在");
        } catch (SQLException ex1) {
            //ODBC連接失敗
            ex1.printStackTrace();
            JOptionPane.showMessageDialog(null, "ODBC連接失敗");
        }
    //初始化窗體對象
        jComboBox2.setEnabled(false);
        jComboBox2.setBounds(new Rectangle(77, 58, 80, 20));
        jComboBox3.setEnabled(false);
        jComboBox3.setBounds(new Rectangle(228, 58, 81, 23));
        jTextField3.setEnabled(false);
        jTextField3.setBounds(new Rectangle(230, 145, 90, 22));
        jTextField3.addActionListener(new StuFrame_jTextField3_actionAdapter(this));
        jTextField4.setEnabled(false);
        jTextField4.setBounds(new Rectangle(77, 100, 86, 23));
        jTextField5.setEnabled(false);
        jTextField5.setBounds(new Rectangle(228, 100, 94, 22));
        jScrollPane1.setBounds(new Rectangle(74, 182, 242, 98));
        jLabel10.setText("照片:");
        jLabel10.setBounds(new Rectangle(360, 17, 44, 25));
        jLabel11.setBorder(BorderFactory.createLineBorder(Color.black));
        jLabel11.setBounds(new Rectangle(382, 42, 125, 145));
        jButton1.setBounds(new Rectangle(383, 197, 64, 26));
        jButton1.setText("增加");
        jButton1.addActionListener(new StuFrame_jButton1_actionAdapter(this));
        jButton2.setBounds(new Rectangle(446, 196, 70, 27));
        jButton2.setText("刪除");
        jButton2.addActionListener(new StuFrame_jButton2_actionAdapter(this));
        jLabel12.setBorder(titledBorder1);
        jLabel12.setText("注:如要刪除學生,輸入學號即可。");
        jLabel12.setBounds(new Rectangle(325, 230, 235, 44));
        jButton3.setBounds(new Rectangle(323, 197, 61, 26));
        jButton3.setEnabled(false);
        jButton3.setText("清除");
        jButton3.addActionListener(new StuFrame_jButton3_actionAdapter(this));
        jButton4.setBounds(new Rectangle(515, 196, 65, 27));
        jButton4.setText("修改");
        jButton4.addActionListener(new StuFrame_jButton4_actionAdapter(this));
        jButton5.setBounds(new Rectangle(402, 19, 93, 20));
        jButton5.setEnabled(false);
        jButton5.setText("更改圖片");
        jButton5.addActionListener(new StuFrame_jButton5_actionAdapter(this));
        jButton6.setBounds(new Rectangle(323, 146, 44, 20));
        jButton6.setEnabled(false);
        jButton6.setText("+");
        jButton6.addActionListener(new StuFrame_jButton6_actionAdapter(this));//加載日期對話框
        jComboBox4.setBounds(new Rectangle(228, 18, 93, 23));
        jComboBox4.addActionListener(new StuFrame_jComboBox4_actionAdapter(this));
        jTextArea1.setEnabled(false);
        jTextField2.setBounds(new Rectangle(228, 18, 93, 24));
        this.add(jLabel1, null);
        this.add(jTextField1);
        this.add(jLabel2);
        this.add(jLabel4);
        //加載學曆下拉列表
        jComboBox2.addItem("");
        jComboBox2.addItem("小學");
        jComboBox2.addItem("初中");
        jComboBox2.addItem("高中");
        jComboBox2.addItem("中專");
        jComboBox2.addItem("大專");
        jComboBox2.addItem("大本");
        jComboBox2.addItem("研究生");
        //——————————
        this.add(jComboBox2);
        this.add(jLabel5);
        //——————加載性別
        jComboBox3.addItem("");
        jComboBox3.addItem("男");
        jComboBox3.addItem("女");
        //————————
        this.add(jComboBox3);
        this.add(jLabel7);
        this.add(jTextField4);
        this.add(jLabel8);
        this.add(jTextField5);
        this.add(jLabel3);
        this.add(jLabel6);
        this.add(jLabel9);
        this.add(jScrollPane1);
        this.add(jComboBox1);
        this.add(jTextField3);
        this.add(jLabel10);
        this.add(jLabel11);
        this.add(jLabel12);
        this.add(jButton3);
        this.add(jButton1);
        this.add(jButton2);
        this.add(jButton4);
        this.add(jButton5);
        this.add(jButton6);
        this.add(jComboBox4);
        this.add(jTextField2);
        jScrollPane1.getViewport().add(jTextArea1);//加入滾動條
        //——————————————————————
    }
//清除按鈕
    public void jButton3_actionPerformed(ActionEvent e) {
        //————————————清空文本框
        jTextField1.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextArea1.setText("");
    //——————————

    }

    public void jTextField3_actionPerformed(ActionEvent e) {



    }
//加載日曆
    public void jButton6_actionPerformed(ActionEvent e) {
//使按鈕不可用
        jButton6.setEnabled(false);
        CalenderTrain ct = new CalenderTrain(jButton6,jTextField3);//打開日曆對話
       ct.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       ct.setVisible(true);//使日曆對話框可見
       jTextField3.setText(ct.dataSelect());//讀取日曆上選擇的值

    }
//班級下拉列表
    public void jComboBox1_actionPerformed(ActionEvent e) {
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //驅動加載
            con = DriverManager.getConnection("jdbc:odbc:glxt"); //數據橋連名子為ODBC名為glxt
            sql = "select stu_xh from stutable where class_id=(select class_id from classtable where class_name=?)";//班級班查學生
//            sql="select class_id from classtable where class_name=?";
            pre = con.prepareStatement(sql);
            pre.setString(1, String.valueOf(jComboBox1.getSelectedItem()));


            ResultSet result1 = pre.executeQuery();
            jComboBox4.removeAllItems();
            jComboBox4.addItem("");
            while (result1.next()) {
                String a=result1.getString(1).trim();

                jComboBox4.addItem(a);//將得到的信息加到下拉列表中
            }

            result1.close();
            pre.close();
            con.close();




       } catch (ClassNotFoundException ex) {
           //驅程不存在
//           System.out.println("2");
           JOptionPane.showMessageDialog(null, "驅程不存在");
       } catch (SQLException ex1) {
           //ODBC連接失敗
//           System.out.println("3");
           ex1.printStackTrace();

           JOptionPane.showMessageDialog(null, "ODBC連接失敗");
       }


    }
//選擇具體學生
    public void jComboBox4_actionPerformed(ActionEvent e) {

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //驅動加載
                con = DriverManager.getConnection("jdbc:odbc:glxt"); //數據橋連名子為ODBC名為glxt
                sql = "select stu_name,stu_xl,stu_sex,stu_tel,stu_address,stu_birthday,stu_td,stu_photo from stutable where stu_xh=?";
                pre = con.prepareStatement(sql);
                pre.setString(1, String.valueOf(jComboBox4.getSelectedItem()));
                result=pre.executeQuery();
                if(result.next()){
                    //把查出的數據存到指定的位置
                    jTextField1.setText(result.getString(1).trim());
                    jComboBox2.setSelectedItem(result.getString(2).trim());
                    jComboBox3.setSelectedItem(result.getString(3).trim());
                    jTextField4.setText(result.getString(4).trim());
                    jTextField5.setText(result.getString(5).trim());
                    jTextField3.setText(result.getString(6).trim());
                    jTextArea1.setText(result.getString(7).trim());
                    Url=result.getString(8);
                    if(Url!=null){//有照片時
                   Image img = Toolkit.getDefaultToolkit().getImage(Url);
                   ImageIcon imgIcon = new ImageIcon(img.getScaledInstance(125,145, 1));
                   jLabel11.setIcon(imgIcon);
                    }else if(Url==null){//無照片時
                        jLabel11.setIcon(null);
                    }
                }
                result.close();
                pre.close();
                con.close();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (SQLException ex1) {
                ex1.printStackTrace();
            }
    }
//修改學生信息
    public void jButton4_actionPerformed(ActionEvent e) {

        if (jButton4.getText().equals("修改") ){//當按鈕為修改時
            if(jComboBox1.getSelectedItem().equals(" ")||jComboBox4.getSelectedItem().equals("")){//班級和學號為空時
                 JOptionPane.showMessageDialog(null, "請先選擇班級和學號才能修改!");
                return;
            }
            //————————更改界面
            jTextField1.setEnabled(true);
            jComboBox2.setEnabled(true);
            jComboBox3.setEnabled(true);
            jTextField4.setEnabled(true);
            jTextField5.setEnabled(true);
            jTextArea1.setEnabled(true);
            jButton6.setEnabled(true);
            jButton3.setEnabled(true);
            jButton1.setEnabled(false);
            jButton2.setEnabled(false);
            jButton5.setEnabled(true);
            jButton4.setText("確定");
            xh=String.valueOf(jComboBox4.getSelectedItem());
            jComboBox4.setVisible(false);
            jTextField2.setVisible(true);
            jTextField2.setText(xh);
//————————————————
        }else if (jButton4.getText().equals("確定")){//當按鈕變為確定時

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -