2 qq 25850917 qq_25850917 于 2016.09.12 15:58 提问

javaSwing 的菜单项太多了,一个屏幕显示不了,是否可以增加滚动条,或其他方法? 20C

javaSwing 的菜单项太多了,一个屏幕显示不了,是否可以增加滚动条,或其他方法?!
见下图:
图片说明

我的代码附件:
public class Nodepad extends JFrame
{
private JTextArea editor;
private Container c;
private Font f=new Font("sanserif",Font.PLAIN,12);
//菜单栏成员属性声明
private JMenuBar mb; //菜单棒
private JMenu fileMenu; //文件菜单
private JMenu editMenu; //编辑菜单
private JMenu formatMenu; //格式菜单
private JMenuItem fileMenuOpen,fileMenuSave,fileMenuExit; //文件菜单的菜单项
private JMenuItem editMenuCopy,editMenuCut,editMenuPaste; //编辑菜单的菜单项
private JMenu formatSet;//格式菜单第一级菜单
private JMenuItem lineWrap,cancleLineWrap,wrapStyleWord,cancleWrapStyleWord;//格式菜单的第二级菜单
private JMenuItem i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20;//测试菜单
private JMenuItem i21,i22,i23,i24,i25,i26,i27,i28,i29,i30,i31,i32,i33,i34,i35,i36,i37,i38,i39,i40;//测试菜单
private JMenuItem i41,i42,i43,i44,i45,i46,i47,i48,i49,i50,i51,i52,i53,i54,i55,i56,i57,i58,i59,i60;//测试菜单

//工具栏成员属性声明
private JToolBar toolBar;   //工具棒
private JButton b1,b2,b3,b4,b5; //声明5个按钮,分别为:“打开”、“保存”、“复制”、“剪切”、“粘贴”

//弹出式菜单属性声明
private JPopupMenu pm;
private JMenuItem item1,item2,item3,item4,item5;



public Nodepad()
{
    super("简单记事本");
    setSize(400,300);

    try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e){ System.err.println("不能被设置外观的原因:"+e);}



    c=getContentPane();         //创建一个内容面板
    editor = new JTextArea();   //创建一个文本区
    c.add(new JScrollPane(editor)); //设置滚动条,并添加到内容面板

    //菜单栏的实现
    //文件菜单的实现
    mb = new JMenuBar();    //创建菜单棒
    fileMenu = new JMenu("文件(F)");//创建菜单
    fileMenuOpen = new JMenuItem("打开(O)...Ctrl+O");
    fileMenuSave = new JMenuItem("保存(S)...Ctrl+S");
    fileMenuExit = new JMenuItem("退出");
    JMHandler JM=new JMHandler();       //创建监听器
    fileMenuOpen.addActionListener(JM); //注册监听器
    fileMenuSave.addActionListener(JM);
    fileMenuExit.addActionListener(JM);
    fileMenu.add(fileMenuOpen);
    fileMenu.add(fileMenuSave);
    fileMenu.addSeparator();    //添加分隔线
    fileMenu.add(fileMenuExit);
    fileMenu.setFont(f);        //设置菜单中文体的字体


    //编辑菜单的实现
    editMenu = new JMenu("编辑(E)");
    editMenuCopy = new JMenuItem("复制(C) Ctrl+C");
    editMenuCut = new JMenuItem("剪切(T) Ctrl+X");
    editMenuPaste = new JMenuItem("粘贴(P) Ctrl+v");
    EMHandler EM=new EMHandler();   //创建监听器
    editMenuCopy.addActionListener(EM);//注册监听器
    editMenuCut.addActionListener(EM);
    editMenuPaste.addActionListener(EM);
    editMenu.add(editMenuCopy);
    editMenu.add(editMenuCut);
    editMenu.add(editMenuPaste);
    editMenu.setFont(f);


    //格式菜单的实现
    formatMenu = new JMenu("格式(O)");            //创建菜单
    formatSet = new JMenu("格式设置");          //二级菜单
    lineWrap = new JMenuItem("自动换行");       //二级菜单项
    cancleLineWrap = new JMenuItem("取消自动换行");
    wrapStyleWord = new JMenuItem("断行不断字");
    cancleWrapStyleWord = new JMenuItem("取消断行不断字");

