花哥哥的瞎说:
这个小程序出现了一个不能理解的问题,后来被解决了,在代码中的提到了问题的出现所在,我至今不能理解,可能需要看源码。程序对很多大佬来说不难理解,我注释写得很详细了。
瞎闹:
/*
在此发现细节问题:GUI界面中,Swing监听器的事务是由一个线程去执行,但是如果你这个监听器里面的事件是一个过程的的话,比如这里的输入流,Java是不会理会你的这个
过程,这里的加载显示就是一个执行的过程,如果你新建一个线程去运行的话,加载的进度条是不会显示,但该监听器的内容还会执行完,再次所以在监听里面在建立一个线程。
*/
package JFileChooser;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InterruptedIOException;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ProgressMonitor;
import javax.swing.ProgressMonitorInputStream;
public class FileChoose extends JFrame{
private JTextArea text;
private JLabel label;
private JTextField textfield;
private JButton ensurebutton,updatebutton;
private ProgressMonitorInputStream in;
private ProgressMonitor p;
private File file;
JPanel toppanel;
FileChoose(){
Font font = new Font("",Font.BOLD,14);
text =new JTextArea(20,30);
text.setFont(font);
label=new JLabel("读取文件名称:");
label.setFont(font);
textfield=new JTextField(10);
textfield.setFont(font);
ensurebutton=new JButton("浏览");
updatebutton=new JButton("主题");
updatebutton.setFont(font);
ensurebutton.setFont(font);
this.setTitle("进度模块");
toppanel=new JPanel();
toppanel.add(label);
toppanel.add(textfield);
toppanel.add(ensurebutton);
toppanel.add(updatebutton);
this.setSize(330,300);//设置窗口的大小
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击窗口关闭按钮结束窗口所在的应用
this.add(toppanel, BorderLayout.NORTH);
this.add(text,BorderLayout.CENTER);//将组件加入窗口
this.setBounds(600,300, 420, 500);
ensurebutton.addActionListener(new Read());
updatebutton.addActionListener(new Theme());
}
class Read implements ActionListener{//确定打开文件的监听
public void actionPerformed(ActionEvent b) {
text.setText("");
JFileChooser filechoose=new JFileChooser("D:/");
int flag=filechoose.showOpenDialog(null);
if(flag==JFileChooser.APPROVE_OPTION) {
file=filechoose.getSelectedFile();//获取选中的文件
String filename=filechoose.getSelectedFile().getName();
textfield.setText(filename);
Task task=new Task(file);//建立任务
Thread t=new Thread(task);
t.start();//执行线程
}
}
}
class Theme implements ActionListener{//确定打开文件的监听
public void actionPerformed(ActionEvent b) {
Color newcolor=JColorChooser.showDialog(null,"调色板",Color.BLUE);
if(newcolor!=null) {
//textfield.setBackground(newcolor);
updatebutton.setBackground(newcolor);
ensurebutton.setBackground(newcolor);
toppanel.setBackground(newcolor);
text.setBackground(newcolor);
}
}
}
public void openfile(File file){//打开文件的方法
byte[] a=new byte[2];//因为中文是两个字符,所以每次读取两个字符。
try {
int length;//读取的文件个数
FileInputStream input=new FileInputStream(file);//制作一个文件输入流
ProgressMonitorInputStream in=new ProgressMonitorInputStream(null,"正在读取文件...",input);//构造一个对象来监视输入流的进度
ProgressMonitor p=in.getProgressMonitor();//获取此流所使用的ProgressMonitor对象
while((length=in.read(a))!=-1) {
String s=new String(a,0,length);
text.append(s);
Thread.sleep(10);//文件太小了,所以休眠方便看到进度条。
}
}catch(InterruptedIOException e) {
JOptionPane.showMessageDialog(null, "取消读取!");
}
catch(FileNotFoundException c) {
JOptionPane.showMessageDialog(null, "文件不存在");
}
catch(Exception e) {
System.out.println(e+"未知错误!");
}
}
class Task implements Runnable{ //打开文件的对象
private File file;
Task(File file){
this.file=file;
}
public synchronized void run(){
openfile(file);//打开文件步骤的同步方法
}
}
public static void main(String[] args) {
new FileChoose();
}
}
供图:
话痨:
这个鬼东西真的花费我很多脑子!
花哥哥严肃说:
所学的知识均从课本,课外读物,网上收集资料,如果哪有侵权惹到您不开心丫,此外还有我也是小白,如果哪有认识不对的地方还请您抽我一耳光说我哪错了,我一定会感谢,联系企鹅号:184820911,有哪不明白或没事都可以找我,陪聊,陪吃不陪睡。