package com.projet;
/**
 * 
 * @Classname:AutomaticReader
 * @describe:实现自动阅读器
 * 1.实现选择文件(IO流)
 * 2.实现保存文件  (IO流)
 * 3.实现自动滚动以及停止滚定(线程知识)
 * 4.实现设置自动滚动速度(事件监听)       
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;public class AutomaticReader extends JFrame implements ActionListener, Runnable {
   /**设置面板,调控自动阅读的速度等*/
       JPanel SetPanel;
       /**文本编辑面板,阅读区*/
       JPanel EditPanel;
       JTextArea EditArea;
       /**开关按钮*/
       JButton SwitchButton;
       JLabel TextLabel;
       JLabel ColorChose;
       /**阅读速度选择框*/
       JComboBox<Object> SpeedBox;
       JComboBox<Object> ColorBox;
       JScrollPane pane;
       /**升降条*/
       JScrollBar bar;
       /**
        * 菜单栏等组件
        */
       JMenuBar MeauBar;
       JMenu[] meau;
       JMenuItem[] FileItem;
       JMenuItem[] HelpItem;
       JMenuItem[] AboutItem;
       /**速度数组*/
       private String[] Speed = {"1", "2", "3", "4", "5"};
       static String[] meauText = {"文件", "格式", "帮助"};
       static String[] FileText = {"新建", "打开", "保存"};
       static String[] HelpText = {"使用帮助", "关于软件"};
       static String[] AboutText = {"状态显示", "时间显示", "字体设置"};
       static String[] ColorText = {"蓝色", "红色", "黑色", "白色", "灰色", "浅粉色"};
       public AutomaticReader() {
        super("文本自动阅读器");
        MeauSet();
        SetPanel();
        EditPanel();
        this.setBounds(300, 400, 530, 400);
        this.setVisible(true);
}
       public void MeauSet() {
        MeauBar = new JMenuBar();
        meau = new JMenu[3];
        FileItem = new JMenuItem[3];
        HelpItem = new JMenuItem[2]; 
        AboutItem = new JMenuItem[3];
        for (int i = 0; i < meau.length; i++) {
    meau[i] = new JMenu(meauText[i]);
    MeauBar.add(meau[i]);
}
        for (int i = 0; i < FileItem.length; i++) {
FileItem[i] = new JMenuItem(FileText[i]);
//TODO 增加事件监听 
FileItem[i].addActionListener(this);
meau[0].add(FileItem[i]);
}
        for (int i = 0; i < HelpItem.length; i++) {
        HelpItem[i] = new JMenuItem(HelpText[i]);
      //TODO 增加事件监听 
        meau[2].add(HelpItem[i]);
}
        for (int i = 0; i < AboutItem.length; i++) {
        AboutItem[i] = new JMenuItem(AboutText[i]);
      //TODO 增加事件监听 
        meau[1].add(AboutItem[i]);
}
        this.setJMenuBar(MeauBar);
       }
       public void SetPanel() {
        SetPanel  = new JPanel();
        TextLabel = new JLabel("选择滚动速度");
        ColorChose = new JLabel("背景颜色");
        SpeedBox = new JComboBox<>(Speed);
        ColorBox = new JComboBox<>(ColorText);  
        ColorBox.addActionListener(this);
        SetPanel.add(ColorChose);
        SetPanel.add(ColorBox);
        SetPanel.add(TextLabel);
        SetPanel.add(SpeedBox);
        SwitchButton = new JButton("开/关(自动阅读)");
        SwitchButton.addActionListener(this);
        SetPanel.add(SwitchButton);
        this.add(SetPanel, BorderLayout.NORTH);
       }
       public void EditPanel() {
        EditArea = new JTextArea();
        Font font = new Font("微软雅黑", Font.BOLD, 20);
        EditArea.setFont(font);
        pane = new JScrollPane(EditArea);
        bar = pane.getVerticalScrollBar();
       
        this.add(pane);
       }
@Override
public void actionPerformed(ActionEvent e) {
  String label = e.getActionCommand();
  //新建文件
  if(label.equals(FileText[0])) {
 //清空文本区域 
 EditArea.setText("");
  }if(label.equals(FileText[1])) {
  FileDialog openFile = new FileDialog(this, "打开文件", FileDialog.LOAD);
openFile.setVisible(true);
// 获取文件路径
String filePath = openFile.getDirectory() + openFile.getFile();
BufferedReader bufferead = null;
FileReader fileread = null;
try {
fileread = new FileReader(filePath);
bufferead = new BufferedReader(fileread);

String content = "";
String CountString = "";
while((content = bufferead.readLine()) != null) {
CountString += content + "\r\n";
}
EditArea.setText(CountString);
} catch (Exception e1) {
e1.printStackTrace();
}finally {
try {
fileread.close();
bufferead.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
  }if(label.equals(FileText[2])) {
  FileDialog openFile = new FileDialog(this, "保存文件", FileDialog.SAVE);
  openFile.setVisible(true);
  String filepath = openFile.getDirectory() + openFile.getFile();
  BufferedWriter bufferwrite = null;
  FileWriter filewrite = null;  
  try {
filewrite = new FileWriter(filepath);
bufferwrite = new BufferedWriter(filewrite);
bufferwrite.write(EditArea.getText());
} catch (IOException e1) {
e1.printStackTrace();
}finally {
try {
bufferwrite.close();
filewrite.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
  }if(e.getSource() == ColorBox) {
  int index  = ColorBox.getSelectedIndex();
  switch(index) 
  {
  case 0:
  EditArea.setBackground(Color.BLUE);
  break;
  case 1:
  EditArea.setBackground(Color.RED);
  break;
  case 2:
  EditArea.setBackground(Color.BLACK);
  break;
  case 3:
  EditArea.setBackground(Color.WHITE);
  break;
  case 4:
  EditArea.setBackground(Color.GRAY);
  break;
  case 5:
  EditArea.setBackground(Color.pink);
  break;
  }
  }if(label.equals("开/关(自动阅读)")) {
  Thread thread = new Thread(this);
  thread.start();
  }
  
}  public static void main(String[] args) {
 AutomaticReader automaticReader = new AutomaticReader();
 }
@Override
public void run() {
bar.setValue(bar.getValue() + 10);
}
}if(label.equals("开/关(自动阅读)")) {
  Thread thread = new Thread(this);
  thread.start();public void run() {
bar.setValue(bar.getValue() + 10);
}我想问一下各位大哥,为什么我设置了点击按钮后启动自动滚动的线程却只能点一下动一下而不是自动滚动??求解答