package three.day.frame;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileDialogDemo01
{
public static void main(String[] args)
{
final Frame mf = new Frame("JAVA");
//设置窗口大小
mf.setSize(new Dimension(400,500));
//设置窗口初始显示位置
mf.setLocation(new Point(450,100));
//添加一个文本域
final TextArea ta = new TextArea();
ta.setText("this is just a test");
mf.add(ta);
//添加菜单功能
MenuBar mb = new MenuBar();
Menu mn1 = new Menu("文件");
Menu mn2 = new Menu("编辑");
MenuItem mi1 = new MenuItem("新建");
MenuItem mi2 = new MenuItem("打开");
mi2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
FileDialog fd = new FileDialog(mf, "打开文件",FileDialog.LOAD);
fd.show();
//fd.setBounds(new Rectangle(new Point(400,400)));
String strName = fd.getDirectory()+fd.getFile();
System.out.println(strName);
if(strName!=null)
{
try{
File fl = new File(strName);
int fsize = (int) fl.length();
FileReader fr = new FileReader(fl);
char[] fileStr = new char[fsize];
int charsRead = 0;
while(fr.ready())
{
charsRead += fr.read(fileStr, charsRead, fsize-charsRead);
}
fr.close();
ta.setText(new String(fileStr,0,charsRead));
} catch (IOException e1)
{
e1.printStackTrace();
}
}
}
});
MenuItem mi3 = new MenuItem("保存");
MenuItem mi4 = new MenuItem("退出");
mi4.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
MenuItem mi5 = new MenuItem("复制");
MenuItem mi6 = new MenuItem("剪切");
MenuItem mi7 = new MenuItem("黏贴");
mn1.add(mi1);
mn1.add(mi2);
mn1.add(mi3);
mn1.add(mi4);
mn2.add(mi5);
mn2.add(mi6);
mn2.add(mi7);
mb.add(mn1);
mb.add(mn2);
mf.setMenuBar(mb);
PopupMenu ppm = new PopupMenu();
ppm.add(mi5);
ppm.add(mi6);
ppm.add(mi7);
mf.addWindowListener(new WindowAdapter()//WindowAdapter()主要用来监听一个窗口的事件
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示窗口
System.out.println(mf.isDisplayable());
//mf.setMaximizedBounds(new Rectangle(100,200,400,800));
mf.setMaximizedBounds(new Rectangle(100,100,200,200));
System.out.println(mf.getGraphicsConfiguration());
System.out.println(mf.getGraphics());
mf.setCursor(10);
mf.setVisible(true);
System.out.println(mf.getGraphicsConfiguration());
System.out.println(mf.getGraphics());
System.out.println(mf.isDisplayable());
}
}