java中 如何使菜单栏中的选项有 只关闭当前窗口 的功能
答案:2 悬赏:10
解决时间 2021-02-21 22:54
- 提问者网友:余味
- 2021-02-21 00:56
我已经通过setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);设置了在点击关闭按钮时,只关闭当前窗口。那如何使菜单栏中的选项有 只关闭当前窗口 的功能。
最佳答案
- 二级知识专家网友:飘零作归宿
- 2021-02-21 02:05
首先,在菜单中添加相应的菜单项。
然后,在该菜单的监听处理方法中,调用窗体的dispose()方法,销毁当前窗体。
说明:以上办法与窗体设置的setDefaultCloseOperation()的参数无关,即使该参数为DO_NOTHING_ON_CLOSE或EXIT_ON_CLOSE或HIDE_ON_CLOSE,调用窗体的dispose()方法只销毁(关闭)当前窗体。
然后,在该菜单的监听处理方法中,调用窗体的dispose()方法,销毁当前窗体。
说明:以上办法与窗体设置的setDefaultCloseOperation()的参数无关,即使该参数为DO_NOTHING_ON_CLOSE或EXIT_ON_CLOSE或HIDE_ON_CLOSE,调用窗体的dispose()方法只销毁(关闭)当前窗体。
全部回答
- 1楼网友:佛说妍妍很渣
- 2021-02-21 03:23
你的jmenu和jmenubar没有加入到jframe中.在init(string s)函数中最后面加两句menubar.add(menu); setjmenubar(menubar);就行了.
完整的程序如下:(改动的地方见注释)
//js.java文件
public class js{
public static void main(string args[]){
window win = new window("窗口1",60,60,200,190);
}
}
//window.java文件
import javax.swing.*;
import javax.swing.jframe;
public class window extends jframe{
jmenubar menubar;
jmenu menu;
jmenuitem item;
jtextfield text;
public window(string s,int x,int y,int w,int h){
init(s);
setlocation(x,y);
setsize(w,h);
setvisible(true);
}
void init(string s){
settitle(s);
menubar = new jmenubar();
menu = new jmenu("菜单");
item = new jmenuitem("菜单项",new imageicon("a.gif"));
menu.add(item);
text = new jtextfield("跳梁小丑",12);
text.sethorizontalalignment(jtextfield.center);
add(text);
menubar.add(menu);//这里加一句
setjmenubar(menubar);//这里加一句
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