黑马程序员 GUI

本文详细解析了GUI的基本概念,包括布局管理器(FlowLayout、BorderLayout、GridLayout、CardLayout、GridBagLayout)、组件(按钮、文本框、文本域、文本标签)以及创建图形化界面的基本步骤。此外,还演示了如何通过代码实现窗口的基本设置和添加事件监听器。
摘要由CSDN通过智能技术生成


-                                                                 ---------- android培训java培训、java学习型技术博客、期待与您交流! ------------

GUI详解

GUI基本概念

GUI即Graphical Users Interger图形用户接口。

 

布局管理器

FlowLayout

从左到右的顺序排列

BorderLayout(边界布局管理器)

东南西北中

Gridlayout(网格布局管理器)

规则的矩阵

CardLayout(卡片布局管理器)

选项卡

GridBagLayout(网格包布局管理器)

 非规则的矩阵

 

组件:

按钮组件:(Button): 此类创建一个标签按钮。当按下该按钮时,应用程序能执行某项动作

常用的方法:

a)  void actionListener(ActionListener); 添加指定的动作侦听器,以接收发自此按钮的动作事件

b)  构造方法:Button(String lable):构造一个带指定标签的按钮。

 

文本框组件:(TextField):对象是允许编辑单行文本的文本组件。

常用的方法:

a)  构造方法:TextField(String columns):构造具有指定列数的新空文本字段。

b)  void actionListener(ActionListenerl):添加指定的操作侦听器,以从此文本字段接收操作事件

c)  void setText(String T): 将此文本组件显示的文本设置为指定文本。

d)  String getText():返回此文本组件所表示文本的选定文本。继承自父类TextComponent

 

文本域组件:(TextArea):对象是显示文本的多行区域。可以将它设置为允许编辑或只读。

常用的方法:

a)  构造函数:TextArea(int rows,int columns):构造一个新文本区,该文本区具有指定的行数和列数,并将空字符串作为文本

b)  void append(String str):将给定文本追加到文本区的当前文本。

c)  void setText(String T): 将此文本组件显示的文本设置为指定文本。

d)  String getText():返回此文本组件所表示文本的选定文本。继承自父类TextComponent

 

 

文本标签:(Label):Label 对象是一个可在容器中放置文本的组件。一个标签只显示一行只读文本

常用方法:

a)  构造方法:Lable():构造一个空标签。

b)  void setText():将此标签的文本设置为指定的文本。

c)  String getText():获取此标签的文本

 

窗口

窗口即Frame,是带有标题和边框的顶层窗口。

它的常用方法:

a)  构造方法:Frame(String title):构造一个新的、最初不可见的、具有指定标题的 Frame 对象。

b)  void setMenuBar(MenuBar mb);添加菜单栏

c)  Component add(Component comp) :将指定组件追加到此容器的尾部。

d)  setLayout(LayoutManager mgr):设置此容器的布局管理器。继承自父类Container方法

e)  void setFont(Font f) :设置此容器的字体。继承自父类Container方法

f)  void setBounds(int x, int y,int width, int height):移动组件并调整其大小,继承自父类Component方法

g)  void setVisible(boolean b): 根据参数 b 的值显示或隐藏此组件。继承自父类Component方法

 

创建图形化界面基本过程

1.      创建Frame窗体

Frame f=new Frame(“窗口”);

2.      对窗体进行基本设置

a)        大小,void setSize(int len,int wid);

b)        位置,即显示在系统本地的位置,void setLoction(int x,int y);

c)        大小和位置设置voidsetBounds(int x,int y,int len,int wid);

d)        布局,void setLayout(LayoutManagermgr)

3.      定义组件

例如:Button btn=new Button(“我是按钮”);

4.      将组件通过窗体的add()方法添加到窗体中

Component add(Component comp)

5.      让窗体显示,通过voidsetVisible(true);

 

我们通过代码,来演示下:

importjava.awt.*;

