import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class rr implements ActionListener
{
Button b1=new Button("b1");
Label l=new Label();
Frame m=new Frame("my hello word");
File f=new File("E:/xxx.txt");
InputStream out=null;
public void go()
{ m.setSize(300,166);
m.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
b1.addActionListener(this);
m.add(b1);
m.add(l);
m.setVisible(true);
try
{
out=new FileInputStream(f);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
} public void actionPerformed(ActionEvent e)
{
byte b[]=new byte[1024];
int i=0;
try
{
i=out.read(b);
}
catch(IOException ee)
{
ee.printStackTrace();
}
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
} l.setText(new String(b,0,i));
}
public static void main(String []args)
{
rr r=new rr();
r.go();
} } 我这个运行后为什么第一次点按钮没什么问题,再点一次好像出现了异常了
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at rr.actionPerformed(rr.java:37)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
而且l里的文字也不见了,还有每次l里面的文字都是只显示一点点,如果我把窗口拉大或拉小文字才会变长
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class rr implements ActionListener
{
Button b1=new Button("b1");
Label l=new Label();
Frame m=new Frame("my hello word");
File f=new File("E:/xxx.txt");
InputStream out=null;
public void go()
{ m.setSize(300,166);
m.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
b1.addActionListener(this);
m.add(b1);
m.add(l);
m.setVisible(true);
try
{
out=new FileInputStream(f);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
} public void actionPerformed(ActionEvent e)
{
byte b[]=new byte[1024];
int i=0;
try
{
i=out.read(b);
}
catch(IOException ee)
{
ee.printStackTrace();
}
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
} l.setText(new String(b,0,i));
}
public static void main(String []args)
{
rr r=new rr();
r.go();
} } 我这个运行后为什么第一次点按钮没什么问题,再点一次好像出现了异常了
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at rr.actionPerformed(rr.java:37)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
而且l里的文字也不见了,还有每次l里面的文字都是只显示一点点,如果我把窗口拉大或拉小文字才会变长
解决方案 »
- 编写一个函数,用多线程完成任务。如何实现在函数执行时阻塞主线程,直到任务完成主线程再继续?
- java作业-通讯录,无数据库,写入数据时出现乱码
- 怎样为组件绘制圆角边框
- 2道练习题难倒我了,求助
- commons-fileupload 可以不可以在上传文件之前取得form中参数的值?
- 求javax.media包
- 大雪天裸体跪求跪求一个简单问题答案!
- dialog右上角的关闭按钮不起作用,要怎么办?
- 晕啊,老是遇到这样的错误,到底是为什么啊???
- 大家常说主线程主线程,但是主线程能像线程那样不停得运行吗?
- 帮我看哈我的设计正常吗?
- 关于背景色的设置 this.setBackground(Color.black); 这样似乎没效果
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
} 你第一次点的时候已经把out给close掉了,你再点,它还去哪找out啊???
{
里面在out=new FileInputStream(f);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test implements ActionListener
{
Button b1=new Button("b1");
Label l=new Label();
Frame m=new Frame("my hello word");
File f=new File("E:/xxx.txt");
InputStream out=null;
public void go()
{
m.setSize(800,666);
m.setLayout(new FlowLayout());
b1.addActionListener(this);
m.add(b1);
m.add(l);
m.setVisible(true);
} public void actionPerformed(ActionEvent e)
{
try
{
out=new FileInputStream(f);
}
catch(FileNotFoundException fe)
{
fe.printStackTrace();
}
byte b[]=new byte[1024];
int i=0;
try
{
i=out.read(b);
}
catch(IOException ee)
{
ee.printStackTrace();
}
try
{
out.close();
}
catch(IOException ee1)
{
ee1.printStackTrace();
}
l.setText(new String(b,0,i));
l.setSize(300,30);
}
public static void main(String []args)
{
Test t=new Test();
t.go();
} }