• Java第二次作业第一题


    编写图形界面程序,在窗体中设置菜单栏,在菜单栏上添加“file”菜单,在文件菜单中添加"new"和"quit"两个菜单项,其中"quit"菜单项单击后可以退出程序。

    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();     //调用构造方法,创建一个窗口
    }
    
    }  
    

    运行结果如下:

    图一

    图二

  • 相关阅读:
    建立文件类型关联
    Delphi程序员,你们现在还好吗?
    加一文档到开始菜单中的文件夹下
    文本转换为GIF
    取消文件类型的关联
    取得任务栏的高度
    TeeChart使用范例
    同步SQL Server服务器时间
    注册系统热键
    山西襄汾溃坝事故已造成259人死亡
  • 原文地址:https://www.cnblogs.com/zqm-sau/p/9807618.html
Copyright © 2020-2023  润新知