返回信息流为什么我写得这个程序打开的窗口不能关闭唉,郁闷好久了,不知道事件监听这个东西要怎么弄。。。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main extends WindowAdapter implements ActionListener
{
/**
* @param args the command line arguments
*/
Frame f1,f2;
Button b1;
TextArea ta1;
Panel p1;
TextField tf1;
FileDialog fd;
public static void main(String[] args) {
(new Main()).display();
}
public void display()
{
f1=new Frame("OpenFile");
f1.setSize(200,150);
f1.setLocation(400,140);
b1=new Button("Open");
f1.add(b1,BorderLayout.CENTER);
b1.addActionListener(this);
f1.addWindowListener(this);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
File file1=null;
if(e.getSource()==b1)
{
fd=new FileDialog(f2,"Open",FileDialog.LOAD);
fd.setVisible(true);
if((fd.getDirectory()!=null)&&(fd.getFile()!=null))
{
f2=new Frame("FileRouteAndContent");
f2.setSize(680,400);
f2.setLocation(600,140);
f2.addWindowListener(this);
tf1=new TextField();
tf1.setEnabled(false);
tf1.setFont(new Font("Dialog",0,20));
f2.add(tf1,"North");
ta1=new TextArea();
ta1.setFont(new Font("Dialog",0,20));
ta1.setEnabled(false);
f2.add(ta1);
p1=new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
f2.add(p1,"South");
f2.setVisible(true);
tf1.setText(fd.getDirectory()+fd.getFile());
try
{
file1=new File(fd.getDirectory(),fd.getFile());
FileReader fr=new FileReader(file1);
BufferedReader br=new BufferedReader(fr);
String aline;
while((aline=br.readLine())!=null)
ta1.append((aline+"\r\n"));
fr.close();
br.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}
}
public void WindowClosing(WindowEvent e)
{
if(e.getSource()==f1)
System.exit(0);
if(e.getSource()==f2)
System.exit(0);
}
}
这是一条镜像帖。来源:北邮人论坛 / java / #14816同步于 2010/6/11
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
我刚学,问个简单的问题,别笑话我啊
submarry
2010/6/11镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
public void WindowClosing(WindowEvent e)
{
if(e.getSource()==f1)
System.exit(0);
if(e.getSource()==f2)
System.exit(0);
}
}
没看懂这个,只要是frame1或者frame2上发生了事件,你就要关闭程序?
【 在 ericyosho 的大作中提到: 】
: public void WindowClosing(WindowEvent e)
: {
: if(e.getSource()==f1)
: ...................
就是这里不会。。。不会写关闭窗口的事件监听
把frame setVisable(false) 就行
【 在 submarry (submarry) 的大作中提到: 】
: 就是这里不会。。。不会写关闭窗口的事件监听
【 在 ox 的大作中提到: 】
: 把frame setVisable(false) 就行
: 【 在 submarry (submarry) 的大作中提到: 】
: : 就是这里不会。。。不会写关闭窗口的事件监听
: ...................
姐姐,我设了两个frame,一个f1一个f2,要该哪个啊?不过设成false会不会看不见了啊?
【 在 ox 的大作中提到: 】
: 把frame setVisable(false) 就行
: 【 在 submarry (submarry) 的大作中提到: 】
: : 就是这里不会。。。不会写关闭窗口的事件监听
: ...................
明白了,已经弄好了,3Q
jiejie...
【 在 submarry 的大作中提到: 】
: 姐姐,我设了两个frame,一个f1一个f2,要该哪个啊?不过设成false会不会看不见了啊?