e10_2.java

来自「java 初学者学习实例」· Java 代码 · 共 58 行

JAVA
58
字号
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class E10_2
{
  public static void main(String args[])
  {
    FileFrame ss = new FileFrame( );
  }
}
class FileFrame extends Frame implements ActionListener
{
  TextArea ta;
  Button open,quit;
  FileDialog fd;
  FileFrame( )
  {
     super("获取并显示文本文件");
     ta = new TextArea(10,45);
     open = new Button("打开");
     quit = new Button("关闭");
     open.addActionListener(this);
     quit.addActionListener(this);
     setLayout(new FlowLayout( ));
     add(ta);
     add(open);
     add(quit);
     setSize(350,280);
     show( );
  }
 public void actionPerformed(ActionEvent e)
 {
    if(e.getActionCommand( )=="打开")
     {
       fd = new FileDialog(this,"打开文件",FileDialog.LOAD);
	   fd.setDirectory("c:\temp");//设置文件						对话框的基础目录
	   fd.show( );//弹出并显示文件对话框,程序				    暂停直至用户选定一文件
	   try{
	   File myfile = new File(fd.getDirectory( ),fd.getFile( ));
	   RandomAccessFile raf = new RandomAccessFile(myfile,"r");
	   while(raf.getFilePointer( )<raf.length( ))
       {
	      ta.append(raf.readLine( )+"\n");
	   }
     }
	catch(IOException ioe)
	{
	   System.err.println(ioe.toString( ));
	}
	}
	if(e.getActionCommand( )=="关闭")
    {
	   dispose( );
	   System.exit(0);
	}
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?