Java第二次作业第一题

package naizi;

import java.awt.*;
import java.awt.event.*;//事件处理需要的包

import javax.swing.*;

public class EditorJFra extends JFrame implements ActionListener
{ 

public EditorJFra (){

    super("图形界面");//设置标题栏 

    this.setLocation(300,240);

    this.setSize(300,150);

    this.setLayout(new GridLayout(3,1));    //设置网格布局管理器,3行1列 

    this.add(new Label("标签")); 

    this.add(new TextField("abc",20));

    this.add(new Button("ok"));

    JMenuBar menubar = new JMenuBar();

    this.setJMenuBar(menubar);      //设置菜单栏

    JMenu menu_file = new JMenu("file"); 

    menubar.add(menu_file);         //向菜单栏添加menu_file菜单

    menu_file.add("new");     //或者menu_file.add(new JMenuItem("quit"));  向菜menu_file菜单添加"new"菜单项

    JMenuItem menuitem_exit = new JMenuItem("quit");

    menu_file.add(menuitem_exit);             //向菜menu_file菜单添加menuitem_exit菜单项

    menuitem_exit.addActionListener(this);        //给menuitem_exit菜单项添加单击事件 

    this.setVisible(true);     //显示窗体

}

public void actionPerformed(ActionEvent e){       //单击事件处理方法

        System.exit(0); 

}
public static void main(String[] args) {
    new EditorJFra();     //调用构造方法,创建一个窗口
}

}  

运行结果如下:

Java第二次作业第一题

图一

Java第二次作业第一题

图二