📄 frame1.java
字号:
package ir;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class Frame1 extends JFrame {
JPanel contentPane;
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel4 = new JPanel();
JLabel jLabel1 = new JLabel();
JPanel jPanel2 = new JPanel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel3 = new JLabel();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JButton jButton1 = new JButton();
JPanel jPanel3 = new JPanel();
JButton jButton2 = new JButton();
BorderLayout borderLayout3 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JPanel jPanel5 = new JPanel();
BorderLayout borderLayout4 = new BorderLayout();
int selectIcon = 0;//un-index
String path=new String ("D:/");
ArrayList unicodeUse[];
String fileNameList[];
int isGenerate=0;
JLabel jLabel4 = new JLabel();
JButton jButton3 = new JButton();
TitledBorder titledBorder1;
TitledBorder titledBorder2;
JLabel jLabel5 = new JLabel();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
image1 = new ImageIcon(ir.Frame1.class.getResource("openFile.png"));
image2 = new ImageIcon(ir.Frame1.class.getResource("closeFile.png"));
image3 = new ImageIcon(ir.Frame1.class.getResource("help.png"));
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder("");
titledBorder2 = new TitledBorder("");
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(640, 480));
this.setTitle("Search");
statusBar.setText(" ");
jPanel1.setLayout(borderLayout2);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 30));
jLabel1.setText("Search Tool");
jPanel2.setMinimumSize(new Dimension(150, 200));
jPanel2.setPreferredSize(new Dimension(150, 200));
jPanel2.setLayout(null);
jLabel2.setText(" key-word");
jLabel2.setBounds(new Rectangle(11, 178, 68, 24));
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(10, 211, 112, 22));
jLabel3.setText(" option:");
jLabel3.setBounds(new Rectangle(10, 54, 67, 22));
jRadioButton1.setText("no-index");
jRadioButton1.setBounds(new Rectangle(10, 79, 111, 25));
jRadioButton1.addActionListener(new Frame1_jRadioButton1_actionAdapter(this));
jRadioButton1.setSelected(true);
jRadioButton2.setText("index");
jRadioButton2.setBounds(new Rectangle(10, 114, 110, 25));
jRadioButton2.addActionListener(new Frame1_jRadioButton2_actionAdapter(this));
jButton1.setBounds(new Rectangle(11, 256, 111, 23));
jButton1.setMaximumSize(new Dimension(71, 10));
jButton1.setMinimumSize(new Dimension(71, 10));
jButton1.setPreferredSize(new Dimension(71, 10));
jButton1.setText("search");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jPanel3.setLayout(borderLayout3);
jButton2.setBounds(new Rectangle(11, 149, 111, 24));
jButton2.setText("generate-index");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jButton2.setEnabled(false);
jPanel5.setLayout(borderLayout4);
jLabel4.setBackground(Color.white);
jLabel4.setForeground(Color.darkGray);
jLabel4.setBorder(titledBorder2);
jLabel4.setDisabledIcon(null);
jLabel4.setIconTextGap(4);
jLabel4.setText("D:/");
jLabel4.setBounds(new Rectangle(11, 29, 79, 17));
jButton3.setBounds(new Rectangle(92, 27, 28, 20));
jButton3.setText("...");
jButton3.addActionListener(new Frame1_jButton3_actionAdapter(this));
jLabel5.setBounds(new Rectangle(11, 306, 111, 21));
jPanel5.setFont(new java.awt.Font("Dialog", 0, 24));
contentPane.add(jPanel4, BorderLayout.NORTH);
jPanel4.add(jLabel1, null);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jPanel2, BorderLayout.WEST);
jPanel2.add(jLabel5, null);
jPanel2.add(jButton1, null);
jPanel2.add(jTextField1, null);
jPanel2.add(jLabel2, null);
jPanel2.add(jButton2, null);
jPanel2.add(jRadioButton2, null);
jPanel2.add(jRadioButton1, null);
jPanel2.add(jLabel3, null);
jPanel2.add(jLabel4, null);
jPanel2.add(jButton3, null);
jPanel1.add(jPanel3, BorderLayout.CENTER);
jPanel3.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jPanel5, null);
fileNameList = new String[10000];
unicodeUse=new ArrayList[65536];
//jPanel3.updateUI();
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
Frame1_AboutBox dlg = new Frame1_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
void jRadioButton2_actionPerformed(ActionEvent e) {
jRadioButton1.setSelected(false);
selectIcon=1;
jButton2.setEnabled(true);
if(isGenerate==0)
{
jButton1.setEnabled(false);
}
}
void jRadioButton1_actionPerformed(ActionEvent e) {
jRadioButton2.setSelected(false);
selectIcon=0;
jButton2.setEnabled(false);
jButton1.setEnabled(true);
}
void jButton1_actionPerformed(ActionEvent e) {
try {
String keyword;
keyword = jTextField1.getText();
ArrayList result = new ArrayList();
if (selectIcon == 0) {
File fileToCheck = new File(path);
int length= fileToCheck.list().length;
String flist[];
flist=fileToCheck.list();
for (int i = 0; i <length; i++) {
String fileName = path +"\\"+ flist[i];
String s;
String headline="";
int icon=0;
StringBuffer text = new StringBuffer();
FileInputStream is = new FileInputStream(fileName);
InputStreamReader ir = new InputStreamReader(is);
BufferedReader in = new BufferedReader(ir);
while ( (s = in.readLine()) != null)
{text.append(s);
if(icon==0)
{headline=s;icon=1;}
}
int num=text.indexOf(keyword);
if(num>=0)
{
result.add(path+"\\"+flist[i]+" "+headline);
}
}
}
else
{
ArrayList tempresult=new ArrayList();
int keywordLen=keyword.length();
for(int i=0;i<keywordLen;i++)
{
int k=(int)keyword.charAt(i);
if(i==0&&unicodeUse[k]!=null)
{
for(int j=0;j<unicodeUse[k].size();j++)
{
tempresult.add(unicodeUse[k].get(j));
}
}
else
{
for(int j=0;j<tempresult.size();j++)
{
if(unicodeUse[k].contains(tempresult.get(j))==false)
{
tempresult.remove(j);
}
}
}//ifelseend
}
int tempResultLen= tempresult.size();
System.out.println(tempresult.size());
for(int i=0;i<tempResultLen;i++)
{
int num = ((Integer)(tempresult.get(i))).intValue();
String s;
String headline="";
int icon=0;
StringBuffer temp=new StringBuffer ();
FileInputStream is=new FileInputStream(fileNameList[num]);
InputStreamReader ir=new InputStreamReader(is);
BufferedReader in=new BufferedReader(ir);
while((s=in.readLine())!=null)
{ temp.append(s);
if(icon==0)
{headline= s;icon=1;}
}
if(temp.indexOf(keyword)>=0)
{
result.add(fileNameList[num]+" "+headline);
}
}
}
String resultText=new String ("共找到"+result.toArray().length+"项");
jLabel5.setText(resultText);
JListUse list = new JListUse (result.toArray());
list.setToolTipText("double click to view the text");
jPanel5.removeAll();
jPanel5.add(list,BorderLayout.CENTER);
jPanel5.updateUI();
}
catch (FileNotFoundException ex) {
}
catch (IOException ex) {
}
}
void jButton2_actionPerformed(ActionEvent e) {
for(int j=0;j<65536;j++)
{
if(unicodeUse[j]!=null)unicodeUse[j].clear();
}
for(int j=0;j<10000;j++)
{
fileNameList[j]=null;
}
JFrame f= new JFrame("generating...");
f.setLocationRelativeTo(this);
final JProgressBar b = new JProgressBar();
b.setStringPainted(true);
b.setMinimum(0);
b.setMaximum(30);
b.setValue(0);
b.setForeground(Color.green);
b.setBorderPainted(true);
f.getContentPane().add(b);
f.pack();
f.setVisible(true);
int processing=0;
Dimension d = b.getSize();
Rectangle rect = new Rectangle(0,0,d.width,d.height);
isGenerate=1;
try {
File fileToCheck=new File(path);
String flist[];
flist=fileToCheck.list();
int length = fileToCheck.list().length;
for (int i = 0; i < length; i++)
{
if(i%100==0)
{b.setValue(++processing);
b.paintImmediately(rect);}
String fileName = path + "\\"+flist[i];
String s;
fileNameList[i] = fileName;
StringBuffer text = new StringBuffer();
FileInputStream is = new FileInputStream(fileName);
InputStreamReader ir = new InputStreamReader(is);
BufferedReader in = new BufferedReader(ir);
while ( (s = in.readLine()) != null) {
text.append(s);
}
int textLength= text.length();
for (int j = 0; j < textLength; j++)
{
int k = (int) (text.charAt(j));
if (unicodeUse[k] == null) {
unicodeUse[k] = new ArrayList();
}
if (unicodeUse[k].size()==0 ||(unicodeUse[k].get(unicodeUse[k].size()-1)).equals(new Integer(i)) == false) {
unicodeUse[k].add(new Integer(i));
}
}
}
}
catch (FileNotFoundException ex) {
}
catch (IOException ex) {
}
f.setVisible(false);
jButton1.setEnabled(true);
}
void jButton3_actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setCurrentDirectory(new File("D:/"));
int result1= chooser.showOpenDialog(this);
if(result1==JFileChooser.APPROVE_OPTION)
{
String tempstring=path;
path=chooser.getSelectedFile().getPath();
jLabel4.setText(path);
if(tempstring.equals(path)==false)
{
isGenerate=0;
if(selectIcon==1)
{
jButton1.setEnabled(false);
}
}
}
}
}
class Frame1_jRadioButton2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jRadioButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton2_actionPerformed(e);
}
}
class Frame1_jRadioButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jRadioButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jRadioButton1_actionPerformed(e);
}
}
class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class Frame1_jButton3_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton3_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -