请JAVA大神帮忙写一个窗口界面的程序,可以读写文件的,有一定的功能的就行,谢谢啦
1个回答
展开全部
package test.xxl;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
public class Demo0602 extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private static JButton open=null;
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public Demo0602(){
open=new JButton("open");
this.add(open);
this.setBounds(400, 200, 100, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
open.addActionListener(this);
}
/**
* @param args
*/
public static void main(String[] args) {
new Demo0602() ;
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
// 目录和文件都可以选择
// jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
//只能选择文件
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 自定义过滤器
// FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
jfc.setFileFilter(new FileFilter() {
// 设置文件格式
private String description = "txt";
@Override
public String getDescription() {
return description;
}
@Override
public boolean accept(File f) {
if(f != null){
if(f.isDirectory()){
return false ;
}
String extension = getExtension(f) ;
if(extension != null && extension.equalsIgnoreCase(this.description))
return true ;
}
return false;
}
private String getExtension(File f) {
if (f != null) {
String filename = f.getName();
int i = filename.lastIndexOf('.');
if (i > 0 && i < filename.length() - 1) {
return filename.substring(i + 1).toLowerCase();
}
}
return null;
}
}) ;
File file=jfc.getSelectedFile();
if(file != null){
showFileContent(file) ;
} else {
System.out.println("不支持的文件类型");
}
}
/**
* 用于显示文件内容
* @param file
*/
public void showFileContent(File file){
JFrame jFrame = new JFrame(file.getAbsolutePath()) ;
BufferedReader br = null ;
JTextArea jta= new JTextArea() ;
int line = 1 ;
try{
br = new BufferedReader(new FileReader(file)) ;
String tmp = null ;
while((tmp = br.readLine()) != null){
jta.setText(jta.getText() + "\r\n" + tmp) ;
line ++ ;
}
br.close() ;
} catch (IOException e) {
e.printStackTrace() ;
}
JScrollPane scrollPane = new JScrollPane(jta);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jFrame.add(scrollPane, BorderLayout.CENTER) ;
jFrame.setBounds(0, 0, screenSize.width , screenSize.height ) ;
jFrame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询