class  AwtDemo

{

public static void main(String[] args)

{

           //创建一个fram窗口

           Frame f=new Frame("窗口");

           //设置窗口的大小

           f.setSize(500,400);

           //设置窗口在系统中显示的位置

           f.setLocation(300,200);

           //设置窗口需要什么布局

           f.setLayout(new FlowLayout());

           //新建一个组件为button、

           Button b=new Button("我是一个按钮");

           //将组件放入到窗口中

           f.add(b);

           //让窗口显示出来,默认是不显示的

           f.setVisible(true);

           System.out.println("helloworld");

}

}

 

事件监听机制

1.       事件源

就是awt包或者swing包中的那些图形界面组件

2.       事件

每一个事件源都有自己的特有的对应事件和共性事件

3.       监听器

将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中

4.       事件处理

对产生的动作进行处理。

 

事件源,时间,事件处理在java中都已经定义好了,直接获取对象来用就可以了。我们需要做的就是事件处理

窗体监视器

我们设置窗体监听器处理总共有三种方式:

1.       创建一个类,让其类实现WindowListener接口,但缺点是要覆盖七个方法。我们只需要要一个方法,但其他方法都必须要复写,麻烦。形式如下:

 public MyWin implements WindowListener

{

   //复写七个方法,麻烦。

}

2.       创建一个类,让它去继承WindowListener接口的一个子类WindowAdapter,这个类覆盖了WindowListener接口的所有方法,我们继承后复写我们需要的方法.不建议使用这种方法,形式如下:

class MyWin extends WindowAdapter

{

   public void windowClosing(WindowEvent e){};

}

使用方法:在main方法中使用new.Fram().addWindowListener(newMyWin())

3.       使用匿名内部类的方式

即我们在addWindowListener()方法中创建匿名内部类,匿名内部类中也可以选择我们需要的覆盖的方法。形式如下:

   new Fram().addWindowListener(newWindowAdapter()

{

                     public voidwindowClosing(WindowEvent e){};

})

 

WindowAdapter常用的几个方法:

a)    最常用的,关闭窗口事件:void windowClosing(WindowEvent e);

b)    激活窗口事件:void windowActivated(WindowEvent e)

c)    窗口打开事件:void windowOpened(WindowEvent e);

 

      我们通过代码演示下这几个方法:

                   import java.awt.*;

importjava.awt.event.*;

class  AwtDemo

{

         public static void main(String[] args)

         {

                   //创建一个fram窗口

                   Frame f=new Frame("窗口");

                   //设置窗口的大小

                   f.setSize(500,400);

                   //设置窗口在系统中显示的位置

                   f.setLocation(300,200);

                   //设置窗口需要什么布局

                   f.setLayout(newFlowLayout());

                   //新建一个组件为button、

                   Button b=new Button("我是一个按钮");

                   //将组件放入到窗口中

                   f.add(b);

 

 

                   //添加监听器,使用匿名内部类

                   f.addWindowListener(newWindowAdapter(){

                            //窗口关闭

                            public voidwindowClosing(WindowEvent e)

                            {

                                     System.out.println("窗口关闭");

                                     System.exit(0);

                            }

                            //窗口激活,即成为前台时激活

                            public voidwindowActivated(WindowEvent e)

                            {

                                     System.out.println("窗口被激活了");

                            }

                            //窗口第一次打开

                            public voidwindowOpened(WindowEvent e)

                            {

                                     System.out.println("窗口被打开了");

                            }

                  

                   });

                   //让窗口显示出来,默认是不显示的

                   f.setVisible(true);

         }

}

 

活动监听器

活动监听器,,形式为

addActionListenernew ActionListener(

public void actionPerformed(ActionEvent e){};

);

方法:void actionPerformed(ActionEvent e);

 

我们用代码演示下,并把前面的代码进行了优化:

import java.awt.*;

import java.awt.event.*;

class FrameDemo

