📄 usersearch.java
字号:
package bookmanager;import javax.swing.*;import com.borland.jbcl.layout.*;import java.awt.*;import java.sql.*; //引入包java.sql.*;import javax.swing.table.*; //引入包sqljavax.swing.table.*;import java.util.*; //引入包sqljava.util*;import java.awt.event.*;import com.borland.dbswing.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import com.borland.jbcl.layout.*;import java.awt.*;import java.sql.*;import javax.swing.*;import javax.swing.table.*;import java.util.*;import java.awt.event.*;public class usersearch extends JFrame { XYLayout xYLayout1 = new XYLayout(); JLabel jLabel1 = new JLabel(); JTextField jTContent = new JTextField(); JButton jBOK = new JButton(); JButton jBCancel = new JButton(); String searchType =new String(); //标记查询类别 String searchValue=new String(); //标记查询值 String infoShowType=new String(); //标记要显示窗体的类型,综合、修改还是删除 private DBManager db = new DBManager(); ResultSet rs ; String sql=new String(); JRadioButton jRByName = new JRadioButton(); JRadioButton jRByP = new JRadioButton(); JRadioButton jRByD = new JRadioButton(); ButtonGroup group=new ButtonGroup(); public usersearch () { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { jLabel1.setBackground(SystemColor.inactiveCaptionText); jLabel1.setFont(new java.awt.Font("Dialog", 0, 16)); xYLayout1.setWidth(423); xYLayout1.setHeight(203); this.getContentPane().setLayout(xYLayout1); jBOK.setBackground(SystemColor.inactiveCaptionText); jBOK.setFont(new java.awt.Font("Dialog", 0, 16)); jBOK.setText("确 定"); jBOK.addActionListener(new search_jBOK_actionAdapter(this)); jBCancel.setBackground(SystemColor.inactiveCaptionText); jBCancel.setFont(new java.awt.Font("Dialog", 0, 16)); jBCancel.setText("取 消"); jBCancel.addActionListener(new search_jBCancel_actionAdapter(this)); jTContent.setFont(new java.awt.Font("Dialog", 0, 16)); jTContent.setText(""); this.getContentPane().setBackground(UIManager.getColor("InternalFrame.inactiveTitleForeground")); this.setTitle("用户信息查询"); jRByName.setBackground(SystemColor.inactiveCaptionText); jRByName.setFont(new java.awt.Font("Dialog", 0, 16)); jRByName.setText("按姓名"); jRByName.addActionListener(new search_jRByName_actionAdapter(this)); jRByP.setBackground(SystemColor.inactiveCaptionText); jRByP.setFont(new java.awt.Font("Dialog", 0, 16)); jRByP.setText("按专业"); jRByP.addActionListener(new search_jRByP_actionAdapter(this)); jRByD.setBackground(SystemColor.inactiveCaptionText); jRByD.setFont(new java.awt.Font("Dialog", 0, 16)); jRByD.setText("按院系"); jLabel1.setText("请输入查询条件:"); jRByD.addActionListener(new search_jRByD_actionAdapter(this)); group.add(jRByName); group.add(jRByP); group.add(jRByD); this.getContentPane().add(jLabel1, new XYConstraints(26, 23, 221, 34)); this.getContentPane().add(jTContent, new XYConstraints(42, 82, 180, 32)); this.getContentPane().add(jRByName, new XYConstraints(28, 152, 85, 17)); this.getContentPane().add(jRByP, new XYConstraints(133, 152, -1, 16)); this.getContentPane().add(jRByD, new XYConstraints(232, 152, 77, 16)); this.getContentPane().add(jBOK, new XYConstraints(318, 38, 80, 29)); this.getContentPane().add(jBCancel, new XYConstraints(314, 85, 80, 29)); } //设置标签内容 void setLabelText(String content){ jLabel1.setText(content); } //设置查询类型 void setType(String i){ searchType=i; } //设置要显示的人员信息窗体 void setShowType(String type){ infoShowType=type; } //退出 void jBCancel_actionPerformed(ActionEvent e) { this.dispose(); } void listShow(){ usershow userList=new usershow(); Dimension dlgSize = userList.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); userList.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); userList.pack(); userList.show() ; userList.resultShow(rs); } //执行查询操作 void jBOK_actionPerformed(ActionEvent e) { if(jTContent.getText().equals("")){ JOptionPane.showMessageDialog(null, "查询值不能为空!"); } else{ sql = "select * from users "; //根据选项判断查询的类型 if (searchType == "username") { sql = sql + " where username='" + jTContent.getText().toString() + "'"; } else if (searchType == "professional") { sql = sql + " where professional=" + Integer.parseInt(jTContent.getText()); } else if (searchType == "department") { sql = sql + "where department='" + jTContent.getText().toString() + "'"; } rs = db.getResult(sql); try { if (rs.first()) { //判断记录是否存在 listShow(); this.dispose(); } else { JOptionPane.showMessageDialog(null, "此人不存在!"); } } catch (SQLException ex) { JOptionPane.showMessageDialog(null, " search failed!"); } } } //响应单选按钮选中时的事件 void jRByName_actionPerformed(ActionEvent e) { jLabel1.setText("请输入要查询人员的姓名:"); jTContent.setEnabled(true); setType("Name"); } void jRByP_actionPerformed(ActionEvent e) { jLabel1.setText("请输入要查询人员所在专业:"); jTContent.setEnabled(true); setType("professional"); } void jRByD_actionPerformed(ActionEvent e) { jLabel1.setText("请输入要查询人员所在院系:"); jTContent.setEnabled(true); setType("department"); }}//*******************以下是监听类部分*******************class search_jBCancel_actionAdapter implements java.awt.event.ActionListener { usersearch adaptee; search_jBCancel_actionAdapter(usersearch adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jBCancel_actionPerformed(e); }}class search_jBOK_actionAdapter implements java.awt.event.ActionListener { usersearch adaptee; search_jBOK_actionAdapter(usersearch adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jBOK_actionPerformed(e); }}class search_jRByName_actionAdapter implements java.awt.event.ActionListener { usersearch adaptee; search_jRByName_actionAdapter(usersearch adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jRByName_actionPerformed(e); }}class search_jRByP_actionAdapter implements java.awt.event.ActionListener { usersearch adaptee; search_jRByP_actionAdapter(usersearch adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jRByP_actionPerformed(e); }}class search_jRByD_actionAdapter implements java.awt.event.ActionListener { usersearch adaptee; search_jRByD_actionAdapter(usersearch adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jRByD_actionPerformed(e); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -