你这个是对addMenu方法的定义和调用有问题,估计把Java当C++来用了。Java穿的都是引用,而不是地址,所以addMenu(menu[i], menuName[i])这里不能把创建好的menu设置会menu[]数组。
改法如下(去掉了addMenu方法):
for(int i=0; i<menulength; i++){menu[i]=new JMenu(menuName[i]);// 新建菜单
baradd(menu);// 添加到工具栏
}
public class menu extends JMenuBar implements ActionListener{
private upPanel upp;
//定义菜单
private JMenu file;
//定义菜单项
private JMenuItem exit,export;
public menu(upPanel upp){
thisupp = upp;
setMenus();
fileadd(export);
fileaddSeparator();
fileadd(exit);
thisadd(file);
}
private void setMenus(){
file = new JMenu("文件(F)");
exit = new JMenuItem("退出");
export = new JMenuItem("导出");
exitaddActionListener(this);
exportaddActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object object = egetSource();
if(object == export){
if(!ResourcesisOpenFileInputPanel){
new InputFileNameDialog(upp);
ResourcesisOpenFileInputPanel = true;
}
Systemoutprintln("导出");
}else if(object == exit){
//处理代码的地方
}
}
主要是添加菜单项的监听,绑定监听,然后写对应相应事件要处理的代码
这不简单吗,直接循环加上去就可以了。
import javaxswing;
import javaawt;
import javaawteventActionEvent;
import javaawteventActionListener;
public class List extends JFrame{
public List(){
initialization();
setTitle("三级菜单");
setBounds(300,300,400,300);
setLayout(null);
Container c=getContentPane();
setVisible(true);
setDefaultCloseOperation(WindowConstantsEXIT_ON_CLOSE);
}
public void initialization(){
JMenu jmenuTwo=null;
String[] menubar={"编辑","帮助"};
String[][] submenu={{"复制","粘贴","","历史记录"},{"关于三级菜单"}};
String[] threemenu={"撤消","恢复"};
JMenuBar jmenubar=new JMenuBar();//创建菜单栏
for(int i=0;i<menubarlength;i++){
JMenu jmenu=new JMenu(menubar[i]);
jmenubaradd(jmenu);
for(int j=0;j<submenu[i]length;j++){
if(""equals(submenu[i][j])){
jmenuaddSeparator();//添加一条横线
}else{
jmenuTwo=new JMenu(submenu[i][j]);
jmenuadd(jmenuTwo);
if(j<2&&i==0){
for(int k=0;k<threemenulength;k++){
JMenuItem menu3=new JMenuItem(threemenu[k]);
jmenuTwoadd(menu3);
}
}
}
}
thissetJMenuBar(jmenubar);//JFrame加菜单栏时用setJmenuBar(),不用add /
}
}
public static void main(String[] args){
new List();
}
}
设置窗口大小,位置,显示,窗口关闭等等,修改后的代码如下:
import javaawt;
import javaawtevent;
import javaxswing;
class Picture extends JFrame {
JMenuBar mb = new JMenuBar();
JMenu color = new JMenu("Color");
JMenuItem red = new JMenuItem("Red");
public Picture() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Systemexit(0);
}
});
Container con = getContentPane();
setJMenuBar(mb);
mbadd(color);
coloradd(red);
thissetSize(400, 300);
Toolkit tk = ToolkitgetDefaultToolkit();
Dimension screen = tkgetScreenSize();
thissetLocation(screenwidth / 2 - thisgetWidth() / 2, screenheight / 2 - thisgetHeight() / 2);
setVisible(true);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
validate();
}
public static void main(String arg[]) {
new Picture();
}
}
你这问题说的不清不楚的?
add(new JLabel("检索此字符出现次数(最长可以70): "));
text=new JTextField(70);
add(text);
是想得到 text=new JTextField(70); 中输入的内容是不
JTextField 的实例化 应该每实例化一次 就定义一个变量
text1=new JTextField(70)
text2=new JTextField(70)
text3=new JTextField(70)
这样就可以用text1paramString()
得到输入的内容了
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingJFrame;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJPopupMenu;
public class TestMenu extends JFrame {
private JPopupMenu jPopupMenu;
public TestMenu(String title){
super(title);
}
public void init(){
JMenuBar bar = new JMenuBar();
JMenu menuFile = new JMenu("文件");
JMenuItem menuFile1 = new JMenuItem("新建");
JMenuItem menuFile2 = new JMenuItem("打开");
JMenuItem menuFile3 = new JMenuItem("关闭");
JMenuItem menuFile4 = new JMenuItem("保存");
menuFileadd(menuFile1);
menuFileadd(menuFile2);
menuFileadd(menuFile3);
menuFileadd(menuFile4);
JMenu helpMenu = new JMenu("帮助");
JMenuItem helpMenu1 = new JMenuItem("新建");
JMenuItem helpMenu2 = new JMenuItem("打开");
helpMenuadd(helpMenu1);
helpMenuadd(helpMenu2);
baradd(menuFile);
baradd(helpMenu);
thissetJMenuBar(bar);
jPopupMenu = new JPopupMenu();
JMenuItem jPopupMenu1 = new JMenuItem("显示");
JMenuItem jPopupMenu2 = new JMenuItem("显示缺省");
jPopupMenuadd(jPopupMenu1);
jPopupMenuadd(jPopupMenu2);
thissetBounds(200, 200, 500, 400);
thissetResizable(false);
thissetVisible(true);
thisaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (egetButton()==MouseEventBUTTON3) {
//d出右键菜单
jPopupMenushow(TestMenuthis, egetX(), egetY());
}
}
});
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestMenu("测试菜单")init();
}
}
package example;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJFrame;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJOptionPane;
import javaxswingJPanel;
import javaxswingborderEmptyBorder;
public class demo extends JFrame{
private JMenuBar menu = new JMenuBar();
private JPanel contentPane = new JPanel();
public demo()
{
setTitle("compiler");
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setJMenuBar(menu);
//JMenu correct = new JMenu("正确用例");
//JMenu wrong = new JMenu("错误用例");
////JMenuItem WP = wrongadd("语法错误");
//JMenuItem WL = wrongadd("词法错误");
JMenu edit = new JMenu("编辑");
JMenu help = new JMenu("帮助");
JMenu document = new JMenu("文件");
JMenuItem copy = editadd("复制");
JMenu color = new JMenu("颜色");
JMenuItem p = coloradd("前景色");
paddActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPaneshowMessageDialog(null,
"该项目正在建设中", "信息",
JOptionPaneINFORMATION_MESSAGE);
}
});
JMenuItem b = coloradd("背景色");
baddActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPaneshowMessageDialog(null,
"该项目正在建设中", "信息",
JOptionPaneINFORMATION_MESSAGE);
}
});
editadd(color);
JMenuItem select = editadd("全选");
menuadd(document);
menuadd(edit);
menuadd(help);
setLocation(400,200);
setSize(300, 300);
contentPanesetBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
}
public static void main(String[]args)
{
demo browser = new demo();
browsersetVisible(true);
}
}
以上就是关于java程序问题,请高手看全部的内容,包括:java程序问题,请高手看、简单的java问题:如何给item添加事件,就是当我左击item1的时候获取"新建(N)"这个值。求一简单例子即可、java 如何用用数组设置一个窗体的三级菜单等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)