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

📄 zxs.java

📁 基于java的BUPT自习室查询系统。可以查询符合要求的自习室
💻 JAVA
字号:
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;




public class Zxs extends JFrame {

	private JTable table;
	private JComboBox day;
	private JComboBox building;
	private static MyData mydata;
	
	protected Vector Infos = new Vector();
	protected SearchDialog sd = new SearchDialog();
	protected Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获取屏幕分辨率
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Zxs frame = new Zxs();
			init();
			frame.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the frame
	 */
	public Zxs() {
		super();
		setMinimumSize(new Dimension(536, 375));
		setTitle("BUPT自习室查询");
		setBounds(100, 100, 536, 375);
		setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2); //使窗口居中
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		

		final JPanel toolpanel = new JPanel();
		toolpanel.setLayout(new BorderLayout());
		getContentPane().add(toolpanel, BorderLayout.NORTH);

		final JToolBar toolBar_1 = new JToolBar();
		toolBar_1.setPreferredSize(new Dimension(0, 30));
		toolBar_1.setFloatable(false);
		toolpanel.add(toolBar_1, BorderLayout.SOUTH);

		final JLabel label_2 = new JLabel();
		toolBar_1.add(label_2);
		label_2.setText("时间段:");

		final JCheckBox check1 = new JCheckBox();
		check1.setToolTipText("请选择希望的可用时间段");
		toolBar_1.add(check1);
		check1.setText("1-2");

		final JCheckBox check2 = new JCheckBox();
		check2.setToolTipText("请选择希望的可用时间段");
		toolBar_1.add(check2);
		check2.setText("3-4");

		final JCheckBox check3 = new JCheckBox();
		check3.setToolTipText("请选择希望的可用时间段");
		toolBar_1.add(check3);
		check3.setText("5-6");

		final JCheckBox check4 = new JCheckBox();
		check4.setToolTipText("请选择希望的可用时间段");
		toolBar_1.add(check4);
		check4.setText("7-9");

		final JCheckBox check5 = new JCheckBox();
		check5.setToolTipText("请选择希望的可用时间段");
		toolBar_1.add(check5);
		check5.setText("晚上");

		final JButton search = new JButton();
		search.setToolTipText("查询符合条件的自习室");
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Infos.clear();
				boolean [] state = {check1.isSelected(),check2.isSelected(),check3.isSelected(),check4.isSelected(),check5.isSelected()};
				try {
					int n;
					n = mydata.bytime(building.getSelectedIndex(), day.getSelectedIndex()+1, state, Infos);
					if(n == 0)  JOptionPane.showMessageDialog(null,"没有找到符合要求的自习室","没有找到",JOptionPane.WARNING_MESSAGE);
					else JOptionPane.showMessageDialog(null,"找到"+n+"个符合要求的自习室","查找结果",JOptionPane.INFORMATION_MESSAGE);
				} catch (SQLException e1) {
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
				//System.out.println(Infos);
				table.updateUI();
			}
		});
		search.setMnemonic('s');
		toolBar_1.add(search);
		search.setText("查询(S)");

		toolBar_1.addSeparator();

		final JButton roominfo = new JButton();
		roominfo.setToolTipText("查询单个教室的占用情况");
		roominfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				opensd();
			}
		});
		roominfo.setMnemonic('i');
		roominfo.setText("查询教室占用情况(I)");
		toolBar_1.add(roominfo);

		toolBar_1.addSeparator();

		final JButton about = new JButton();
		about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"BUPT自习室查询器 Powered By Yulei666","关于",JOptionPane.INFORMATION_MESSAGE);
			}
		});
		about.setToolTipText("关于");
		about.setText("");
		about.setIcon(new ImageIcon(getToolkit().getImage(getClass().getResource("img/about.gif"))));
		toolBar_1.add(about);

		final JToolBar toolBar = new JToolBar();
		toolBar.setPreferredSize(new java.awt.Dimension(0, 30));
		toolpanel.add(toolBar, BorderLayout.NORTH);
		toolBar.setFloatable(false);

		final JLabel label = new JLabel();
		label.setText("教学楼:");
		toolBar.add(label);

		building = new JComboBox();
		building.setToolTipText("请选择教学楼");
		building.setModel(new DefaultComboBoxModel(new String[] {"全部", "教一", "教二", "教三", "教四", "图书馆"}));
		building.setMaximumSize(new Dimension(80, 24));
		building.setPreferredSize(new java.awt.Dimension(0, 0));
		toolBar.add(building);

		final JLabel label_1 = new JLabel();
		label_1.setHorizontalTextPosition(SwingConstants.LEADING);
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setMaximumSize(new Dimension(50, 30));
		label_1.setText("星期:");
		toolBar.add(label_1);

		day = new JComboBox();
		day.setToolTipText("请选择星期");
		day.setModel(new DefaultComboBoxModel(new String[] {"一", "二", "三", "四", "五"}));
		day.setMaximumSize(new Dimension(80, 24));
		day.setPreferredSize(new java.awt.Dimension(0, 0));
		toolBar.add(day);

		final JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		String names[] ={"教学楼","楼层","教室号"};
		Vector columnnames = new Vector(Arrays.asList( names ));
		
		
		Infos.addElement(columnnames);
		Infos.addElement(columnnames);
		Infos.addElement(columnnames);
		Infos.addElement(columnnames);

		table = new JTable(Infos,columnnames);
		table.setToolTipText("双击条目查看选定教室占用情况");
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int mods=e.getModifiers();
				if((mods&InputEvent.BUTTON1_MASK)!=0&&e.getClickCount()>=2){
					searchroom(table.getSelectedRow());
				}
			}
		});
		
		DefaultTableModel t = new javax.swing.table.DefaultTableModel(Infos,columnnames){
            /**
			 * 
			 */
			private static final long serialVersionUID = 647760029404730849L;

			public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        }; 
        table.setModel(t);
		scrollPane.setViewportView(table);
		//
	}
	
	private static void init(){
		mydata = new MyData();
		try {
			mydata.init();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			JOptionPane.showMessageDialog(null,"数据库连接出错,请检查","错误",JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private void searchroom(int row){
		ResultDialog rd = new ResultDialog();
		Vector info = new Vector();
		try {
			mydata.byroom(Integer.parseInt((String)table.getValueAt(row, 0)), Integer.parseInt((String)table.getValueAt(row, 2)), info);
			rd.init(info, Integer.parseInt((String)table.getValueAt(row, 0)), Integer.parseInt((String)table.getValueAt(row, 2)));
			rd.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
			rd.setLocation(this.getLocation().x+50, this.getLocation().y+60);
			rd.setVisible(true);
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (NumberFormatException e1) {
			// TODO 自动生成 catch 块
			JOptionPane.showMessageDialog(null,"请正确选择","错误",JOptionPane.ERROR_MESSAGE);
		}
		//System.out.println(info);
	}
	
	private void opensd(){
		sd.init(mydata);
		sd.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		sd.setLocation(this.getLocation().x+50, this.getLocation().y+60);
		sd.setVisible(true);
	}

}

⌨️ 快捷键说明

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