導入Java . awt . card layout;
導入Java . awt . color;
導入Java . awt . flow layout;
完整的過程如下:
導入Java . io . *;
導入javax . swing . *;
導入Java . awt . card layout;
導入Java . awt . color;
導入Java . awt . flow layout;
導入Java . awt . event . *;
類InputArea擴展JPanel實現ActionListener
{
文件f = null
RandomAccessFile out
JTextField字,註意;
JButton按鈕;
輸入區(文件f)
{
set background(color . red);
this.f = f
word = new JTextField(8);
note = new JTextField(8);
button = new JButton(" entry ");
button . addactionlistener(this);
Add(new JLabel("輸入詞"));加(字);
Add(new JLabel("輸入解釋"));補充(註);
添加(按鈕);
}
公共無效操作已執行(操作事件e)
{
嘗試{
RandomAccessFile out = new RandomAccessFile(f,“rw”);
if(f.exists())
{
長長度= f . length();
out.seek(長度);
}
out . write utf(" word:"+word . gettext());
Out.writeUTF("解釋:"+note . gettext());
out . close();
}
catch(io exception ee){ system . out . println(ee);}
}
}
類RandWin擴展JFrame實現ActionListener
{
File file = null
JMenuBar欄;
JMenu fileMenu
JMenuItem輸入、顯示;
JTextArea txt
InputArea輸入消息;
CardLayout card = null
JPanel pCenter
JButton BTN = new JButton(" display ");
蘭德溫()
{
超級(《英漢大詞典》);
File=new File("英漢詞典。txt ");
input message = new input area(file);
txt=new JTextArea(5,50);
card = new card layout();
pCenter = new JPanel();
set layout(new flow layout());
add(input message);
添加(BTN);
添加(txt);
BTN . addactionlistener(this);
addWindowListener(新WindowAdapter()
{公共void窗口關閉(WindowEvent e)
{ system . exit(0);
}
});
set visible(true);
setBounds(100,50,380,200);
validate();
}
公共無效操作已執行(操作事件e)
{
int number = 1;
嘗試{
RandomAccessFile in = new RandomAccessFile(file," r ");
字符串word = null
While ((word =in.readUTF())!=空)
{
txt . append(" \ n "+number+" "+word);
txt . append("+in . read utf());
txt . append(" \ n-");
數字++;
}
in . close();
}
catch(Exception ee){ system . out . println(ee);}
}
}
公共類示例8_5
{
公共靜態void main(String args[])
{ new rand win();}
}