Java_awt_图形用户界面GUI简单应用

来源:互联网 发布:铃声放大软件 编辑:程序博客网 时间:2024/06/04 00:46

 

以下代码实现的功能
   1.写入路径,点击转到,列出该路径下的目录及文件;
   2.在路径文本框中敲入回车符,完成1功能;
   3.新建菜单
   4.点击菜单下的打开,可以读入该文件的文本信息;
   5.点击菜单下的保存,可以保存大文本框中的文本信息;
   6.点击菜单下的退出,可以结束程序!

关于swing应用实例>>

/*基本文件的读取与保存Strawberry2013-5-6*/import java.awt.*;import java.awt.event.*;import java.io.*;class AwtDemo{public static void main(String[] args){new Awt();}}class Awt{private Frame f;private TextField txtf;private TextArea txta;private Label labAddr;private Button bt;private MenuBar mbar;private Menu caidan;private MenuItem openMenu;private MenuItem saveMenu;private MenuItem exitMenu;Awt(){f = new Frame("my");f.setBounds(200,200,500,450);//设置出现的位置,及大小f.setLayout(new FlowLayout());labAddr = new Label("目录:");f.add(labAddr);txtf = new TextField(30);f.add(txtf);bt = new Button("转到");f.add(bt);txta = new TextArea(20, 60);f.add(txta);mbar = new MenuBar();//mbar(MenuBar)caidan = new Menu("菜单");//|--caidan(Menu)openMenu = new MenuItem("打开");//|--openMenu(MenuItem)saveMenu = new MenuItem("保存");//|--saveMenu(MenuItem)exitMenu = new MenuItem("退出");//|--exitMenu(MenuItem)mbar.add(caidan);caidan.add(openMenu);caidan.add(saveMenu);caidan.addSeparator();//---------------分割线caidan.add(exitMenu);f.setMenuBar(mbar);myEvent();f.setVisible(true);}public void myEvent()//监听方法集{f.addWindowListener(new WindowAdapter()//窗体监听机制,由于有多个故有适配器{public void windowClosing(WindowEvent e)//当监听器发现窗体关闭,就会发生windwClosing动作,{//将事件打包,存于WindowEvent的e中,可以查看事件的相信信息!System.exit(0);//程序退出}});bt.addActionListener(new ActionListener()//button按钮的活动监听{public void actionPerformed(ActionEvent e)//只有这一个方法{showDir(txtf.getText());}});/* button的双击监听bt.addMouseListener(new MouseAdapter(){public void mouseClicked(ActionEvent e){if(e.getClickCount() == 2)//双击判断{//方法区}}});*/txtf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){if(e.getKeyCode() == KeyEvent.VK_ENTER)//监听是否为输入了Enter{e.consume();//********此处注意:::由于不接收enter符,会有提示音!当不希望某一字符输入时,可以使用该函数撤销输入的字符!showDir(txtf.getText());}}});openMenu.addActionListener(new ActionListener()//菜单的打开{public void actionPerformed(ActionEvent e){FileDialog fd = new FileDialog(f, "打开文件");fd.setVisible(true);String dir = fd.getDirectory();//获取路径String name = fd.getFile();if((dir==null) || (name==null))return;elseshowText(dir+name);//操作return;}});saveMenu.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){FileDialog fd = new FileDialog(f, "打开文件", FileDialog.SAVE);fd.setVisible(true);String dir = fd.getDirectory();String name = fd.getFile();if((dir==null) || (name==null))return;elsesaveText(dir+name);return;}});exitMenu.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.exit(0);}});}public void showDir(String path)//列出目录及文件的文本信息{if(path.equals("")){txta.setText("目录不能为空!");return;}File dir = new File(path);if(!dir.exists())txta.setText("文件目录"+txtf.getText()+"不存在!");else if(!dir.isDirectory())txta.setText("只存在文件,不存在目录!");else{txta.setText("");String[] str = dir.list();for(String s: str)txta.append(s+"\r\n");}return;}public void showText(String path)//读取文件的文本信息{try{BufferedReader bufr = new BufferedReader(new FileReader(path));txta.setText("");String str = null;while((str=bufr.readLine()) != null)txta.append(str+"\r\n");bufr.close();}catch (IOException e){}return;}public void saveText(String path)//保存大文本框中的文本信息{try{BufferedWriter bufw = new BufferedWriter(new FileWriter(path));bufw.write(txta.getText());bufw.close();}catch (IOException e){}return;}}


原创粉丝点击