【训练8】模式敲击键盘
使用IO流按字节读取文件,并通过线程的休眠控制读取字节的速度,再将读取的字节显示在文本域中,最后使用synchronized关键字实现暂停读取和继续读取的功能。
/*【训练8】模式敲击键盘
* 使用IO流按字节读取文件,并通过线程的休眠控制读取字节的速度,再将读取的
* 字节显示在文本域中,最后使用synchronized关键字实现暂停读取和继续
* 读取的功能。
* */
package dxc;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class Xl8 extends JFrame{
private JPanel contentPane;
private JTextArea textArea;
private JButton btnStart;
private ReadData readData;
public Xl8() {
init();
addAction();
}
public void addAction() {
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String buttonName = btnStart.getText();
if(buttonName.equals("开始打字")) {
readData.start();
btnStart.setText("敲累了,歇一会……");
}else if(buttonName.equals("敲累了,歇一会……")) {
readData.pause();
btnStart.setText("革命尚未成功,同志仍需努力……");
}else if(buttonName.equals("革命尚未成功,同志仍需努力……")) {
readData.reStart();
btnStart.setText("敲累了,歇一会……");
}
}
});
}
public void init() {
setResizable(false);
setTitle("模拟英文打字");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
//内容面板
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setBorder(new EmptyBorder(5,5,5,5));
contentPane.setLayout(new BorderLayout(0,0));
contentPane.setBackground(Color.WHITE);
//按钮面板
JPanel buttonPane = new JPanel();
contentPane.add(buttonPane,BorderLayout.SOUTH);
buttonPane.setBackground(Color.WHITE);
//开始打字按钮
btnStart = new JButton("开始打字");
buttonPane.add(btnStart);
btnStart.setFont(new Font("微软雅黑",Font.PLAIN,16));
//滚动面板
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane,BorderLayout.CENTER);
//文本域
textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
//
readData = new ReadData(textArea);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Xl8 frame = new Xl8();
frame.setVisible(true);
}
}
class ReadData2 extends Thread{
private JTextArea textArea;
static boolean flag = false;
public ReadData2(JTextArea textArea) {
this.textArea = textArea;
}
//挂起线程
synchronized void pause() {
flag = true;
}
//唤醒所有等待中的线程
synchronized void reStart() {
notifyAll();
flag = false;
}
@Override
public void run() {
FileInputStream fis = null;
try {
fis = new FileInputStream("E:\\BaiduNetdiskDownload\\2. TM(示例源码+习题答案)\\TM(示例源码+习题答案)\\sl\\20\\编程训练\\8\\白雪公主.txt");
byte[] b = new byte[1]; //可容纳一个字节的缓冲区
StringBuilder sb = new StringBuilder();
while(fis.read(b) != -1) { //循环读取文件输入流,循环读入缓冲区中。
String str = new String(b);//循环将字节内容,转换为字符串内容
sb.append(str);
synchronized(this){
while(flag) {
try {
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
textArea.setText(sb.toString());
try {
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}