📄 example8_5.java
字号:
/* 英汉小词典 */
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class InputArea extends Panel implements ActionListener
{
File f=null;
RandomAccessFile out;
TextField word,note;
Button button;
InputArea(File f)
{ setBackground(Color.cyan);
this.f=f;
word=new TextField(8);
note=new TextField(8);
button=new Button("录入");
button.addActionListener(this);
add(new Label("输入单词"));add(word);
add(new Label("输入解释"));add(note);
add(button);
}
public void actionPerformed(ActionEvent e)
{
try{
RandomAccessFile out=new RandomAccessFile(f,"rw");
if(f.exists())
{ long length=f.length();
out.seek(length);
}
out.writeUTF("单词:"+word.getText());
out.writeUTF("解释:"+note.getText());
out.close();
}
catch(IOException ee){}
}
}
class RandWin extends Frame implements ActionListener
{
File file=null;
MenuBar bar;
Menu fileMenu;
MenuItem 录入,显示;
TextArea txt;
InputArea inputMessage;
CardLayout card=null; //卡片式布局.
Panel pCenter;
Button btn=new Button("显示");
RandWin()
{
super("英汉小词典");
file=new File("英汉小词典.txt");
inputMessage=new InputArea(file);
txt=new TextArea(5,50);
card=new CardLayout();
pCenter=new Panel();
setLayout(new FlowLayout());
add(inputMessage);
add(btn);
add(txt);
btn.addActionListener(this);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,380,200);
validate();
}
public void actionPerformed(ActionEvent e)
{
int number=1;
try{
RandomAccessFile in=new RandomAccessFile(file,"r");
String 单词=null;
while((单词=in.readUTF())!=null)
{ txt.append("\n"+number+" "+单词);
txt.append(" "+in.readUTF()); //读取汉语解释.
txt.append("\n------------------------- ");
number++;
}
in.close();
}
catch(Exception ee){}
}
}
public class Example8_5
{
public static void main(String args[])
{ new RandWin(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -