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

📄 listmembers.java

📁 图书管理系统v1.0是使用JAVA语言开发的解决图书基本管理的一个应用程序。 该系统能够实现简单的图书和用户管理
💻 JAVA
字号:
package lib;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.SQLException;

public class ListMembers extends JInternalFrame {
	//for creating the North Panel
	private JPanel northPanel = new JPanel();
	//for creating the Center Panel
	private JPanel centerPanel = new JPanel();
	//for creating the label
	private JLabel label = new JLabel("用户清单");
	//for creating the button
	private JButton printButton;
	private JButton refreshButton;
	//for creating the table
	private JTable table;
	//for creating the TableColumn
	private TableColumn column = null;
	//for creating the JScrollPane
	private JScrollPane scrollPane;

	//for creating an object for the ResultSetTableModel class
	private ResultSetTableModel tableModel;
	/***************************************************************************
	 * for setting the required information for the ResultSetTableModel class. *
	 ***************************************************************************/
	private static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
	private static final String DATABASE_URL = "jdbc:odbc:JLibrary";
	private static final String DEFAULT_QUERY = "SELECT MemberID, ID, Name, EMail," +
	        "Major, Money,Expired FROM Members";

	//constructor of listMembers
	public ListMembers() {
		//for setting the title for the internal frame
		super("用户", true, true, true , true);
		//for setting the icon
		setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/List16.gif")));
		//for getting the graphical user interface components display area
		Container cp = getContentPane();

		//for bassing the required information to the ResultSetTableModel object
		try {
			tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, DEFAULT_QUERY);
			//for setting the Query
			try {
				tableModel.setQuery(DEFAULT_QUERY);
			}
			catch (SQLException sqlException) {
			}
		}
		catch (ClassNotFoundException classNotFound) {
		}
		catch (SQLException sqlException) {
		}
		//for setting the table with the information
		table = new JTable(tableModel);
		//for setting the size for the table
		table.setPreferredScrollableViewportSize(new Dimension(700, 200));
		//for setting the font
		table.setFont(new Font("Default", Font.PLAIN, 12));
		//for setting the scrollpane to the table
		scrollPane = new JScrollPane(table);

		//for setting the size for the table columns
		for (int i = 0; i < 6; i++) {
			column = table.getColumnModel().getColumn(i);
			if (i == 0) //MemberID
				column.setPreferredWidth(30);
			if (i == 1) //ID
				column.setPreferredWidth(20);
			if (i == 2) //Name
				column.setPreferredWidth(150);
			if (i == 3) //E-MAIL
				column.setPreferredWidth(120);
			if (i == 4) //Major
				column.setPreferredWidth(20);
			if (i == 5) //Expired
				column.setPreferredWidth(40);
		}
		//for setting the font to the label
		label.setFont(new Font("Default", Font.BOLD, 14));
		//for setting the layout to the panel
		northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		//for adding the label to the panel
		northPanel.add(label);
		//for adding the panel to the container
		cp.add("North", northPanel);

		//for setting the layout to the panel
		centerPanel.setLayout(new BorderLayout());
		//for creating an image for the button
		ImageIcon printIcon = new ImageIcon(ClassLoader.getSystemResource("images/Print16.gif"));
		//for adding the button to the panel
		printButton = new JButton("打印用户列表", printIcon);
		//for setting the tip text
		printButton.setToolTipText("打印");
		//for setting the font to the button
		printButton.setFont(new Font("Default", Font.PLAIN, 12));
		//for adding the button to the panel
		ImageIcon refreshIcon = new ImageIcon(ClassLoader.getSystemResource("images/refresh.gif"));
		refreshButton = new JButton("刷新用户列表",refreshIcon);
		refreshButton.setToolTipText("刷新");
		refreshButton.setFont(new Font("Default",Font.PLAIN, 12));

		centerPanel.add(printButton, BorderLayout.NORTH);
		//for adding the scrollpane to the panel
		centerPanel.add(scrollPane, BorderLayout.CENTER);
		//for setting the border to the panel
		centerPanel.add(refreshButton,BorderLayout.SOUTH);
		
		centerPanel.setBorder(BorderFactory.createTitledBorder("用户:"));
		//for adding the panel to the container
		cp.add("Center", centerPanel);

		//for adding the actionListener to the button
		printButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Thread runner = new Thread() {
					public void run() {
						try {
							PrinterJob prnJob = PrinterJob.getPrinterJob();
							prnJob.setPrintable(new PrintingMembers(DEFAULT_QUERY));
							if (!prnJob.printDialog())
								return;
							setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
							prnJob.print();
							setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
						}
						catch (PrinterException ex) {
							System.out.println("Printing error: " + ex.toString());
						}
					}
				};
				runner.start();
			}
		});
		refreshButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
			    centerPanel.setVisible(false);
			    centerPanel.remove(scrollPane);
		try {
			tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, DEFAULT_QUERY);
			//for setting the Query
			try {
				tableModel.setQuery(DEFAULT_QUERY);
			}
			catch (SQLException sqlException) {
			}
		}
		catch (ClassNotFoundException classNotFound) {
		}
		catch (SQLException sqlException) {
		}
		//for setting the table with the information
		table = new JTable(tableModel);
		//for setting the size for the table
		table.setPreferredScrollableViewportSize(new Dimension(700, 200));
		//for setting the font
		table.setFont(new Font("Default", Font.PLAIN, 12));
		//for setting the scrollpane to the table
		scrollPane = new JScrollPane(table);
		for (int i = 0; i < 6; i++) {
			column = table.getColumnModel().getColumn(i);
			if (i == 0) //MemberID
				column.setPreferredWidth(30);
			if (i == 1) //ID
				column.setPreferredWidth(20);
			if (i == 2) //Name
				column.setPreferredWidth(150);
			if (i == 3) //E-MAIL
				column.setPreferredWidth(120);
			if (i == 4) //Major
				column.setPreferredWidth(20);
			if (i == 5) //Expired
				column.setPreferredWidth(40);
		}
		centerPanel.add(scrollPane,BorderLayout.CENTER);
		centerPanel.setVisible(true);			    
		}
		});
		//for setting the visible to true
		setVisible(true);
		//to show the frame
		pack();
	}
}

⌨️ 快捷键说明

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