{

privateFrame f;

privateButton btn;

FrameDemo()

{

           //一初始化对象就调用init()方法;

           init();

}

publicvoid init()

{

           f=newFrame("my frame");

           //设置大小和位置的方法

           f.setBounds(400,300,400,500);

           f.setLayout(newFlowLayout());

           btn=newButton("打开记事本");

           f.add(btn);

           //加载事件方法

           event();

           f.setVisible(true);

}

publicvoid event()

{

           /*

           窗口监听器

           */

           f.addWindowListener(newWindowAdapter(){

                    publicvoid windowClosing(WindowEvent e)

                    {

                             System.out.println("窗口关闭监听器");

                             System.exit(0);

                    }

           });

           /*

                    按钮监听器

                    按钮是事件源,我们让按钮具备打开windows系统的记事本功能

           */

           btn.addActionListener(newActionListener(){

                    publicvoid actionPerformed(ActionEvent e)

                    {

                             //创建一个Runtime对象

                             Runtimer=Runtime.getRuntime();

                             System.out.println("按钮监听器");

                             try

                             {

                                       //让其打开一个记事本

                                       r.exec("notepad");

                             }

                             catch(Exception e1)

                             {

                                       thrownew RuntimeException("没有响应");

                             }

                    }

           });

}

publicstatic void main(String[] args)

{

           newFrameDemo();

}

}

 

鼠标监听器

鼠标监听器的形式为:

addMouseListenernew MouseAdapter(){

        

})

鼠标监听器常用的方法是:

a)       void mouseClicked(MouseEvent e):单击触发事件

双击或多击触发事件:使用MouseEvent类里的一个方法:

void getMouseCount():此事件关联的鼠标单击次数

b)       void mouseEntered(MouseEvent e): 鼠标进入到组件上时调用。\

c)       void mouseExited(MouseEvent e); 鼠标离开组件时调用。

 

我们基本演示下鼠标事件的监听器:

import java.awt.*;

import java.awt.event.*;

class MouseDemo

{

privateFrame f;

privateButton btn;

MouseDemo()

{

           init();

}

publicvoid init()

{

           f=newFrame("鼠标事件");

           btn=newButton("鼠标事件");

           f.setBounds(300,400,400,500);

           f.setLayout(newFlowLayout());

           f.add(btn);

           myEvent();

           f.setVisible(true);

}

publicvoid myEvent()

{

           /*

           窗口监听器

           */

           f.addWindowListener(newWindowAdapter(){

                    publicvoid windowClosing(WindowEvent e)

                    {

                             System.exit(0);

                    }

           });

           /*

         鼠标监听器

           */

           btn.addMouseListener(newMouseAdapter(){

                    intcount=0;

                    intclickCount=0;

                             publicvoid mouseEntered(MouseEvent e)

           {

          

                    System.out.println("鼠标在组件上"+(count++));

           }

           publicvoid mouseClicked(MouseEvent e)

           {

                    if(e.getClickCount()==2)//双击

                    {

                             System.out.println("双击触发"+clickCount++);

                    }

                    //System.out.println("单击触发"+(clickCount++));

           }

           });

}

publicstatic void main(String[] args)

{

           newMouseDemo();

}

}

 

键盘监听器

键盘监听器的形式为:

addKeyListener(new KeyAdapter(){

 

});

键盘监视器的常用方法:

a)       void keyPressed(KeyEvent e): 按下某个键时调用此方法

b)       void keyReleased(KeyEvent e): 释放某个键时调用此方法。

c)       void keyTyped(KeyEvent e): 键入某个键时调用此方法

KeyEvent类的常用方法:

a)  char getkeyChar():返回与此事件中的键盘关联的字符

b)  int getKeyCode():返回与此事件中的键关联的整数 keyCode

c)  static String getKeyText():返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。

d)  boolean isControlDown():按住Ctrl键,此方法继承自InputEvent

