3个回答
展开全部
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test extends JFrame implements ActionListener {
JFrame jf;
JPanel jp,jp2;
JTextArea jta;
JButton jb;
public Test(){
jf = new JFrame("查找并显示中文");
jp = new JPanel();
jp.setLayout(new GridLayout(2,1));
jf.add(jp,"Center");
jta = new JTextArea("请打开文件");
jp.add(new JScrollPane(jta));
jta.setLineWrap(true);
jp2 = new JPanel();
jp.add(jp2);
jb = new JButton("打开");
jp2.add(jb);
jb.addActionListener(this);
jf.setBounds(300, 250, 600, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("打开")){
JFileChooser jfc = new JFileChooser("C:\\");
int returnVal = jfc.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
jta.setText(null);
File file = jfc.getSelectedFile();
//添加文件操作
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while((line=br.readLine())!=null){
char[] chars = line.toCharArray();
StringBuffer sb = new StringBuffer();
for(int i = 0;i<chars.length;i++){
int num = (int)chars[i];
if(num>=19968 && num <= 171941){
sb.append(chars[i]);
}
}
if(sb.length()>0){
jta.setText(jta.getText()+sb+"\n");
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
保存为Test.java编译运行即可
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test extends JFrame implements ActionListener {
JFrame jf;
JPanel jp,jp2;
JTextArea jta;
JButton jb;
public Test(){
jf = new JFrame("查找并显示中文");
jp = new JPanel();
jp.setLayout(new GridLayout(2,1));
jf.add(jp,"Center");
jta = new JTextArea("请打开文件");
jp.add(new JScrollPane(jta));
jta.setLineWrap(true);
jp2 = new JPanel();
jp.add(jp2);
jb = new JButton("打开");
jp2.add(jb);
jb.addActionListener(this);
jf.setBounds(300, 250, 600, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("打开")){
JFileChooser jfc = new JFileChooser("C:\\");
int returnVal = jfc.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
jta.setText(null);
File file = jfc.getSelectedFile();
//添加文件操作
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while((line=br.readLine())!=null){
char[] chars = line.toCharArray();
StringBuffer sb = new StringBuffer();
for(int i = 0;i<chars.length;i++){
int num = (int)chars[i];
if(num>=19968 && num <= 171941){
sb.append(chars[i]);
}
}
if(sb.length()>0){
jta.setText(jta.getText()+sb+"\n");
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
保存为Test.java编译运行即可
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询