package three.day.frame;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class NotepadFrameDemo01 extends JFrame
{
public static void main(String[] args)
{
new NotepadFrm().setVisible(true);
}
public NotepadFrameDemo01()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("我的记事本");
//设置窗口大小
setSize(new Dimension(400,500));
//设置窗口初始显示位置
setLocation(new Point(450,100));
//添加一个文本域
final TextArea ta = new TextArea();
ta.setText("this is just a test");
add(ta);
final Clipboard cb = this.getToolkit().getSystemClipboard();
//添加菜单功能
JMenuBar JMenubar = new JMenuBar();
JMenu mn1 = new JMenu("文件");
JMenu mn2 = new JMenu("编辑");
JMenu mn3 = new JMenu("格式");
JMenuItem mi1 = new JMenuItem("新建");
mi1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
ta.setText("");
}
});
JMenuItem mi2 = new JMenuItem("打开");
mi2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
FileDialog fd = new FileDialog((Frame) getParent(), "打开文件",FileDialog.LOAD);
fd.setVisible(true);
//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)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
JMenuItem mi3 = new JMenuItem("保存");
mi3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
FileDialog fd = new FileDialog((Frame) getParent(), "打开文件",FileDialog.SAVE);
fd.show();
String strPath = fd.getDirectory();
String strName02 = fd.getFile();
System.out.println(strName02);
if(strName02!=null)
{
try{
File fl = new File(strPath,strName02);
FileWriter file_writer = new FileWriter(fl);
BufferedWriter out = new BufferedWriter(file_writer);
out.write(ta.getText(),0,(ta.getText()).length());
out.close();
file_writer.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
});
JMenuItem mi4 = new JMenuItem("退出");
mi4.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
JMenuItem mi5 = new JMenuItem("复制");
mi5.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String tempStr = ta.getSelectedText();
StringSelection ss = new StringSelection(tempStr);
cb.setContents(ss, null);
}
});
JMenuItem mi6 = new JMenuItem("剪切");
mi6.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String tempStr = ta.getSelectedText();
StringSelection ss = new StringSelection(tempStr);
int sStart = ta.getSelectionStart();
int sEnd = ta.getSelectionEnd();
ta.replaceText("", sStart, sEnd);
}
});
JMenuItem mi7 = new JMenuItem("黏贴");
mi7.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Transferable tf = cb.getContents(this);
DataFlavor df = DataFlavor.stringFlavor;
if(tf.isDataFlavorSupported(df))
try{ String str;
str=(String)tf.getTransferData(df);
ta.append(str);
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
});
JMenuItem mi8 = new JMenuItem("字体");
mi8.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
final Dialog dg = new Dialog((Frame) getParent(), "字体设置");
dg.setSize(new Dimension(300,400));
dg.setLocation(new Point(600,200));
dg.setBackground(new Color(204,232,207));
Panel pl = new Panel();
Button bt1 = new Button("确定");
Button bt2 = new Button("取消");
bt2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dg.dispose();
}
});
final Label lb = new Label("");
lb.setSize(new Dimension(300,100));
pl.add(bt1);
bt1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
lb.setFont(new Font("宋体",Font.ITALIC|Font.BOLD,15));
lb.setText("恭喜,设置成功");
}
});
pl.add(bt2);
pl.add(lb);
dg.add(pl);
dg.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//System.exit(0);
dg.dispose();
}
});
dg.show();
}
});
mn1.add(mi1);
mn1.add(mi2);
mn1.add(mi3);
mn1.add(mi4);
mn2.add(mi5);
mn2.add(mi6);
mn2.add(mi7);
mn3.add(mi8);
JMenubar.add(mn1);
JMenubar.add(mn2);
JMenubar.add(mn3);
setJMenuBar(JMenubar);
//添加关闭窗口的时间监听器
//方法1如下(另外实现class WinLis 这个类)
//this.addWindowListener(new WinLis());
//方法二如下:
addWindowListener(new WindowAdapter()//WindowAdapter()主要用来监听一个窗口的事件
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示窗口
setVisible(true);
}
}