e)  void consume():使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。此方法继承自InputEvent

 

我们通过代码来看看这些方法的用法:

 

为按钮添加键盘监视器用法

importjava.awt.*;

importjava.awt.event.*;

classKeyDemo

{

         private Frame f;

         private Button btn;

         KeyDemo()

         {

                   init();

         }

         public void init()

         {

                   f=newFrame("keyEvent");

                   btn=new Button("键盘事件");

                   f.setBounds(400,400,500,400);

                  f.setLayout(newFlowLayout());

                   f.add(btn);

                   myEvent();

                   f.setVisible(true);

         }

         public void myEvent()

         {

                   f.addWindowListener(newWindowAdapter(){

                            public voidwindowClosing(WindowEvent e)

                            {

                                     System.exit(0);

                            }

                   });

         //为按钮添加监视器

                   btn.addKeyListener(newKeyAdapter(){

                            public voidkeyPressed(KeyEvent e)

                            {

                                     /*

                                     组合键

                                     */

                                     if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)

                                     {

                                               System.out.println("Contrl+Entet组合键");

                                     }

 

                                     /*

                                     敲入Enter退出

                                     if(e.getKeyCode()==KeyEvent.VK_ENTER)

                                     {

                                               System.exit(0);

                                     }*/

 

                                                                                             //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());

                            }

                   });

         }                

         public static void main(String[] args)

         {

                   new KeyDemo();

         }

}

 

为文本添加键盘监视器

importjava.awt.*;

importjava.awt.event.*;

classKeyDemo

{

         private Frame f;

         private Button btn;

         private TextField tf;//文本组件

         KeyDemo()

         {

                   init();

         }

         public void init()

         {

                   f=newFrame("keyEvent");

                   btn=new Button("键盘事件");

                   //创建一个文本对象,并传入是20列数

                   tf=new TextField(20);

                   f.setBounds(400,400,500,400);

                   f.setLayout(newFlowLayout());

                   //将文本组件添加进去

                   f.add(tf);

                   f.add(btn);

                   myEvent();

                   f.setVisible(true);

         }

         public void myEvent()

         {

                   f.addWindowListener(newWindowAdapter(){

                            public voidwindowClosing(WindowEvent e)

                            {

                                     System.exit(0);

                            }

                   });

                   //为文本框添加监视器

                   tf.addKeyListener(newKeyAdapter(){

                            public voidkeyPressed(KeyEvent e)

                            {

                                     intkey=e.getKeyCode();

                                     //只能输入键盘上0-9的数字

                                     if(!(key>=KeyEvent.VK_0&&key<=KeyEvent.VK_9)){

                                               System.out.println(e.getKeyText(key)+"是非法字符");

                                               //非法字符无法显示在文本框内

                                               e.consume();

                                     }

                            }

                   });

         }

         public static void main(String[] args)

         {

                   new KeyDemo();

         }

}

 

对话框(Dialog)

Dialog是一个提示用户信息的一个对话框。它和Frame一样是个窗体

Dialog d=new Dialog();

 

对话框的创建跟Frame的过程几乎是一样的,都要设置大小,位置和添加组件

 

它的常用方法为:

a)  构造方法:Dialog(Frame f,String title,boolean modal):构造一个最初不可见的 Dialog,它带有指定的所有者 Frame、标题和模式

b)  void setVisible(boolean b): 根据参数 b 的值显示或隐藏此 Dialog

c)  void setBounds(int x,int y,intlen,int wid);:设置大小和位置

d)  Component add(Component comp):添加一个用户组件

 

还有常常和文件关联的一种FileDialog,它的常用方法:

a)  FileDialog(Dialog parent, String title, int mode):创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。

b)  String getDirectory()  :获取此文件对话框的目录

c)  String getFile():获取此文件对话框的选定文件。

 

我们基本演示下对话框的使用:

importjava.awt.*;

importjava.awt.event.*;

class DialogDemo

