| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1108 人关注过本帖
标题:[求助]中文显示成乱码??
只看楼主 加入收藏
meizhelan
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-6-6
收藏
 问题点数:0 回复次数:2 
[求助]中文显示成乱码??

我编了一道程序,在窗口的文本区中中文显示了乱码,但在生成的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 
2006-06-15 23:12
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

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所以要互相转换一下就可以了


可惜不是你,陪我到最后
2006-06-16 10:39
meizhelan
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-6-6
收藏
得分:0 
多谢版主!!
2006-06-16 13:48
快速回复:[求助]中文显示成乱码??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046700 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved