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

📄 initwindow.java

📁 碟片出租系统,后台是SQL SERVER,联系编程时可以全部采用或者部分采用.
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;
import java.util.Properties;

public class InitWindow extends JFrame
{
	final String JDBC_DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
	final String DATABASE_URL = "jdbc:microsoft:sqlserver://localhost:1433;DATABASENAME=yswkcsj";
	final String sqlStr[] = {"              数据库连接错误", 
		"              确定系统已经安装sql server 2000!", 
		"              请重新输入用户名和密码:", "连接"};
	final String adminStr[] = {"              数据库连接成功!",
		 "              请输入管理员名和密码进入系统:", "登录"};
	final Font font = new Font("宋体", Font.PLAIN, 12);
	protected ImageIcon image;
	protected JPanel panel, labelPanel, textPanel;
	protected JLabel imageLabel, textLabel;
	protected JTextField userText;
	protected JPasswordField passwordText;
	protected JButton okButton, cancelButton;
	protected JCheckBox autoLogin;
	protected String user;
	protected String password;
	protected Properties settings;
	
	public static void main(String[] args)
	{
		InitWindow init = new InitWindow();
		System.out.println("正在连接数据库....");
		init.connectDatabase();
	}
	
	public InitWindow()
	{
		super();
		this.setUndecorated(true);
		settings = new Properties();
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());
		imageLabel = new JLabel(new ImageIcon("init.png"));
		pane.add(imageLabel, BorderLayout.NORTH);
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		pane.add(panel, BorderLayout.CENTER);
		setSize(558, 327);
		setLocation();
		setVisible(true);
	}
	
	public void addErrorPanel(String str[])
	{
		panel.removeAll();
		labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(str.length, 1));
		for (int i = 0; i < str.length - 1; i++)
		{
			textLabel = new JLabel(str[i], JLabel.LEFT);
			textLabel.setFont(font);
			labelPanel.add(textLabel);
	    }
		panel.add(labelPanel, BorderLayout.WEST);
		textPanel = new JPanel();
		textPanel.setLayout(new GridLayout(3, 3, 5, 5));
		textLabel = new JLabel("用户名:", JLabel.CENTER);
		textLabel.setFont(font);
		textPanel.add(textLabel);
		userText = new JTextField(8);
		userText.setRequestFocusEnabled(true);
		textPanel.add(userText);
		textLabel = new JLabel("      ");
		textPanel.add(textLabel);
		textLabel = new JLabel("密码:", JLabel.CENTER);
		textLabel.setFont(font);
		textPanel.add(textLabel);
		passwordText = new JPasswordField(8);
		textPanel.add(passwordText);
		textLabel = new JLabel("      ");
		textPanel.add(textLabel);
		okButton = new JButton(str[str.length - 1]);
		okButton.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					user = userText.getText();
					if(user.equals(""))
					{
						JOptionPane.showMessageDialog(InitWindow.this, "用户名不能为空!"
							, "输入信息错误", JOptionPane.ERROR_MESSAGE);
						return;
					}
					password = passwordText.getText();
					if(event.getActionCommand().equals("连接"))
					{
						settings.setProperty("user", user);
						settings.setProperty("password", password);
						try
						{
							settings.store( new FileOutputStream( "set.txt" ), "Program is used:");
						}catch( Exception e )
						{
							e.printStackTrace();
						}
						connectDatabase();
					}
					else
					{
						if(FilmHireSystem.tableModel.checkPassword(user, password))
						{
							if(autoLogin.isSelected())
							{
								settings.setProperty("adminUser", user);
								settings.setProperty("adminPassword", password);
							}
							else
							{
								settings.remove("adminUser");
								settings.remove("adminPassword");
							}
							try
							{
								settings.store( new FileOutputStream( "set.txt" ), "Program is used:");
							}catch( Exception e )
							{
								e.printStackTrace();
							}
							new FilmHireSystem(user);
							dispose();
						}
						else
						{
							JOptionPane.showMessageDialog(InitWindow.this, "用户名或密码错误!",
								"登录失败", JOptionPane.ERROR_MESSAGE);
							userText.setText("");
							passwordText.setText("");
							userText.grabFocus();
						}
					}
				}
			}
		);
		textPanel.add(okButton);
		cancelButton = new JButton("取消");
		cancelButton.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					if(okButton.getText().equals("登录"))
						FilmHireSystem.tableModel.disconnectFromDatabase();
					System.out.println("系统异常退出!");
					System.exit(1);
				}
			}
		);
		textPanel.add(cancelButton);
		if(str[str.length-1].equals("登录"))
		{
			autoLogin = new JCheckBox("自动登录");
			textPanel.add(autoLogin);
		}
		panel.add(textPanel, BorderLayout.CENTER);
		userText.grabFocus();
		setSize(558, 400);
		setLocation();
		setVisible(true);
	}
	
	public void connectDatabase()
	{
		try
		{
			settings.load(new FileInputStream("set.txt"));
		}
		catch(Exception e)
		{
			settings.setProperty("user", "sa");
			settings.setProperty("password", "");
		}
		user = settings.getProperty("user");
		password = settings.getProperty("password");
		try
		{
			FilmHireSystem.tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, 
												user, password);
			user = settings.getProperty("adminUser");
			password = settings.getProperty("adminPassword");
			if(user != null && FilmHireSystem.tableModel.checkPassword(user, password))
			{
				new FilmHireSystem(user);
				dispose();
			}
			else
			{
				if(user != null) JOptionPane.showMessageDialog(this, "密码错误!",
					"登录失败", JOptionPane.ERROR_MESSAGE);
				addErrorPanel(adminStr);
			}
		}
		catch(SQLException exception)
		{
			String err = exception.getMessage();
			System.err.println(err);
			if(err.indexOf("yswkcsj") != -1)
			{
				try
				{
					System.out.println("正在创建数据库....");
					new CreateDatabase(user, password);
				}
				catch(Exception ex)
				{
					System.err.println(ex.getMessage());
				}
				connectDatabase();
			}
			else
			{
				addErrorPanel(sqlStr);
			}
		}
		catch(ClassNotFoundException exception)
		{
			System.err.println(exception.getMessage());
			addErrorPanel(sqlStr);
		}
	}
	
	public void setLocation()
	{
		Dimension size = getSize();
		int x,y;
		x = (1024 - size.width) / 2;
		y = (768 - size.height) / 2;
		this.setLocation(x, y);
	}	
	
}

⌨️ 快捷键说明

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