{

         private Frame f;

         private Dialog d;//对话框

         private Button btn;

         private Button okbtn;//对话框上的确定按钮

         private Label l;//标签文本

        

         DialogDemo()

         {

                   init();

         }

         public void init()

         {

                   f=newFrame("dialog");

                   f.setBounds(300,100,600,500);

                   f.setLayout(newFlowLayout());

                   btn=new Button("点击弹出对话框");

                   f.add(btn);

                  

                   //创建一个对话框

                    d=new Dialog(f,"d对话框提示信息-self",true);

                   //创建对话框上的文本标签文本

                    l=new Label();

                    //创建对话框的确定按钮组件

                    okbtn=new Button("确定");

                    //设置对话框的大小

                    d.setBounds(300,100,300,200);

                    //设置对话框的布局

                    d.setLayout(new FlowLayout());

                    //为对话框添加组件

                    d.add(l);

                    d.add(okbtn);

                  

                    myEvent();

                    f.setVisible(true);

                  

         }

         public void myEvent()

         {

                   //创建对话框上确定的监听器

                   okbtn.addActionListener(newActionListener(){

                            public voidactionPerformed(ActionEvent e)

                            {

                                     //设置对话框为不可见

                                     d.setVisible(false);

                            }

                   });

                   f.addWindowListener(newWindowAdapter(){

                            public voidwindowClosing(WindowEvent e)

                            {

                                     System.exit(0);

                            }

                   });

                  

                   //创建按钮弹出对话框的按钮监视器

                   btn.addActionListener(newActionListener(){

                            public voidactionPerformed(ActionEvent e)

                            {

                                     //在对话框上显示的文字

                                     l.setText("提示用户信息");

                                     //点击会弹出对话框

                                     d.setVisible(true);

                            }

                   });

        

         }

         public static void main(String[] args)

         {

                   new DialogDemo();

         }

}

 

应用:我们输入一个目录,点击确定输出目录下所有文件和文件夹,输入错误对话框提示错误信息。

importjava.awt.*;

importjava.awt.event.*;

importjava.io.*;

classTest

{

         private Frame f;

         private Button btn;

         private TextField tf;

         private TextArea ta;

 

         private Dialog d;

         private Label l;

         private Button okbtn;

         Test()

         {

                   init();

         }

         public void init()

         {

                   //基本窗体

                   f=newFrame("myWindows");

                   f.setBounds(300,100,600,500);

                   f.setLayout(newFlowLayout());

                   tf=new TextField(60);

                   f.add(tf);

 

                   btn=new Button("转到");

                   f.add(btn);

 

                   ta=new TextArea(25,70);

                   f.add(ta);//添加文本域组件

                  

                   //对话框窗体

                   d=new Dialog(f,"信息",true);

                   l=new Label();

                   okbtn=new Button("确定");

                   d.setBounds(300,100,300,200);

                   d.setLayout(newFlowLayout());

                   d.add(l);

                   d.add(okbtn);

 

                   myEvent();

 

                   f.setVisible(true);

         }

         public void myEvent()

         {

                   //对话框确定按钮监听器

                   okbtn.addActionListener(newActionListener(){

                            public voidactionPerformed(ActionEvent e)

                            {

                                     d.setVisible(false);//是对话框不可见

                            }

                   });

                   //对话框键盘按钮监视器

                   okbtn.addKeyListener(newKeyAdapter(){

                            public voidkeyPressed(KeyEvent e){

                                     if(e.getKeyCode()==KeyEvent.VK_ENTER)

                                               d.setVisible(false);

                                     tf.setText("");

                            }

                   });

                   //窗体关闭监视器

           f.addWindowListener(new WindowAdapter(){

                   public voidwindowClosing(WindowEvent e)

                   {

                            System.exit(0);

                   }

           });

           //窗体按钮监视器

           btn.addActionListener(new ActionListener(){

                   public voidactionPerformed(ActionEvent e)

                   {

                            show();

                   }

           });

           //文本框监视器

           tf.addKeyListener(new KeyAdapter(){

                            public voidkeyPressed(KeyEvent e)

                      {

                                     if(e.getKeyCode()==KeyEvent.VK_ENTER)

                                     show();

                            }

           });

         }

         public void show()

         {

                   ta.setText("");

                            StringdirPath=tf.getText();

                            File f=newFile(dirPath);

                            if(f.exists()&&f.isDirectory()){

                                     String[]name=f.list();//获取给定目录下所有的文件或文件夹

                                     for (Stringnames:name)

                                     {

                                               ta.append(names+"\r\n");//将获得的文件名追加到末尾

                                     }

                            }else

                            {

                                     l.setText("用户,您输入的路径"+dirPath+"不合法,请重新输入");

                                     d.setVisible(true);//是对话框可见

                            }

         }

         public static void main(String[] args)

         {

                   new Test();

         }

}

 

