我编了一道程序,在窗口的文本区中中文显示了乱码,但在生成的TXT文本中却显示了中文..我的程序不知道哪里错了,请高手帮我修改,修改以后请全代码给我.多谢..我要在窗口文本区中显示中文.我用的是eclipse编程工具. 以下是程序代码:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Myproject extends Frame implements ActionListener {
public static void main (String args[]) throws Exception{
new Myproject();
}
private FileDialog dialog,dialog1;
private Button b1,b2,b3;
private TextArea ta;
private TextField tf1,tf2;
private Label l1,l2,l3;
String fname,fname1;
Myproject() throws IOException{
Frame fr = new Frame("合并文件");
fr.setLayout(null);
ta = new TextArea();
tf1 = new TextField();
tf2 = new TextField();
b1 = new Button("打开一");
b2 = new Button("打开二");
b3 = new Button("在文本域中显示第三文件");
l1 = new Label("请输入文件一路径:");
l2 = new Label("请输入文件二路径:");
l3 = new Label("显示第三个文件内容:");
fr.add(l1);
fr.add(l2);
fr.add(l3);
fr.add(b1);
fr.add(b2);
fr.add(b3);
fr.add(tf1);
fr.add(tf2);
fr.add(ta);
fr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}});
l1.reshape(20,40,100,20);
l2.reshape(20,70,100,20);
l3.reshape(20,100,250,30);
b1.reshape(580,40,150,20);
b2.reshape(580,70,150,20);
b3.reshape(200,100,530,30);
tf1.reshape(150,40,400,20);
tf2.reshape(150,70,400,20);
ta.reshape(10,140,730,430);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
fr.setSize(750,580);
fr.setBackground(Color.YELLOW);
fr.setResizable(false);
r.setVisiblsete(true);
dialog = new FileDialog(fr,"My FileDialog");
dialog.setVisible(false);
dialog1 = new FileDialog(fr,"My FileDialog");
dialog1.setVisible(false);
}
void copyText() throws Exception {
String s1 = "",s2 = "",s3 = "";
long filePoint = 0;
RandomAccessFile file = new RandomAccessFile(fname,"r");
long fileLength = file.length();
while(filePoint < fileLength ){
s1 += file.readLine() + "\r\n";
filePoint = file.getFilePointer();
}
file.close();
filePoint = 0;
RandomAccessFile file1 = new RandomAccessFile(fname1,"r");
fileLength = file1.length();
while(filePoint<fileLength ){
s2 += file1.readLine() + "\r\n";
filePoint = file1.getFilePointer();
}
file1.close();
s3 = s1 + "\r\n" + s2;
RandomAccessFile file2 = new RandomAccessFile("E://hao","rw");
file2.writeBytes(s3);
ta.setText(s3);
file2.close();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == b1) {
dialog.setVisible(true);
fname = dialog.getDirectory() + dialog.getFile();
tf1.setText(fname);
}
if(event.getSource() == b2) {
dialog1.setVisible(true);
fname1 = dialog1.getDirectory() + dialog1.getFile();
tf2.setText(fname1);
}
if(event.getSource() == b3) {
try {
copyText();
} catch (Exception e) {}
}
}
}
[此贴子已经被作者于2006-6-15 23:18:00编辑过]
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Myproject extends Frame implements ActionListener {
public static void main (String args[]) throws Exception{
new Myproject();
}
private FileDialog dialog,dialog1;
private Button b1,b2,b3;
private TextArea ta;
private TextField tf1,tf2;
private Label l1,l2,l3;
String fname,fname1;
Myproject() throws IOException{
Frame fr = new Frame("合并文件");
fr.setLayout(null);
ta = new TextArea();
tf1 = new TextField();
tf2 = new TextField();
b1 = new Button("打开一");
b2 = new Button("打开二");
b3 = new Button("在文本域中显示第三文件");
l1 = new Label("请输入文件一路径:");
l2 = new Label("请输入文件二路径:");
l3 = new Label("显示第三个文件内容:");
fr.add(l1);
fr.add(l2);
fr.add(l3);
fr.add(b1);
fr.add(b2);
fr.add(b3);
fr.add(tf1);
fr.add(tf2);
fr.add(ta);
fr.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}});
l1.reshape(20,40,100,20);
l2.reshape(20,70,100,20);
l3.reshape(20,100,250,30);
b1.reshape(580,40,150,20);
b2.reshape(580,70,150,20);
b3.reshape(200,100,530,30);
tf1.reshape(150,40,400,20);
tf2.reshape(150,70,400,20);
ta.reshape(10,140,730,430);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
fr.setSize(750,580);
fr.setBackground(Color.YELLOW);
fr.setResizable(false);
fr.setVisible(true);
dialog = new FileDialog(fr,"My FileDialog");
dialog.setVisible(false);
dialog1 = new FileDialog(fr,"My FileDialog");
dialog1.setVisible(false);
}
void copyText() throws Exception {
String s1 = "",s2 = "",s3 = "";
long filePoint = 0;
RandomAccessFile file = new RandomAccessFile(fname,"r");
long fileLength = file.length();
while(filePoint < fileLength ){
s1 += file.readLine() + "\r\n";
filePoint = file.getFilePointer();
}
file.close();
filePoint = 0;
RandomAccessFile file1 = new RandomAccessFile(fname1,"r");
fileLength = file1.length();
while(filePoint<fileLength ){
s2 += file1.readLine() + "\r\n";
filePoint = file1.getFilePointer();
}
file1.close();
s3 = s1 + "\r\n" + s2;
RandomAccessFile file2 = new RandomAccessFile("E://hao","rw");
file2.writeBytes(s3);
byte[] b=s3.getBytes("iso8859-1");
String ss=new String(b);
ta.setText(ss);
file2.close();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == b1) {
dialog.setVisible(true);
fname = dialog.getDirectory() + dialog.getFile();
tf1.setText(fname);
}
if(event.getSource() == b2) {
dialog1.setVisible(true);
fname1 = dialog1.getDirectory() + dialog1.getFile();
tf2.setText(fname1);
}
if(event.getSource() == b3) {
try {
copyText();
} catch (Exception e) {}
}
}
}
你的程序有几处错误,连编译都通过不了
我现在已经帮你改过来了
你的之所以会显示乱码,是因为字符的编码不同,
我们中文用的是GB2312或者GBK
而西方用的是iso8859-1所以要互相转换一下就可以了
----------------解决方案--------------------------------------------------------
多谢版主!!
----------------解决方案--------------------------------------------------------