JAVA登陆界面+背景图片+背景音乐

来源:互联网 发布:淘宝店铺引流推广 编辑:程序博客网 时间:2024/04/27 10:59
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.applet.*;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
import java.net.*;
import static javax.swing.JFrame.*;
import java.awt.event.*;
import java.io.*;
import java.util.Calendar;
import java.sql.*;
import javax.sound.sampled.*;
class TestMusic  {
   private AudioFormat format;
   private byte[] samples;
   public TestMusic(String filename) {
    try {
    // 打开音频输入流
     AudioInputStream stream =AudioSystem.getAudioInputStream(new File(filename));
     format = stream.getFormat();
    // 获得音频信号
     samples = getSamples(stream);
    }
    catch (UnsupportedAudioFileException ex) {
     ex.printStackTrace();
    }
    catch (IOException ex) {
     ex.printStackTrace();
    }
   }
   public byte[] getSamples() {
    return samples;
   }
   private byte[] getSamples(AudioInputStream audioStream) {
    // 获得要读取的字节数
    int length = (int)(audioStream.getFrameLength() * format.getFrameSize());
    // 读取整个流
    byte[] samples = new byte[length];
    DataInputStream is = new DataInputStream(audioStream);
    try {
     is.readFully(samples);
    }
    catch (IOException ex) {
     ex.printStackTrace();
    }
    // 返回样本
    return samples;
   }
   public void play(InputStream source) {
    // use a short, 100ms (1/10th sec) buffer for real-time
    // 转换至声音流
    int bufferSize = format.getFrameSize() *
    Math.round(format.getSampleRate() / 10);
    byte[] buffer = new byte[bufferSize];
    // create a line to play to
    SourceDataLine line;
    try {
     DataLine.Info info =
     new DataLine.Info(SourceDataLine.class, format);
     line = (SourceDataLine)AudioSystem.getLine(info);
     line.open(format, bufferSize);
    }
    catch (LineUnavailableException ex) {
     ex.printStackTrace();
     return;
    }
    // start the line
    line.start();
    // copy data to the line
    try {
     int numBytesRead = 0;
     while (numBytesRead != -1) {
      numBytesRead =
      source.read(buffer, 0, buffer.length);
      if (numBytesRead != -1) {
       line.write(buffer, 0, numBytesRead);
      }
     }
    }
    catch (IOException ex) {
     ex.printStackTrace();
    }
    // wait until all data is played, then close the line
    line.drain();
    line.close();
  }
}
public class Login extends JFrame implements ActionListener{
  JTextField user1;
  JPasswordField password1;
  JLabel jl1, jl2, jl3, jl5;
  JButton btnLogin, btnReg;
  JCheckBox jc1, jc2;
  int search_record=0;
  Connection Con=null;Statement sql=null;
  String url = "jdbc:mysql://localhost:3306/university?useUnicode=true&characterEncoding=utf-8&useSSL=false";
  String name = "com.mysql.jdbc.Driver"; 
  String user = "root"; 
  String password = "123456zxc.";
  
