📄 massagechoosing.java
字号:
/**MassageChoosing.java*/
package mfg;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Vector;
import java.sql.*;
public class MassageChoosing
{
String[] s1={"按标题查询","按作者查询"};
String[] s2={"模糊查询","精确查询"};
static JFrame f1=null; /**查询窗体*/
static Container content1=null; /***/
static JPanel p1=null; /***/
static JTextField t1=null; /**关键字填写域*/
static JComboBox c1=null; /**选择是按种内容查询*/
static JComboBox c2=null; /**选择查询类型*/
static JButton b1=null; /**查询按钮*/
static JButton b2=null; /**查询窗口中退出按钮*/
static JButton jb1=null; /**关于按钮*/
static JFrame f2=null; /**查询结果显示窗口*/
static Container content2=null; /***/
static JPanel p2=null; /***/
static JButton b3=null; /**详细信息按钮*/
static JButton b4=null; /**查询结果窗口中退出按钮*/
static JLabel l2=null; /***/
static String[] name={"文章编号","打开方式","文章名称"}; /**查询结果页面列名称*/
static DefaultTableModel dt=null; /**表格类型*/
static JTable table=null; /**表格*/
static JPanel p3=null; /***/
static JTextArea ta=null; /**摘要显示区域*/
static String[] lwdg=new String[16]; /**存储摘要信息*/
public MassageChoosing()
{
f1=new JFrame("文献信息查询系统!");
content1=f1.getContentPane();
p1=new JPanel();
t1=new JTextField("关键字");
c1=new JComboBox(s1);
c2=new JComboBox(s2);
b1=new JButton("查询");
b2=new JButton("退出");
jb1=new JButton("关于");
}
public void jiemian1()
{
//new MassageChoosing();
/**生成查询页面*/
content1.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,3));
p1.setBorder(BorderFactory.createTitledBorder("信息查询"));
p1.add(t1);
p1.add(c1);
p1.add(c2);
p1.add(b1);
p1.add(jb1);
p1.add(b2);
t1.addActionListener(new ActionListener() { /**TextField键盘事件监听器*/
public void actionPerformed(ActionEvent e1)
{
MassageChoosing.searching();
f1.dispose(); /**隐藏查询窗口*/
}
});
b1.addActionListener(new ActionListener() { /**查询按钮监听器*/
public void actionPerformed(ActionEvent e2)
{
MassageChoosing.searching();
f1.dispose(); /**隐藏查询窗口*/
}
});
b2.addActionListener(new ActionListener() { /**退出按钮监听器*/
public void actionPerformed(ActionEvent e3)
{
System.exit(0); /**结束查询关闭窗口*/
}
});
jb1.addActionListener(new ActionListener() { /**关于按钮监听器*/
public void actionPerformed(ActionEvent e3)
{
JOptionPane.showMessageDialog(f1,"文献信息查询系统 \n"+"作者:孟凡刚 学号:2003427016 \n"+"班级:网络工程2班"); /**显示作者信息*/
}
});
content1.add(p1);
f1.pack();
f1.setVisible(true);
f1.addWindowListener(new WindowAdapter() { /**窗体事件监听器*/
public void windowClosing(WindowEvent e4)
{
System.exit(0);
}
});
}
/**查询并显示结果*/
public static void searching()
{
/**生成查询结果显示页面*/
f2=new JFrame("查询结果");
content2=f2.getContentPane();
p2=new JPanel();
b3=new JButton("详细信息");
b4=new JButton("退出");
l2=new JLabel();
dt=new DefaultTableModel(name,0);
table=new JTable(dt);
p3=new JPanel();
ta=new JTextArea(10,86);
content2.setLayout(new BorderLayout());
p2.setLayout(new GridLayout(1,2));
p2.add(b3);
p2.add(l2);
p2.add(b4);
table.setPreferredScrollableViewportSize(new Dimension(150,60));
//table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
JScrollPane s1=new JScrollPane(table);
p3.setLayout(new GridLayout(1,1));
ta.setTabSize(10);
ta.setFont(new Font("标楷体",Font.BOLD,16));
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
p3.add(new JScrollPane(ta));
content2.add(p2,BorderLayout.NORTH);
content2.add(s1,BorderLayout.CENTER);
content2.add(p3,BorderLayout.SOUTH);
f2.pack();
f2.show();
f2.setBounds(50,50,850,500);
/**创建数据库连接*/
String result;
int result2;
String sqlselect;
String co1=null;
Statement stmt;
Connection con;
String url;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); /**注册数据源*/
url="jdbc:odbc:mfglunwen";
con=DriverManager.getConnection(url); /**连接数据库*/
stmt=con.createStatement();
//String co1=null;
co1=t1.getText();
/**判断查询方式,由此产生查询语句*/
if(c1.getSelectedItem().equals("按标题查询"))
{
if(c2.getSelectedItem().equals("模糊查询"))
{
sqlselect="Select lwnum,lwopenway,lwtitle,lwdigest"+" from lunwen"+" where lwtitle like '%"+co1+"%'";
}
else
{
sqlselect="Select lwnum,lwopenway,lwtitle,lwdigest"+" from lunwen"+" where lwtitle ='"+co1+"'";
}
}
else
//if(c1.equals("按作者查询"))
{
if(c2.getSelectedItem().equals("模糊查询"))
sqlselect="Select lwnum,lwopenway,lwtitle,lwdigest"+" from lunwen"+" where lwauthor like '%"+co1+"%'";
else
{
sqlselect="Select lwnum,lwopenway,lwtitle,lwdigest"+" from lunwen"+" where lwauthor='"+co1+"'";
}
}
final ResultSet rs=stmt.executeQuery(sqlselect); /**查询数据库,并将结果存放在rs中*/
int i=0,j=0;
/**将结果显示在表格中*/
while(rs.next())
{
dt.addRow(new Vector()); /**在表格末尾新产生一行*/
j=0;
result="";
result=rs.getString(1);
table.setValueAt(result,i,j);
j++;
result=rs.getString(2);
/**
if(result.equals("*.pdf"))
{
dt.isCellEditable(i,j);
dt.setValueAt(icon1,i,j);
}
if(result.equals("*.doc"))
{
dt.isCellEditable(i,j);
dt.setValueAt(icon2,i,j);
}
*/
table.setValueAt(result,i,j);
j++;
result=rs.getString(3);
table.setValueAt(result,i,j);
lwdg[i]=rs.getString(4);
j++;
i++;
}
rs.close();
stmt.close();
}
catch (Exception e)
{
ta.setText(e.getMessage()); /**如在数据库操作中产生异常,将异常信息显示在TextField中*/
}
table.addMouseListener(new MouseAdapter() { /**表格单击事件监听器*/
public void mouseClicked(MouseEvent evt)
{
if(evt.getClickCount()==1)
{
Point pt=evt.getPoint();
int rowIndex=table.rowAtPoint(pt); /**记录单击表格的行坐标*/
//System.out.println("aaa"+dtrow+"aaa"+lwdg[rowIndex]);
ta.setText(dt.getValueAt(rowIndex,2).toString()+":\n 摘要: \n"+lwdg[rowIndex]); /**显示点击表格相应行的摘要信息*/
}
}
});
f2.addWindowListener(new WindowAdapter() { /**窗体事件监听器*/
public void windowClosing(WindowEvent e5)
{
//System.exit(1);
f2.dispose(); /**隐藏查询结果显示窗口*/
f1.setVisible(true); /**显示查询窗口*/
return;
}
});
b4.addActionListener(new ActionListener() { /**退出按钮监听器*/
public void actionPerformed(ActionEvent e3)
{
//System.exit(1);
f2.dispose(); /**隐藏查询结果显示窗口*/
f1.setVisible(true); /**显示查询窗口*/
return;
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -