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

📄 loginframe.java

📁 模拟图书馆管理系统 实现了各种图书馆管理功能
💻 JAVA
字号:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.io.*;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JTabbedPane;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Rectangle;
import javax.swing.*;
import java.sql.*;

public class LoginFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JTabbedPane jTabbedPane = null;

	private JPanel jPanel = null;

	private JPanel jPanel1 = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private JDesktopPane jDesktopPane = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JTextField jTextField = null;

	private JPasswordField jPasswordField = null;

	private JCheckBox jCheckBox = null;

	private JCheckBox jCheckBox1 = null;

	private JDesktopPane jDesktopPane1 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private JLabel jLabel5 = null;

	private JLabel jLabel7 = null;

	private JLabel jLabel8 = null;

	private JButton jButton2 = null;

	private JTextField jTextField1 = null;

	private JTextField jTextField2 = null;

	private JTextField jTextField3 = null;

	private JPasswordField jPasswordField1 = null;

	private JTextField jTextField4 = null;
	
	private Connection connection = null;

	private BookFrame bookFrame = null;
	/**
	 * This is the default constructor
	 */
	public LoginFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(414, 310);
		this.setContentPane(getJContentPane());
		this.setTitle("图书馆管理系统");
		this.setResizable(false);
		this.setLocation(400, 200);
		this.setVisible(true);
		try{
		RandomAccessFile randomAccessFile = new RandomAccessFile("config.txt","r");
		String ifSet = randomAccessFile.readLine();
		String idSet = randomAccessFile.readLine();
		String passwordSet = randomAccessFile.readLine();
		//System.out.println(ifSet);
		if(ifSet.equals(new String("SAVE:true"))){
			//System.out.println("true");
			jCheckBox.setSelected(true);
			String id = idSet.substring(3);
			String password = passwordSet.substring(9);
			jTextField.setText(id);
			//System.out.println(id);
			if(id.trim().equals(new String("admin"))) jCheckBox1.setSelected(true);
			jPasswordField.setText(password);
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				//System.out.println("windowClosing()"); // TODO Auto-generated Event stub windowClosing()
			    System.exit(1);
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weighty = 0.008;
			jLabel = new JLabel();
			//jLabel.setText("欢迎界面 待定");
			jLabel.setIcon(new ImageIcon("Icon\\library.jpg"));
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(jLabel, gridBagConstraints);
			jContentPane.add(getJTabbedPane(), gridBagConstraints1);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("用户登录", null, getJPanel(), null);
			jTabbedPane.addTab("数据源设置", null, getJPanel1(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJDesktopPane(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(getJDesktopPane1(), null);
			jPanel1.add(getJButton2(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(222, 147, 72, 20));
			jButton.setText("登录");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//setVisible(false);
					//BookFrame bookFrame = new BookFrame();
					
					String url = "jdbc:microsoft:sqlserver://"+jTextField2.getText()+":"+jTextField1.getText()+";DatabaseName="+jTextField4.getText();
					String user = jTextField3.getText();
					String password = jPasswordField1.getText();
					
					if(connection == null)
						connection = DataBaseConnect.connect(url, user, password) ;
					
					try{
						Statement statement = connection.createStatement();
						String id = jTextField.getText();
						String password2 = jPasswordField.getText();
						String query = "select * from Reader where id = "
							         + "'" + id + "'"
							         + "and password = "
							         + "'" + password2 + "'";
						 ResultSet resultSet = statement.executeQuery(query);
						 if(resultSet.next()){
							 if(bookFrame == null){
								 setVisible(false);
								 bookFrame = new BookFrame(id,connection);

									if(jCheckBox.isSelected()==true){
									try{
										FileWriter fileWriter = new FileWriter("config.txt"); 
										fileWriter.write("SAVE:true\n");
										fileWriter.write("ID:"+jTextField.getText()+"\n");
										fileWriter.write("PASSWORD:"+jPasswordField.getText()+"\n");
										fileWriter.close();
									}catch(Exception ioE){
										ioE.printStackTrace();
									}
									}else{
										try{
											FileWriter fileWriter = new FileWriter("config.txt"); 
											fileWriter.write("SAVE:false\n");
											fileWriter.write("ID:"+"null\n");
											fileWriter.write("PASSWORD:"+"null\n");
											fileWriter.close();
										}catch(Exception ioE){
											ioE.printStackTrace();
										}
									}
									//System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
								
									
							 }
							// JOptionPane.showMessageDialog(null, "登录成功","成功",JOptionPane.INFORMATION_MESSAGE);
						 }
						 else{
							 JOptionPane.showMessageDialog(null, "密码错误或帐号不存在", "登录不成功", JOptionPane.ERROR_MESSAGE);
						 }
							// System.out.println(true);
						 
					}catch(Exception sqlE){
						sqlE.printStackTrace();
						JOptionPane.showMessageDialog(null, "登录不成功", "不成功", JOptionPane.ERROR_MESSAGE);
					}
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(307, 147, 66, 20));
			jButton1.setText("注册");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ReaderLoginFrame readerLoginFrame = new ReaderLoginFrame();
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jDesktopPane	
	 * 	
	 * @return javax.swing.JDesktopPane	
	 */
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(27, 60, 57, 23));
			jLabel2.setText(" 密码");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(25, 17, 58, 23));
			jLabel1.setText("  帐号");
			jDesktopPane = new JDesktopPane();
			jDesktopPane.setBounds(new Rectangle(14, 16, 362, 120));
			jDesktopPane.add(jLabel1, null);
			jDesktopPane.add(jLabel2, null);
			jDesktopPane.add(getJTextField(), null);
			jDesktopPane.add(getJPasswordField(), null);
			jDesktopPane.add(getJCheckBox(), null);
			jDesktopPane.add(getJCheckBox1(), null);
		}
		return jDesktopPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(96, 16, 159, 23));
			jTextField.setText("");
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(97, 59, 158, 23));
			jPasswordField.setText("");
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setBounds(new Rectangle(263, 59, 95, 22));
			jCheckBox.setText("记住密码");
			//jCheckBox.setSelected(true);
		}
		return jCheckBox;
	}

	/**
	 * This method initializes jCheckBox1	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox1() {
		if (jCheckBox1 == null) {
			jCheckBox1 = new JCheckBox();
			jCheckBox1.setBounds(new Rectangle(263, 18, 95, 22));
			jCheckBox1.setText("我是管理员");
			//jCheckBox1.setSelected(true);
			jCheckBox1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					if(jCheckBox1.isSelected()){
						jTextField.setText("admin");
						jPasswordField.setText("");
					}
					else
						jTextField.setText("");
					//System.out.println(jCheckBox1.isSelected());
					//System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
				}
			});
            			
		}
		return jCheckBox1;
	}

	/**
	 * This method initializes jDesktopPane1	
	 * 	
	 * @return javax.swing.JDesktopPane	
	 */
	private JDesktopPane getJDesktopPane1() {
		if (jDesktopPane1 == null) {
			jLabel8 = new JLabel();
			jLabel8.setBounds(new Rectangle(15, 44, 57, 19));
			jLabel8.setText("数据库名");
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(256, 11, 39, 19));
			jLabel7.setText("端口");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(15, 114, 51, 19));
			jLabel5.setText("密码");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(15, 78, 51, 19));
			jLabel4.setText("用户名");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(15, 11, 57, 19));
			jLabel3.setText("数据源 IP");
			jDesktopPane1 = new JDesktopPane();
			jDesktopPane1.setBounds(new Rectangle(6, 7, 389, 139));
			jDesktopPane1.add(jLabel3, null);
			jDesktopPane1.add(jLabel4, null);
			jDesktopPane1.add(jLabel5, null);
			jDesktopPane1.add(jLabel7, null);
			jDesktopPane1.add(jLabel8, null);
			jDesktopPane1.add(getJTextField1(), null);
			jDesktopPane1.add(getJTextField2(), null);
			jDesktopPane1.add(getJTextField3(), null);
			jDesktopPane1.add(getJPasswordField1(), null);
			jDesktopPane1.add(getJTextField4(), null);
		}
		return jDesktopPane1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(279, 149, 92, 23));
			jButton2.setText(" 测试连接");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String url = "jdbc:microsoft:sqlserver://"+jTextField2.getText()+":"+jTextField1.getText()+";DatabaseName="+jTextField4.getText();
					String user = jTextField3.getText();
					String password = jPasswordField1.getText();
					DataBaseConnect.connectTest(url, user, password);
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(303, 13, 53, 15));
			jTextField1.setText("1433");
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setBounds(new Rectangle(80, 13, 161, 21));
			jTextField2.setText("127.0.0.1");
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setBounds(new Rectangle(80, 80, 161, 21));
			jTextField3.setText("sa");
		}
		return jTextField3;
	}

	/**
	 * This method initializes jPasswordField1	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField1() {
		if (jPasswordField1 == null) {
			jPasswordField1 = new JPasswordField();
			jPasswordField1.setBounds(new Rectangle(80, 114, 161, 21));
		}
		return jPasswordField1;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setBounds(new Rectangle(80, 45, 160, 20));
			jTextField4.setText("Library");
		}
		return jTextField4;
	}

}  //  @jve:decl-index=0:visual-constraint="181,39"

⌨️ 快捷键说明

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