0

好的,当我告诉您我已经对此进行了尽职调查时,请相信我。我一直在尝试调试这个最长的时间,我一遍又一遍地查看代码。我检查了 Stack Overflow,没有人遇到过这个问题。

所以我试图在 Java Swing 中创建一个菜单。此菜单位于JPanel. 我知道按照惯例,它应该在 a 上,JFrame但它最适合我在 JPanel 上的情况。问题是,当我单击“功能”菜单并尝试将鼠标悬停在其中的一个菜单项上时,菜单会自动消失。我试图在 a 上实现相同的代码,JFrame但发生了同样的事情。

所以事不宜迟,代码如下:

public static String[] getRGBInputsFromDialogBox(String title) {
    JTextField rField = new JTextField(5);
    JTextField gField = new JTextField(5);
    JTextField bField=new JTextField(5);

    JPanel myPanel = new JPanel(new FlowLayout());
    myPanel.add(new JLabel("R(x,y): "));
    myPanel.add(rField);
    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
    myPanel.add(new JLabel("G(x,y): "));
    myPanel.add(gField);
    myPanel.add(Box.createHorizontalStrut(15));//spacer
    myPanel.add(new JLabel("B(x,y): "));
    myPanel.add(bField);



    String[] myClasses=ClassInfo.getClasses();                                
    //  { { { { } , { } } , { { } , { } } } , { { { } , { } } , { { } , { } } } }

    //Creating Menu Bar
    JMenuBar menuBar=new JMenuBar();
        JMenu functionsMenu=new JMenu("Functions");
            JMenuItem[] classes=new JMenuItem[myClasses.length];
            for(int i=0;i<ClassInfo.classes.length;i++) {
                String[][] classMethods=ClassInfo.showMethodsForClass(ClassInfo.classes[i]);//  { {static} , {instance} }
                final String className=ClassInfo.classes[i].getSimpleName();
                JMenuItem[] staticMethodsItem=new JMenuItem[classMethods[0].length];
                JMenuItem[] instanceMethodsItem=new JMenuItem[classMethods[1].length];
                JMenuItem staticMenuItem=new JMenuItem("Static methods");
                JMenuItem instanceMenuItem=new JMenuItem("Instance methods");
                JMenu staticMenu=new JMenu();
                staticMenuItem.add(staticMenu);
                JMenu instanceMenu=new JMenu();
                instanceMenuItem.add(instanceMenu);

                for(int a=0;a<classMethods[0].length;a++)   {
                    final String methodName=classMethods[0][a];
                    staticMethodsItem[a]=new JMenuItem(methodName);
                    staticMethodsItem[a].addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)  {
                            ClassInfo.copyToClipBoard(ClassInfo.staticWriteableString(methodName,className));
                        }
                    });
                    staticMenu.add(staticMethodsItem[a]);
                }

                for(int a=0;a<classMethods[1].length;a++)   {
                    final String methodName=classMethods[1][a];
                    instanceMethodsItem[a]=new JMenuItem(methodName);
                    instanceMethodsItem[a].addActionListener(new ActionListener()   {
                        public void actionPerformed(ActionEvent e)  {
                            ClassInfo.copyToClipBoard(ClassInfo.instanceWriteableString(methodName));
                        }
                    });
                    instanceMenu.add(instanceMethodsItem[a]);
                }

                classes[i]=new JMenuItem(className);
                JMenu methodTypeMenu=new JMenu();
                methodTypeMenu.add(staticMenuItem); //add static method submenu to the new class submenu
                methodTypeMenu.add(instanceMenuItem);//add instance method submenu to the new class submenu
                classes[i].add(methodTypeMenu); //Make the new class a submenu
                functionsMenu.add(classes[i]); //Add a new class in the functions menu
            }
    menuBar.add(functionsMenu);
    myPanel.add(menuBar);

    int result = JOptionPane.showConfirmDialog(null, myPanel, title, JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        String[] arr= {rField.getText(),gField.getText(),bField.getText()};
        return arr;
    }
    return null;
}

我知道的相当多。任何形式的帮助都将不胜感激。非常感谢!

编辑:根据要求,这是我的 courseInfo 课程:

public class ClassInfo {

    public static Class<?>[] classes={Outputer.class,Vector3.class};
/*  
    public static void main(String[] args)  {
        System.out.println(classes[0].getMethods()[4].toGenericString());
        //public static int Outputer.fib(int) for static methods
        //public double Outputer.getOutputB(int,int) for instance methods
    }
*/  
    public static String[] getClasses() {
        final JFrame classesFrame=new JFrame("Select a Class You want to Use");
        String[] classNames=new String[classes.length];
        for(int i=0;i<classes.length;i++)   {
            classNames[i]=classes[i].getName();
        }
        return classNames;
/*  
        SpinnerListModel listModel=new SpinnerListModel(classNames); 
        JPanel spinnerPanel= new JPanel();
        JPanel buttonPanel=new JPanel();
        JLabel spinnerLabel=new JLabel("Choose a Class Whose Methods You Want to Inspect");
        final JSpinner classSpinner = new JSpinner(listModel);
        spinnerPanel.add(spinnerLabel);
        spinnerPanel.add(classSpinner);

        JButton inspectButton=new JButton("Inspect Methods");
        buttonPanel.add(inspectButton);

        inspectButton.addActionListener(new ActionListener()    {
            public void actionPerformed(ActionEvent e)  {
                for(int i=0;i<classes.length;i++)   {
                    if(classes[i].getName().equals(classSpinner.getValue()))    {
                        showMethodsForClass(classes[i]);
                        classesFrame.dispose();
                        break;
                    }
                }
            }
        });

        classesFrame.add(spinnerPanel);
        classesFrame.add(buttonPanel);
        classesFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        classesFrame.pack();
        classesFrame.setVisible(true);
*/
    }//End Of Method----------------------------------------------------------------------------------------------------------------

