- This program will create a frame on the Windows screen (which will always stay on top) that will read a user-specified file at an user-specified speed.
- Type this in your IDE in a class named AdvanceReader.
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class AdvanceReader {
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
JFrame frm=new JFrame("Reader");
frm.setAlwaysOnTop(true);
JTextField txt=new JTextField();
frm.setSize(new Dimension(700,100));
frm.add(txt);
txt.setEditable(false);
System.out.println("Enter file address (.txt)");
Scanner scan=new Scanner(System.in);
String x="";
for (;;) {
x=scan.nextLine();
File y=new File(x);
if (y.exists()==true) {break;}
System.out.println("No such file is present on desktop. Try again.");}
System.out.println("Enter reading speed (1-10)");
double y=scan.nextDouble();
y=(11-y)*10;
System.out.println("Welcome to reader.");
System.out.println("-----------------------------------------------");
frm.setVisible(true);
System.out.println("");
Scanner scan2=new Scanner(new File(x));
String abc="";
while (scan2.hasNextLine()){
String a=scan2.nextLine();
for (int i=0;i<a.length();i++) {
abc=abc+a.charAt(i);
txt.setText(abc);
Thread.sleep((long) y);
}
abc="";
}
System.exit(0);
}
}
- Execute the program
No comments:
Post a Comment