菜单

我们要添加菜单就要用到Frame的addMenubar方法,添加的是菜单栏。菜单很多项,所以根据层层添加:MenuBar菜单栏---->Menu菜单à子菜单

我们看看这三个类的常用方法:

1.      MenuBar:

a)      构造方法:MenuBar():创建新的菜单栏

b)      Menu add(Menu m):将指定的菜单添加到菜单栏

2.      Menu:

a)      构造方法:Menu(String label)构造具有指定标签的新菜单

b)      MenuItem add(MenuItem mi):将指定的菜单项添加到此菜单

3.      MenuItem

a)      构造方法:MenuItem(Stringlabel): 造具有指定的标签且没有键盘快捷方式的新菜单项

b)      voidaddActionListener(ActionListener l): 添加指定的动作侦听器,以从此菜单项接收动作事件。

 

我们基本演示下菜单的使用:

import java.awt.*;

import java.awt.event.*;

class MyMenuDemo

{

         privateFrame f;

         privateMenuBar mb;//菜单栏

         privateMenu m,subM;//主菜单,子菜单

         privateMenuItem closeMI,subMI;//关闭子菜单,子条目

         MyMenuDemo()

         {

                   init();

         }

         publicvoid init()

         {

                   f=newFrame("myWindows");

                   f.setBounds(300,100,600,500);

                   f.setLayout(newFlowLayout());

                  

                   //创建菜单栏

                   mb=newMenuBar();

                   //创建菜单

                   m=newMenu("文件");

                   subM=newMenu("子菜单");

                   //创建子菜单

                   closeMI=newMenuItem("退出");

                   subMI=newMenuItem("子条目");

 

                   f.setMenuBar(mb);

                   //菜单栏添加菜单

                   mb.add(m);

                   //菜单添加子菜单

                   m.add(subM);

                   //菜单添加关闭子菜单

                   m.add(closeMI);

                   //子菜单添加子条目

                   subM.add(subMI);

                   myEvent();

 

                   f.setVisible(true);

         }

 

         publicvoid myEvent()

         {

                   //关闭菜单添加监视器

                   closeMI.addActionListener(newActionListener(){

                            publicvoid actionPerformed(ActionEvent e)

                            {

                                     System.exit(0);

                            }

                   });

         }

         publicstatic void main(String[] args)

         {

                   newMyMenuDemo();

         }

}

 

应用:我们用IO流的读写及GUI做一个类似于window的记事本,要求可新建,保存,打开,关闭功能。

         importjava.awt.*;

import java.awt.event.*;

import java.io.*;

class MyMenuTest

{

         privateFrame f;

         privateMenuBar mb;

         privateMenu file;

         privateMenuItem open;//打开

         privateMenuItem close;//关闭

         privateMenuItem save;//保存呢

         privateMenuItem newFile;//新建

 

         privateFileDialog openF;//打开文件对话框

         privateFileDialog saveF;//保存文件对话框