  public Login(){   
  message_log("DataWindow Start : ");     
  try{
   Class.forName(name);  //找MySQL的驱动
  }catch(ClassNotFoundException e){
   e.printStackTrace();
 }
  try{
   Con=DriverManager.getConnection(url, user, password); //连接驱动
   message_log("Start Con="+sql);
   sql=Con.createStatement();
   message_log("Start sql="+sql);
    }catch(SQLException ee) {
   ee.printStackTrace();
  }
   this.setTitle("学生信息管理");
   //窗体组件初始化
   init();
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setLayout(null); //绝对定位
   this.setBounds(100,100,400,220);
   this.setResizable(false);
   Image image = new ImageIcon("C:/b.jpg").getImage();  //向标题插入图片
   this.setIconImage(image);  //标题图片
   this.setLocationRelativeTo(null);//居中显示
   this.setVisible(true);   //窗体可见
  }
  public void init(){
   Container con=this.getContentPane();
   jl1 = new JLabel();
   /*设置背景图片*/
   int width = 500;
   int height = 500;
   Image image1 = new ImageIcon("C:/2.gif").getImage();
   jl1.setIcon(new ImageIcon(image1));
   jl1.setBounds(0,0,400,220);
   //用户号码登录输入框
   user1 = new JTextField();
   user1.setBounds(135,75,150,20);
   //用户号码登录输入框旁边的文字
   jl2 = new JLabel("一卡通号");
   jl2.setBounds(75,75,70,20);
   //密码输入框
   password1 = new JPasswordField();
   password1.setBounds(135,105,150,20);
   //密码框旁边的文字
   jl3 = new JLabel("密码");
   jl3.setBounds(75,105,70,20);
   jl5 = new JLabel("学生信息管理");
   jl5.setBounds(110,0,200,100);
   jl5.setFont(new Font("宋体",Font.BOLD,22));
   jc1=new JCheckBox("记住密码");
   jc1.setBounds(105,133,77,15);
   jc1.setBorderPaintedFlat(true);
   jc1.setAutoscrolls(true);
   jc2=new JCheckBox("自动登入");
   jc2.setBounds(195,133,77,15);
   btnLogin = new JButton("登录");
   btnReg = new JButton("注册");
   btnLogin.setBounds(100,165,70,20);
   btnReg.setBounds(210,165,70,20);
   btnLogin.addActionListener(this);
   btnReg.addActionListener(this);
   jl1.add(jl5);
   jl1.add(jl2);
   jl1.add(jl3);
   jl1.add(user1);
   jl1.add(password1);
   jl1.add(jc1);
   jl1.add(jc2);
   jl1.add(btnLogin);
   jl1.add(btnReg);
   
   con.add(jl1);
  }
  @Override  //伪代码,表示重写
  public void actionPerformed(ActionEvent e){
   if(e.getActionCommand().equals("登录")){
    search_record = 0;
    try {
     query ();
    }
    catch (SQLException ee){}
    if (search_record == 1) {
     this.setVisible(false);
     ShowTable w = new ShowTable();
     w.setVisible(true);
    }
    else {
     jl1.setText("wrong");
    }
   }
   if(e.getActionCommand().equals("注册")){
    //this.setVisible(false);
    ZhuCe registe = new ZhuCe();
    registe.setVisible(true);
   }
  }
  
  public void query () throws SQLException {
  message_log ("query start");
  String xulie,
      mima;
  Con=DriverManager.getConnection(url, user, password);       
        message_log ("Con=" + Con);
        ResultSet rs = sql.executeQuery ("SELECT * FROM university");
        message_log ("input=" + user1.getText ().trim () + ", " +password1.getText().trim());
        while (rs.next()){
            message_log ("while start " );
   xulie = rs.getString ("user1");
   mima = rs.getString("password1");
   if (xulie.trim ().equals (user1.getText ().trim ())
    && mima.trim().equals (password1.getText().trim())) {
    message_log ("equal");
    message_log ("in query : xulie= " + xulie
    + "  mima= " + mima);
    user1.setText ("YOU ARE LOGGED");
    search_record = 1;
    break;
   }
   
   if(search_record==0){
    user1.setText("CANNOT FOUND THIS USER");
   }
     }
 }
  
     public void message_log(String msg)
   {
  try {
   java.util.Date RightNow = Calendar.getInstance().getTime();
   java.text.SimpleDateFormat LOG_FILENAME_SDF=new java.text.SimpleDateFormat("yyyyMMdd");
   java.text.SimpleDateFormat SMS_LOG_DETAIL_TIME=new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss ");
   // get the Log File Location
   FileOutputStream fos;
   fos = new FileOutputStream(LOG_FILENAME_SDF.format(RightNow) + "_Database.log" ,true);   //生成一个log的日志文件
   PrintWriter pw = new PrintWriter(fos);
   pw.print(SMS_LOG_DETAIL_TIME.format(RightNow));
   pw.println(" " + msg);
   pw.close();
   fos.close();
  } catch (Exception ex) {
   System.out.println(msg);
   System.out.println("SMSMsgLog: Error \r\n"+ex.getMessage());
   ex.printStackTrace();
  }
    }
 public static void main(String args[]){
  Login log = new Login();
}}
0 0
原创粉丝点击