    i1 = new JMenuItem("i1");
    i2 = new JMenuItem("i2");
    i3 = new JMenuItem("i3");
    i4 = new JMenuItem("i4");
    i5 = new JMenuItem("i5");
    i6 = new JMenuItem("i6");
    i7 = new JMenuItem("i7");
    i8 = new JMenuItem("i8");
    i9 = new JMenuItem("i9");
    i10 = new JMenuItem("i10");
    i11 = new JMenuItem("i11");
    i12 = new JMenuItem("i12");
    i13 = new JMenuItem("i13");
    i14 = new JMenuItem("i14");
    i15 = new JMenuItem("i15");
    i16 = new JMenuItem("i16");
    i17 = new JMenuItem("i17");
    i18 = new JMenuItem("i18");
    i19 = new JMenuItem("i19");
    i20 = new JMenuItem("i20");
    i21 = new JMenuItem("i21"); 
    i22 = new JMenuItem("i22"); 
    i23 = new JMenuItem("i23"); 
    i24 = new JMenuItem("i24"); 
    i25 = new JMenuItem("i25"); 
    i26 = new JMenuItem("i26"); 
    i27 = new JMenuItem("i27"); 
    i28 = new JMenuItem("i28"); 
    i29 = new JMenuItem("i29"); 
    i30 = new JMenuItem("i30"); 
    i31 = new JMenuItem("i31");
    i32 = new JMenuItem("i32");
    i33 = new JMenuItem("i33");
    i34 = new JMenuItem("i34");
    i35 = new JMenuItem("i35");
    i36 = new JMenuItem("i36");
    i37 = new JMenuItem("i37");
    i38 = new JMenuItem("i38");
    i39 = new JMenuItem("i39");
    i40 = new JMenuItem("i40");
    i41 = new JMenuItem("i41");
    i42 = new JMenuItem("i42");
    i43 = new JMenuItem("i43");
    i44 = new JMenuItem("i44");
    i45 = new JMenuItem("i45");
    i46 = new JMenuItem("i46");
    i47 = new JMenuItem("i47");
    i48 = new JMenuItem("i48");
    i49 = new JMenuItem("i49");
    i50 = new JMenuItem("i50");
    i51 = new JMenuItem("i51");
    i52 = new JMenuItem("i52");
    i53 = new JMenuItem("i53");
    i54 = new JMenuItem("i54");
    i55 = new JMenuItem("i55");
    i56 = new JMenuItem("i56");
    i57 = new JMenuItem("i57");
    i58 = new JMenuItem("i58");
    i59 = new JMenuItem("i59");
    i60 = new JMenuItem("i60");

    //wangkz
    //formatSet.setLayout(getLayout());

// System.out.println(formatSet.getAutoscrolls());
// System.out.println(formatSet.getLayout().toString());
// formatSet.setLayout(mgr);
// formatSet.resize(width, height);

    FMHandler FM=new FMHandler();               //创建事件监听器
    lineWrap.addActionListener(FM);             //注册二级菜单项的监听器
    cancleLineWrap.addActionListener(FM);
    wrapStyleWord.addActionListener(FM);
    cancleWrapStyleWord.addActionListener(FM);
    formatSet.add(lineWrap);
    formatSet.add(cancleLineWrap);
    formatSet.addSeparator();   //添加分隔线
    formatSet.add(wrapStyleWord);
    formatSet.add(cancleWrapStyleWord);

