JAVA中如何把我创建好的文件输出到JFrame中通过点击查询显示在文本域内啊
publicclassStreamextendsJFrameimplementsActionListener{JButtona1=newJButton("查询");JBu...
public class Stream extends JFrame implements ActionListener{
JButton a1 = new JButton("查询");
JButton a2 = new JButton("退出");
JLabel l = new JLabel("学生信息显示系统");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JTextArea area = new JTextArea(20,20);
public Stream() {
this.setBounds(500, 240, 300, 200);
p1.add(a1);
p1.add(a2);
p2.add(l);
a1.addActionListener(this);
a2.addActionListener(this);
this.add(p1,BorderLayout.SOUTH);
this.add(area);
this.add(p2,BorderLayout.NORTH);
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
new Stream();
File f = new File("D:\\14");
f.mkdirs();
FileWriter fw = new FileWriter("D:/14/aa.txt");
fw.write("姓名:aa");
fw.write("\r\n性别:男");
fw.write("\r\n学号:14");
fw.close();
FileReader fr = new FileReader("D:/14/aa.txt");
int i;
while((i = fr.read())!=-1){
System.out.print((char)i);
}
fr.close();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==a1){
}else if(e.getSource()==a2){
System.exit(0);
}
}
} 展开
JButton a1 = new JButton("查询");
JButton a2 = new JButton("退出");
JLabel l = new JLabel("学生信息显示系统");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JTextArea area = new JTextArea(20,20);
public Stream() {
this.setBounds(500, 240, 300, 200);
p1.add(a1);
p1.add(a2);
p2.add(l);
a1.addActionListener(this);
a2.addActionListener(this);
this.add(p1,BorderLayout.SOUTH);
this.add(area);
this.add(p2,BorderLayout.NORTH);
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
new Stream();
File f = new File("D:\\14");
f.mkdirs();
FileWriter fw = new FileWriter("D:/14/aa.txt");
fw.write("姓名:aa");
fw.write("\r\n性别:男");
fw.write("\r\n学号:14");
fw.close();
FileReader fr = new FileReader("D:/14/aa.txt");
int i;
while((i = fr.read())!=-1){
System.out.print((char)i);
}
fr.close();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==a1){
}else if(e.getSource()==a2){
System.exit(0);
}
}
} 展开
1个回答
展开全部
public void actionPerformed(ActionEvent e) {
if(e.getSource()==a1){
String content = readFileContent("D:/14/aa.txt");
area.setText(content);
}else if(e.getSource()==a2){
System.exit(0);
}
public String readFileContent(String path) throws FileNotFoundException {
File file = new File(path);
if (!file.exists()) {
System.out.println("file not exist!");
return "";
}
FileInputStream fi = new FileInputStream(path);
BufferedReader buffReader = new BufferedReader(new InputStreamReader(fi));
StringBuilder resultStr = new StringBuilder();
String tempStr;
try {
while ((tempStr = buffReader.readLine()) != null) {
resultStr.append(tempStr);
}
return resultStr.toString();
} catch (IOException e) {
return "";
}
}
if(e.getSource()==a1){
String content = readFileContent("D:/14/aa.txt");
area.setText(content);
}else if(e.getSource()==a2){
System.exit(0);
}
public String readFileContent(String path) throws FileNotFoundException {
File file = new File(path);
if (!file.exists()) {
System.out.println("file not exist!");
return "";
}
FileInputStream fi = new FileInputStream(path);
BufferedReader buffReader = new BufferedReader(new InputStreamReader(fi));
StringBuilder resultStr = new StringBuilder();
String tempStr;
try {
while ((tempStr = buffReader.readLine()) != null) {
resultStr.append(tempStr);
}
return resultStr.toString();
} catch (IOException e) {
return "";
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询