📄 library.java
字号:
// Library.java
import java.awt.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
public class Library extends JFrame
{
private JTextField booknumberJTextField;
private JTextField studentnameJTextField;
private JTextField nameJTextField1;
private JTextField returnJTextField1;
private JTextField nameJTextField2;
private JTable table;
private JLabel IDJLabel;
// JPanel to contain Personal information
private JPanel PersonalInformationJPanel;
// JLabel and JTextField to display identification number
private JLabel booknumberJLabel;
private JTextField IDJTextField;
// JLabel and JTextField for name
private JLabel booknameJLabel;
private JTextField nameJTextField;
private JLabel borrowJLabel1;
private JTextField borrowJTextField1;
private JLabel borrowJLabel2;
private JTextField borrowJTextField2;
private JLabel classesJLabel;
private JComboBox classesJComboBox;
private JLabel returnJLabel2;
private JTextField returnJTextField2;
private JPanel ClassesJPanel;
// JComboBox, JList and JScrollPane for Class number
private JComboBox ClassesJComboBox;
// JButtons
private JButton scanNewJButton;
private JButton addJButton;
private JButton removeJButton;
private JButton editJButton;
private JButton updateJButton;
// array contains options for classJComboBox
private String[] classnumber = { "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10" };
private int parcelID = 1; // ID for new Parcels
private Connection myConnection;
private Statement myStatement;
private ResultSet myResultSet;
private String name,borrow1,classes,borrow2,bookname1,bookname2,return2;
private int return1,booknumber;
/* 声明表格模型 */
private DefaultTableModel tableModel = new DefaultTableModel();
/* 声明表格头数组 */
private String[] tableHeads = {"编号","姓名","班级","本数","书名1","书名2","1借书","2借书","1距还","2距还"};
/* 将表格头转换过向量类型,以备表格模型使用 */
private Vector tableHeadName = new Vector();
public JScrollPane scrollPane;
// no-argument constructor
public Library()
{
try
{
// load Cloudscape driver
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver");
// connect to database
myConnection =
DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;DatabaseName=Library","sa","1" );
// create Statement for executing SQL
myStatement = myConnection.createStatement();
}
catch ( SQLException exception )
{
exception.printStackTrace();
}
catch ( ClassNotFoundException exception )
{
exception.printStackTrace();
}
for (int i = 0; i < tableHeads.length; i++) {
tableHeadName.add(tableHeads[i]);
}
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up IDJLabel
IDJLabel = new JLabel();
getContentPane().add(IDJLabel);
IDJLabel.setBounds(19, 14, 74, 24);
IDJLabel.setText( "编号:" );
// set up IDJTextField
IDJTextField = new JTextField();
getContentPane().add(IDJTextField);
IDJTextField.setBounds(66, 17, 37, 21);
IDJTextField.setEditable( false );
JLabel studentnamelabel = new JLabel("姓名:");
getContentPane().add(studentnamelabel);
studentnamelabel.setBounds(151, 17, 60, 21);
studentnameJTextField = new JTextField();
studentnameJTextField.setEditable(false);
getContentPane().add(studentnameJTextField);
studentnameJTextField.setBounds(192, 17, 120, 20);
// set up classesJLabel
classesJLabel = new JLabel();
getContentPane().add(classesJLabel);
classesJLabel.setBounds(340, 14, 47, 24);
classesJLabel.setText( "班级:" );
// set up classesJComboBox
classesJComboBox = new JComboBox( classnumber );
getContentPane().add(classesJComboBox);
classesJComboBox.setBounds(393, 16, 70, 21);
classesJComboBox.setEnabled( false );
// set up PersonalInformationJPanel
PersonalInformationJPanel = new JPanel();
PersonalInformationJPanel.setBackground(Color.YELLOW);
PersonalInformationJPanel.setBounds( 10, 54, 490, 186 );
final TitledBorder titledBorder = new TitledBorder( "个人借书信息" );
titledBorder.setTitleFont(new Font("", Font.BOLD, 15));
titledBorder.setTitleColor(Color.RED);
PersonalInformationJPanel.setBorder(
titledBorder );
PersonalInformationJPanel.setLayout( null );
contentPane.add( PersonalInformationJPanel );
// set up booknumberJLabel
booknumberJLabel = new JLabel();
booknumberJLabel.setBounds(15, 24, 91, 30);
PersonalInformationJPanel.add(booknumberJLabel);
booknumberJLabel.setText( "借书本数:" );
booknumberJTextField = new JTextField();
PersonalInformationJPanel.add(booknumberJTextField);
booknumberJTextField.setEditable(false);
booknumberJTextField.setBounds(93, 30, 51, 20);
// set up booknameJLabel
booknameJLabel = new JLabel();
booknameJLabel.setBounds( 15, 65, 84, 25 );
booknameJLabel.setText( "书名:(1)" );
PersonalInformationJPanel.add( booknameJLabel );
// set up nameJTextField1
nameJTextField1 = new JTextField();
nameJTextField1.setEditable(false);
PersonalInformationJPanel.add(nameJTextField1);
nameJTextField1.setBounds(93, 68, 156, 20);
final JLabel booknamelabel2 = new JLabel();
PersonalInformationJPanel.add(booknamelabel2);
booknamelabel2.setText(" (2)");
booknamelabel2.setBounds(242, 70, 59, 15);
// set up borrowJLabel1
borrowJLabel1 = new JLabel();
borrowJLabel1.setBounds( 15, 103, 102, 25 );
borrowJLabel1.setText( "借书日期(1)" );
PersonalInformationJPanel.add( borrowJLabel1 );
// set up borrowJTextField1
borrowJTextField1 = new JTextField();
borrowJTextField1.setBounds( 108, 106, 196, 21 );
borrowJTextField1.setEditable( false );
PersonalInformationJPanel.add( borrowJTextField1 );
// set up borrowJLabel2
borrowJLabel2 = new JLabel();
borrowJLabel2.setBounds( 15, 138, 102, 24 );
borrowJLabel2.setText( "借书日期(2)" );
PersonalInformationJPanel.add( borrowJLabel2 );
// set up borrowJTextField2
borrowJTextField2 = new JTextField();
borrowJTextField2.setBounds( 108, 141, 196, 21 );
borrowJTextField2.setEditable( false );
PersonalInformationJPanel.add( borrowJTextField2 );
returnJTextField1 = new JTextField();
PersonalInformationJPanel.add(returnJTextField1);
returnJTextField1.setEditable(false);
returnJTextField1.setBounds(406, 106, 60, 20);
// set up returnJLabel2
returnJLabel2 = new JLabel();
returnJLabel2.setBounds( 331, 138, 69, 24 );
returnJLabel2.setText( "距还天数:" );
PersonalInformationJPanel.add( returnJLabel2 );
// set up returnJTextField2
returnJTextField2 = new JTextField();
returnJTextField2.setBounds( 406, 141, 60, 21 );
returnJTextField2.setEditable( false );
PersonalInformationJPanel.add( returnJTextField2 );
nameJTextField2 = new JTextField();
PersonalInformationJPanel.add(nameJTextField2);
nameJTextField2.setEditable(false);
PersonalInformationJPanel.add(nameJTextField2);
nameJTextField2.setBounds(286, 65, 180, 21);
PersonalInformationJPanel.add( nameJTextField2 );
final JLabel returnlabel1 = new JLabel();
PersonalInformationJPanel.add(returnlabel1);
returnlabel1.setText("距还天数:");
returnlabel1.setBounds(331, 103, 69, 25);
PersonalInformationJPanel.add(returnlabel1);
PersonalInformationJPanel.add(returnlabel1);
final JLabel label_4 = new JLabel();
label_4.setText("(每人最多能借两本书)");
label_4.setBounds(161, 29, 156, 20);
PersonalInformationJPanel.add(label_4);
// set up ClassesJPanel
ClassesJPanel = new JPanel();
ClassesJPanel.setBackground(Color.GREEN);
ClassesJPanel.setBounds( 10, 316, 483, 193 );
final TitledBorder titledBorder_1 = new TitledBorder( "班级借书信息" );
titledBorder_1.setTitleFont(new Font("", Font.BOLD, 15));
titledBorder_1.setTitleColor(Color.blue);
ClassesJPanel.setBorder(
titledBorder_1 );
ClassesJPanel.setLayout( null );
contentPane.add( ClassesJPanel );
// set up ClassesJComboBox
ClassesJComboBox = new JComboBox( classnumber );
ClassesJComboBox.setBounds( 19, 29, 98, 21 );
ClassesJPanel.add( ClassesJComboBox );
ClassesJComboBox.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when JComboBox
// is selected
public void actionPerformed( ActionEvent event )
{
ClassesJComboBoxActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
scrollPane = new JScrollPane();
scrollPane.setBounds(12, 59, 461, 119);
ClassesJPanel.add(scrollPane);
// set up scanNewJButton
scanNewJButton = new JButton();
scanNewJButton.setBackground(new Color(131, 228, 223));
scanNewJButton.setBounds( 8, 255, 95, 26 );
scanNewJButton.setText( "创建(S)" );
scanNewJButton.setMnemonic( KeyEvent.VK_S );
contentPane.add( scanNewJButton );
scanNewJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when scanNewJButton is pressed
public void actionPerformed( ActionEvent event )
{
scanNewJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up addJButton
addJButton = new JButton();
addJButton.setBackground(new Color(131, 228, 223));
addJButton.setBounds( 111, 254, 85, 26 );
addJButton.setText( "添加(A)" );
addJButton.setMnemonic( KeyEvent.VK_A );
addJButton.setEnabled( false );
contentPane.add( addJButton );
addJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when addJButton is pressed
public void actionPerformed( ActionEvent event )
{
addJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up removeJButton
removeJButton = new JButton();
removeJButton.setBackground(new Color(255, 0, 0));
removeJButton.setBounds( 202, 254, 85, 26 );
removeJButton.setText( "删除(R)" );
removeJButton.setMnemonic( KeyEvent.VK_R );
removeJButton.setEnabled( false );
contentPane.add( removeJButton );
removeJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when removeJButton is pressed
public void actionPerformed( ActionEvent event )
{
removeJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up editJButton
editJButton = new JButton();
editJButton.setBackground(new Color(131, 228, 223));
editJButton.setBounds( 308, 254, 85, 26 );
editJButton.setText( "修改(E)" );
editJButton.setMnemonic( KeyEvent.VK_E );
editJButton.setEnabled( false );
contentPane.add( editJButton );
editJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when editJButton is pressed
public void actionPerformed( ActionEvent event )
{
editJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up updateJButton
updateJButton = new JButton();
updateJButton.setBackground(new Color(131, 228, 223));
updateJButton.setBounds( 399, 254, 85, 26 );
updateJButton.setText( "保存(U)" );
updateJButton.setMnemonic( KeyEvent.VK_U );
updateJButton.setEnabled( false );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -