1, read the test paper and then do the problem calculation points
File file1=NewFile ("D:\\file", "test.txt"); Try{FileReader in1=NewFileReader (FILE1); BufferedReader in2=NewBufferedReader (in1); String s; intCount=0; for(;(s=in2.readline ())! =NULL;){ if(!s.startswith ("-") {System.out.println (s); } Else{System.out.print ("Input your Answer:"); S=s.replaceall ("-", "" "); String Daan; Scanner Scanner1=NewScanner (system.in); Daan=Scanner1.next (); if(S.equals (Daan)) {count++; }}} System.out.println ("Point" +count); } Catch(Exception e) {System.out.println (E.getmessage ()); }
2, use card layout to do two pages, to input and output
PackageTestwin;ImportJava.awt.EventQueue;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.io.*;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;ImportJavax.swing.JTextArea;ImportJavax.swing.JMenuBar;ImportJavax.swing.JMenu;ImportJavax.swing.JMenuItem;Importjava.awt.CardLayout; Public classTestwinImplementsActionListener {//using a card layout, call two pages from the menu bar PrivateJFrame frame; File File1; JMenuItem input=NewJMenuItem ("Input"); JMenuItem Show=NewJMenuItem ("Show"); Inputarea InputMessage; CardLayout Card; JPanel Panel; JTextArea TextArea; /*** Launch the application. */ Public Static voidMain (string[] args) {Eventqueue.invokelater (NewRunnable () { Public voidrun () {Try{Testwin window=NewTestwin (); Window.frame.setVisible (true); } Catch(Exception e) {e.printstacktrace (); } } }); } /*** Create the application. */ PublicTestwin () {initialize (); } /*** Initialize The contents of the frame. */ Private voidInitialize () {file1=NewFile ("D:\\file", "test.txt"); Frame=NewJFrame (); Frame.setbounds (100, 100, 450, 300); Frame.setdefaultcloseoperation (Jframe.exit_on_close); JMenuBar MenuBar=NewJMenuBar (); Frame.setjmenubar (MenuBar); JMenu Menu=NewJMenu ("\u83dc\u5355"); Menubar.add (menu); Menu.add (input); Menu.add (show); Input.addactionlistener ( This); Show.addactionlistener ( This); TextArea=NewJTextArea (12,50); InputMessage=NewInputarea (FILE1); Card=Newcardlayout (); Panel=NewJPanel (); Panel.setlayout (card); Panel.add ("Input", InputMessage); Panel.add ("Show", TextArea); Frame.add (panel); } @Override Public voidactionperformed (ActionEvent e) {//TODO Auto-generated method stubs if(E.getsource () = =input) {card.show (panel,"Input"); } Else if(E.getsource () = =show) { intNumber=1; Textarea.settext (NULL); Card.show (panel,"Show"); Try{randomaccessfile in=NewRandomaccessfile (File1, "R"); String name=NULL; for(;(Name=in.readutf ())! =NULL;) {textarea.append ("\ n" + "Name:" +name); Textarea.append ("\ T" +In.readutf ()); Textarea.append ("\ T" +In.readutf ()); Textarea.append ("\ n----------------------------------------------------------------------------"); number++; } in.close (); } Catch(Exception E1) {System.out.println (E1.getmessage ()); System.out.println ("2222"); } } }}
View Code
PackageTestwin;ImportJavax.swing.JPanel;ImportJava.awt.Color;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.io.*;Importjavax.swing.*;Importjavax.swing.BoxLayout;ImportJavax.swing.JLabel; Public classInputareaextendsJPanelImplementsactionlistener{/*** Create the panel. */File file1; Randomaccessfile out1;//Use a box layout and then enter the file with a random streamBox Basebox,box1,box2; JButton button1; PrivateJLabel Lblnewlabel; PrivateJLabel Lblnewlabel_1; PrivateJLabel lblnewlabel_2; PrivateJLabel Lblnewlabel_3; PrivateJTextField Text1; PrivateJTextField Text2; PrivateJTextField Text3; PrivateJButton button; PublicInputarea (File f) {setforeground (Color.cyan); This. file1=F; Basebox=Box.createhorizontalbox (); Box1=Box.createverticalbox (); Box2=Box.createverticalbox (); Basebox.add (Box1); Lblnewlabel=NewJLabel ("\u8f93\u5165\u59d3\u540d"); Box1.add (Lblnewlabel); Lblnewlabel_1=NewJLabel ("\u8f93\u5165qq"); Box1.add (Lblnewlabel_1); Lblnewlabel_2=NewJLabel ("\U8F93\U5165\U7535\U8BDD"); Box1.add (lblnewlabel_2); Lblnewlabel_3=NewJLabel ("\u5355\u51fb\u5f55\u5165"); Box1.add (Lblnewlabel_3); Basebox.add (BOX2); Text1=NewJTextField (); Box2.add (Text1); Text1.setcolumns (10); Text2=NewJTextField (); Box2.add (TEXT2); Text2.setcolumns (10); Text3=NewJTextField (); Box2.add (TEXT3); Text3.setcolumns (10); Button=NewJButton ("\u5f55\u5165"); Box2.add (button); Add (Basebox); Button.addactionlistener ( This); } @Override Public voidactionperformed (ActionEvent e) {//TODO Auto-generated method stubs Try{randomaccessfile out1=NewRandomaccessfile (File1, "RW"); LongLength=file1.length (); Out1.seek (length); Out1.writeutf ("Name:" +Text1.gettext ()); Out1.writeutf ("QQ:" +Text2.gettext ()); Out1.writeutf ("Phone:" +Text3.gettext ()); Out1.close (); Text1.settext (NULL); Text2.settext (NULL); Text3.settext (NULL); } Catch(Exception E1) {System.out.println (E1.getmessage ()); } } }
View Code
3
IO Stream + database after class exercises