    formatSet.add(i1 );        
    formatSet.add(i2 );  
    formatSet.add(i3 );  
    formatSet.add(i4 );  
    formatSet.add(i5 );  
    formatSet.add(i6 );  
    formatSet.add(i7 );  
    formatSet.add(i8 );  
    formatSet.add(i9 );  
    formatSet.add(i10); 
    formatSet.add(i11); 
    formatSet.add(i12); 
    formatSet.add(i13); 
    formatSet.add(i14); 
    formatSet.add(i15); 
    formatSet.add(i16); 
    formatSet.add(i17); 
    formatSet.add(i18); 
    formatSet.add(i19); 
    formatSet.add(i20); 
    formatSet.add(i21); 
    formatSet.add(i22); 
    formatSet.add(i23); 
    formatSet.add(i24); 
    formatSet.add(i25); 
    formatSet.add(i26); 
    formatSet.add(i27); 
    formatSet.add(i28); 
    formatSet.add(i29); 
    formatSet.add(i30); 
    formatSet.add(i31); 
    formatSet.add(i32); 
    formatSet.add(i33); 
    formatSet.add(i34); 
    formatSet.add(i35); 
    formatSet.add(i36); 
    formatSet.add(i37); 
    formatSet.add(i38); 
    formatSet.add(i39); 
    formatSet.add(i40); 
    formatSet.add(i41); 
    formatSet.add(i42); 
    formatSet.add(i43); 
    formatSet.add(i44); 
    formatSet.add(i45); 
    formatSet.add(i46); 
    formatSet.add(i47); 
    formatSet.add(i48); 
    formatSet.add(i49); 
    formatSet.add(i50); 
    formatSet.add(i51); 
    formatSet.add(i52); 
    formatSet.add(i53); 
    formatSet.add(i54); 
    formatSet.add(i55); 
    formatSet.add(i56); 
    formatSet.add(i57); 
    formatSet.add(i58); 
    formatSet.add(i59); 
    formatSet.add(i60);

    //wangkz

// formatSet.setAutoscrolls(true);
// formatSet.setAutoscrolls

    formatMenu.add(formatSet);

// JScrollPane myScrollPane=new JScrollPane(formatSet);
// formatMenu.add(myScrollPane);
formatMenu.setFont(f);

    //将菜单全部添加菜单棒里
    mb.add(fileMenu);
    mb.add(editMenu);
    mb.add(formatMenu);

    //工具栏的实现
    //按钮分别为:“打开”、“保存”、“复制”、“剪切”、“粘贴”
    toolBar =new JToolBar();    //创建工具棒
    b1= new JButton(new ImageIcon("img/open.gif"));
    b2= new JButton(new ImageIcon("img/save.gif"));
    b3= new JButton(new ImageIcon("img/copy.gif"));
    b4= new JButton(new ImageIcon("img/cut.gif"));
    b5= new JButton(new ImageIcon("img/paste.gif"));
    TBHandler TB=new TBHandler();       //创建按钮监听器
    b1.addActionListener(TB);   b2.addActionListener(TB);
    b3.addActionListener(TB);   b4.addActionListener(TB);
    b5.addActionListener(TB);
    //把按钮全部添加到工具棒中
    toolBar.add(b1);    toolBar.add(b2);
    toolBar.add(b3);    toolBar.add(b4);
    toolBar.add(b5);


    //弹出菜单的实现
    pm =new JPopupMenu();   //创建弹出菜单
    item1 = new JMenuItem("打开");
    item2 = new JMenuItem("保存");
    item3 = new JMenuItem("复制");
    item4 = new JMenuItem("剪切");
    item5 = new JMenuItem("粘贴");
    JPHandler JP=new JPHandler();
    item1.addActionListener(JP);  //注册菜单项的鼠标事件监听器
    item2.addActionListener(JP);
    item3.addActionListener(JP);
    item4.addActionListener(JP);
    item5.addActionListener(JP);
    editor.addMouseListener(JP);  //注册文本区的鼠标事件监听器
    pm.add(item1);  pm.add(item2);
    pm.add(item3);  pm.add(item4);
    pm.add(item5);






    //把菜单栏、工具栏、弹出菜单添加到内容面板
    setJMenuBar(mb);        //显示菜单栏
    c.add(toolBar,BorderLayout.NORTH);


    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

2个回答

bashenmail
bashenmail   2016.09.13 09:34

菜单设置个高度,应该就出现滚动条了吧

wgh_930708
wgh_930708   2016.09.14 15:19

java swing中好像有个scrollbar的东西,这个就是滚动条组件

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!