    public static String[][] showMethodsForClass(Class<?> myClass)  {
        final String className=myClass.getSimpleName();
        Method[] methods=myClass.getMethods();
        String[] staticMethodStrings1=new String[methods.length];
        String[] instanceMethodStrings1=new String[methods.length];
        int statCount=0,instCount=0;
        for(int i=0;i<methods.length;i++)   {
            String method=methods[i].toGenericString();
            System.out.println(method); //For Debugging
            if(!method.contains("private") && !method.contains("java."))    {
                if(methodIsStatic(method))  {
                    staticMethodStrings1[statCount]=method;
                    statCount++;
                } else  {
                    instanceMethodStrings1[instCount]=method;
                    instCount++;
                }
            }
        } //End of for loop

        String[] staticMethods=new String[statCount];
        String[] instanceMethods=new String[instCount];

        for(;statCount>0;statCount--)   {
            staticMethods[statCount-1]=staticMethodStrings1[statCount-1];
        }

        for(;instCount>0;instCount--)   {
            instanceMethods[instCount-1]=instanceMethodStrings1[instCount-1];
        }
        String[][] arr= {staticMethods,instanceMethods};
        return arr;
/*      JFrame frame=new JFrame("Select a Method to Copy");
        frame.setLayout(new GridLayout());

        JPanel staticPanel=new JPanel();
        JPanel instancePanel=new JPanel();
        final JComboBox staticMethodsBox=new JComboBox(staticMethods);
        final JComboBox instanceMethodsBox=new JComboBox(instanceMethods);
        JButton copyStaticButton=new JButton("Copy this Static Method");
        JButton copyInstanceButton=new JButton("Copy this Instance Method");

        copyStaticButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)  {
                copyToClipBoard(staticWriteableString((String)staticMethodsBox.getSelectedItem(),className));
            }
        });

        copyInstanceButton.addActionListener(new ActionListener()   {
            public void actionPerformed(ActionEvent e)  {
                copyToClipBoard(instanceWriteableString((String)staticMethodsBox.getSelectedItem()));
            }
        });


        staticPanel.add(staticMethodsBox);
        staticPanel.add(copyStaticButton);

        instancePanel.add(instanceMethodsBox);
        instancePanel.add(copyInstanceButton);

        frame.add(staticPanel);
        frame.add(instancePanel);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
*/      


    }//End Of Method----------------------------------------------------------------------------------------------------------------

    private static boolean methodIsStatic(String methodString)  {
        return methodString.contains("static ");
    }//End Of Method----------------------------------------------------------------------------------------------------------------

    //public static int Outputer.fib(int) for static methods
    //public double Outputer.getOutputB(int,int) for instance methods

    public static String staticWriteableString(String methodStr,String className)   {
        return methodStr.substring(methodStr.indexOf('.')-className.length());
    }

    public static String instanceWriteableString(String methodStr)  {
        return methodStr.substring(methodStr.indexOf('.'));
    }

    public static void copyToClipBoard(String myString) {
        StringSelection stringSelection = new StringSelection (myString);
        Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
        clpbrd.setContents (stringSelection, null);
    }

}//End of Class---------------------------------------------------------------------------------------------------------------------
4

1 回答 1

1

我想我理解了这个问题。您正在与 JMenu 和 JMenuItem 混为一谈。这是结构:

JMenuBar 有一个 JMenu/JMenuItem 有一个 JMenu/JMenuItem

将您的内部 for 循环更改为此,它应该可以工作:

JMenuBar menuBar=new JMenuBar();
JMenu functionsMenu=new JMenu("Functions");
JMenuItem[] classes=new JMenuItem[myClasses.length];
for(int i=0;i<ClassInfo.classes.length;i++) {
    String[][] classMethods=ClassInfo.showMethodsForClass(ClassInfo.classes[i]);//  { {static} , {instance} }
    final String className=ClassInfo.classes[i].getSimpleName();
    JMenuItem[] staticMethodsItem=new JMenuItem[classMethods[0].length];
    JMenuItem[] instanceMethodsItem=new JMenuItem[classMethods[1].length];
    JMenu staticMenu=new JMenu("Static Methods");
    JMenu instanceMenu=new JMenu("Instance Methods");

    for(int a=0;a<classMethods[0].length;a++)   {
        final String methodName=classMethods[0][a];
        staticMethodsItem[a]=new JMenuItem(methodName);
        staticMethodsItem[a].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)  {
                ClassInfo.copyToClipBoard(ClassInfo.staticWriteableString(methodName,className));
            }
        });
        staticMenu.add(staticMethodsItem[a]);
    }

    for(int a=0;a<classMethods[1].length;a++)   {
        final String methodName=classMethods[1][a];
        instanceMethodsItem[a]=new JMenuItem(methodName);
        instanceMethodsItem[a].addActionListener(new ActionListener()   {
            public void actionPerformed(ActionEvent e)  {
                ClassInfo.copyToClipBoard(ClassInfo.instanceWriteableString(methodName));
            }
        });
        instanceMenu.add(instanceMethodsItem[a]);
    }

    classes[i]=new JMenu(className);
    classes[i].add(staticMenu); //add static method submenu to the new class submenu
    classes[i].add(instanceMenu);//add instance method submenu to the new class submenu
    functionsMenu.add(classes[i]); //Add a new class in the functions menu
}
menuBar.add(functionsMenu);
于 2013-06-14T04:43:10.937 回答