编写一个程序,生成一个窗口。标题为记事本,有一个文件菜单,正中显示一个TextArea,默认值为“我是TextArea!”。
窗口程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class WindowJiShiBen extends Frame
implements ActionListener
{
MenuBar menubar;
Menu menu;
MenuItem itemExit;
MenuItem itemSave;
TextArea text1;
FileWriter tofile;
BufferedWriter out;
FileDialog filedialog_save;
WindowJiShiBen(String s)
{
super(s);
text1 =new TextArea("我是TextArea");
menubar =new MenuBar();
menu =new Menu("文件");
itemExit =new MenuItem("退出");
itemExit.setShortcut(new MenuShortcut(KeyEvent.VK_E));
itemSave=new MenuItem("保存");
itemSave.setShortcut(new MenuShortcut(KeyEvent.VK_S));
filedialog_save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);
menu.add(itemExit);
menubar.add(menu);
menu.add(itemSave);
setMenuBar(menubar);
add(text1);
itemExit.addActionListener(this);
itemSave.addActionListener(this);
setBounds(100,100,150,150);
setVisible(true);
validate();
filedialog_save.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ filedialog_save.setVisible(false);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==itemExit)
{System.exit(0);}
else if(e.getSource()==itemSave)
{filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{ try { File file=new
File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(text1.getText(),0,(text1.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
}
}
public class jishiben
{
public static void main(String args[])
{
WindowJiShiBen win=new WindowJiShiBen("记事本");
}
}
Your support will encourage me to continue to create!