Simple Document Viewer (programmer Using Tools)

zhaozj2021-02-16  56

Import java.io. *; import javax.swing. *; import java.awt. *; import java.awt.event.actionListener; import java.awt.event.ActionEvent;

public class TestSearch extends JFrame implements ActionListener {private JTextField tf1, tf2; private JButton b1, b2; private JTextArea ta; private JPanel p, p1, p2; private JLabel l1, l2; public TestSearch () {super ( "Search"); Container c = getContentPane (); p = new jPanel (2,1)); p1 = new jPanel (); p2 = new jPanel (); l1 = new jlabel ("Find file"); l2 = NEW JLABEL ("The content you want to find"); ta = new jtextAREA (); tf1 = new jtextfield (20); tf2 = new jtextfield (18); b1 = new jbutton ("Browse"); b2 = new jbutton (" Start search "); C.SetLayout (new borderLayout ()); C.Add (BorderLayout.North, P); C.Add (BorderLayout.center, New Jscrollpane (TA)); p1.Add (L1); P1. Add (TF1); P1.Add (b1); p2.Add (L2); P2.Add (TF2); P2.Add (b2); p.Add (p1); p.Add (p2); b1.addactionListener (this); b2.addactionListener (this); setsize (400, 400); show (); public static void main (String args []) {new testsearch ();}

public void actionPerformed (ActionEvent parm1) {if (parm1.getSource () == b1) {JFileChooser chooser = new JFileChooser (); chooser.setDialogType (JFileChooser.OPEN_DIALOG); chooser.setFileSelectionMode (JFileChooser.FILES_ONLY); int i = chooser .showdialog (null, null); if (i == jfilechooser.Approve_option) {tf1.settext (chooser.getSelectedFile (). getabsolutefile (). Tostring ());}} f (parm1.getsource () == b2) {Ta.settext (""); string str1 = tf1.gettext (); string str2 = tf2.gettext (); int i = 1; try {buffreader breader = new bufferedReader (New FileReader (str1)); while (True ) {String str3 = Breader.readline (); if (str3.indexof (str2)! = - 1) {ta.append ("/" " str2 " / "" "in the" i "line of the file ! / n "); i ; continue;} i ; if (str3 == null) {ta.append (" Replenance "); Break;}}} catch (Exception E) {}}}}}}}

转载请注明原文地址:https://www.9cbs.com/read-22239.html

New Post(0)