         privateTextArea ta;

         privateFile ff;

         MyMenuTest()

         {

                   init();

         }

         publicvoid init()

         {

                   f=newFrame("我的java记事本");

                   f.setBounds(300,100,700,500);

                   //f.setLayout(newFlowLayout());

 

                   mb=newMenuBar();

                   file=newMenu("文件");

                   open=newMenuItem("打开");

                   save=newMenuItem("保存");

                   close=newMenuItem("关闭");

                   newFile=newMenuItem("新建");

 

                   f.setMenuBar(mb);

                   mb.add(file);

 

                   file.add(newFile);

                   file.add(open);

                   file.add(save);

                   file.add(close);

                  

                   ta=newTextArea();

                   f.add(ta);

                  

                   openF=newFileDialog(f,"我要打开");

                   saveF=newFileDialog(f,"我要保存",FileDialog.SAVE);

 

                   myEvent();

                   f.setVisible(true);   

         }

         publicvoid myEvent()

         {

                   //保存菜单事件

                   save.addActionListener(newActionListener(){

                            publicvoid actionPerformed(ActionEvent e)

                            {

                                     //判断是否已存在文件

                                     if(ff==null)

                                     {

                                               saveF.setVisible(true);

                                               Stringdir=saveF.getDirectory();

                                               Stringfile=saveF.getFile();

                                               if(dir==null||file==null)

                                               {

                                                        return;

                                               }

                                               ff=newFile(dir,file);

                                     }

 

                                     BufferedWriterbw=null;

                                     try

                                     {

                                               bw=newBufferedWriter(new FileWriter(ff));

                                               Stringtext=ta.getText();

                                               bw.write(text);

                                               bw.flush();

                                     }

                                     catch(IOException e1)

                                     {

                                               thrownew RuntimeException("文件保存失败");

                                     }finally

                                     {

                                               if(bw!=null)

                                               {

                                                        try

                                                        {

                                                                 bw.close();

                                                        }

                                                        catch(IOException e2)

                                                        {

                                                                 thrownew RuntimeException("流保存失败");

                                                        }

                                               }

                                     }

                            }

                   });

 

                   //打开一个文件菜单

                   open.addActionListener(newActionListener(){

                            publicvoid actionPerformed(ActionEvent e)

                            {

                                     openF.setVisible(true);

                                     Stringdir=openF.getDirectory();

                                     Stringfile=openF.getFile();

                                     if(dir==null||file==null)

                                     {

                                               return;

                                     }

                                     ta.setText("");

 

                                     ff=newFile(dir,file);

                                     BufferedReaderbr=null;

                                     try

                                     {

                                               br=newBufferedReader(new FileReader(ff));

                                               Stringlen=null;

                                               while((len=br.readLine())!=null)

                                               {

                                                        ta.append(len+"\r\n");//依次添加

                                               }

                                     }

                                     catch(IOException e1)

                                     {

                                               thrownew RuntimeException("文件打开失败");

                                     }finally

                                     {

                                               if(br!=null)

                                                        try

                                                        {

                                                                 br.close();

                                                        }

                                                        catch(IOException e2)

                                                        {

                                                                 thrownew RuntimeException("流关闭失败");

                                                        }

                                     }

                            }

                   });

                  

                   //新建文件菜单

                   newFile.addActionListener(newActionListener(){

                  

                            publicvoid actionPerformed(ActionEvent e){

                                     ta.setText("");

                            }

                   });

                   //关闭

                   close.addActionListener(newActionListener(){

                            publicvoid actionPerformed(ActionEvent e)

                            {

                                     System.exit(0);

                            }

                   });

                   //窗口关闭

                   f.addWindowListener(newWindowAdapter(){

                            publicvoid windowClosing(WindowEvent e)

                            {

                                     System.exit(0);

                            }

                   });

         }

         publicstatic void main(String[] args)

         {

                   newMyMenuTest();

         }

}

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值