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

📄 frame1.java~36~

📁 选课系统
💻 JAVA~36~
字号:
package 选课系统;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class Frame1 extends JFrame{
    String[] job;
    static JFrame f = null;

    JPanel contentPane;
    JPanel jPanel1 = new JPanel();
    XYLayout xYLayout1 = new XYLayout();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextArea jTextArea1 = new JTextArea();
    XYLayout xYLayout2 = new XYLayout();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JList jList1 = new JList();//setSelectionMode( MULTIPLE_INTERVAL_SELECTION);//
    JLabel jLabel1 = new JLabel();
    public Frame1() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(xYLayout1);
        setSize(new Dimension(400, 300));
        setTitle("Frame Title");
        jPanel1.setLayout(xYLayout2);
        jButton1.setText("添加");
        jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
        jButton2.setText("显示");
        jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
        jLabel1.setText("选课");
        contentPane.add(jPanel1, new XYConstraints(0, 0, 402, 136));
        jPanel1.add(jButton1, new XYConstraints(29, 76, 106, 25));
        jPanel1.add(jButton2, new XYConstraints(224, 76, 111, 24));
        jPanel1.add(jList1, new XYConstraints(133, 3, 232, 58));
        jPanel1.add(jLabel1, new XYConstraints(64, 16, 34, 29));
        contentPane.add(jScrollPane1, new XYConstraints(0, 138, 400, 162));
        jScrollPane1.getViewport().add(jTextArea1);
       // jList1.setModel();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:qqqq");
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            int count = 0;
            rs = stmt.executeQuery(
                    "select count(course_name) from course group by course_name");
            while (rs.next()) {
                count++;
            }
            job = new String[count];
            int i = 0;
            rs = stmt.executeQuery("select course_name from course");
            while (rs.next()) {
                job[i] = rs.getString(1);
                i++;
            }

            for (i = 0; i < count; i++) {
                jList1.setListData(job);
            }
            jList1.setLocation(50, 50);
            jList1.setSize(25, 10);

            stmt.close();
            rs.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }

    public void jButton1_actionPerformed(ActionEvent e) {
        jTextArea1.setText("");
        Object[] w=new Object[3];
        w=jList1.getSelectedValues();
         for (int j = 0; j < w.length; j++)
          {
              jTextArea1.append(w[j].toString() + "\n");
          }
    }

    public void jButton2_actionPerformed(ActionEvent e) {
        jTextArea1.setText(" ");
         try {
                  ResultSet rs=null;
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动器
                  Connection con =
                                 DriverManager.getConnection("jdbc:odbc:qqqq");   //和数据库建立连接
                  Statement stmt = con.createStatement();  //创建语句
                  String SQLOrder = "select * from course"; //创建SQL命令字符串
                  rs=stmt.executeQuery(SQLOrder);//添加一条记录
                  while(rs.next()){
                      jTextArea1.append("课程号:"+rs.getInt("course_id"));
                      jTextArea1.append("课程名称:"+rs.getString("course_name"));
                      jTextArea1.append("学分:"+rs.getString("course_credit")+'\n');


                  }
                  stmt.close();
                  con.close(); //关闭数据库的连接
         }catch(Exception ex){}
           }
    }
    class Frame1_jButton1_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jButton1_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }

        public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
        }
    }



class Frame1_jButton2_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jButton2_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}

⌨️ 快捷键说明

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