java程序阅读题菜单_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(); //调用构造方法,创建一个窗口

}

}

运行结果如下:

06cd4e04cecdd81a18d6fb47f3a8857e.png

图一

61a8c3f2d56807df4b4de20ff121db80.png

图二

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值