0

我在调用一个类的 jmenuitem 中添加了一个 actionlistener,该类读取一个 excel 文件并在 jframe 中打开一个 jgraph。我还向不同的 jmenuitem 添加了另一个 actionlistener 来为同一个 exel 文件调用同一个类,但使用不同的 excel 表(不同的 int 参数)。但是,当我运行主框架并单击菜单项时,一次只能打开其中一个。我必须关闭一个才能打开另一个。我需要更改它,以便每当用户单击它时,每次都会出现一个新的不同框架/窗口。

public class ReadExcel {

//reads excel file sheet and saves some strings in arrays
public static ArrayList<String> RM = new ArrayList<String>() ;
...




public static void excel(String excelfile, Integer sheetno) {
...
}
}

public class graphgen extends JFrame {

//creates a graph based on ReadExcel arrays


public graphgen() {

        gen();

    }

    public void gen(){

    }

public static void main(String[] args)
    {


        graphgen frame = new graphgen();
        p2.add(graphComponent, BorderLayout.CENTER);
        frame.setLayout(new BorderLayout());
        frame.add(p2, BorderLayout.CENTER);
        frame.pack();
        frame.setResizable(true);
        frame.setSize(1600, 1200);
        frame.setVisible(true);


    }

}

具有菜单栏的主类:

   menuItem = new JMenuItem("MenuItem1",KeyEvent.VK_B);
    menuItem.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event2) {

                                    new ReadExcel();
                                 ReadExcel.excel(".xls", 0);
                                    new graphgen();
                                    graphgen.main(null);
                                }
                            });     
                        subsubmenu1.add(menuItem);


    menuItem = new JMenuItem("MenuItem2",KeyEvent.VK_C);
    menuItem.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent event2) {

                                    new ReadExcel();
                                   ReadExcel.excel(".xls", 1);
                                    new graphgen();
                                    graphgen.main(null);
                                }
                            });     
                        subsubmenu1.add(menuItem);

public static void main(String[] args)
            {


                GUIquery frame = new GUIquery();
                p.add(graphComponent, BorderLayout.CENTER);
                frame.setLayout(new BorderLayout());
                frame.add(p, BorderLayout.CENTER);
                frame.setJMenuBar(GUIquery.createMenuBar());
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(true);
                frame.setSize(1600, 1200);
                frame.setVisible(true);


            }

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: multiple points
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at graphgen.gen(graphgen.java:645)
    at graphgen.<init>(graphgen.java:62)
    at GUIquery$9.actionPerformed(GUIquery.java:713)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

2 回答 2

0

对于您显示的错误,因为在您的代码 ( at graphgen.gen(graphgen.java:645)) 中的某个时刻,您正在解析 aString并将其转换为 double。但是,该字符串不是有效的double- 因为它有多个小数点 (.) - 所以它会引发错误。您应该打印出(通过System.out.println())您尝试转换的内容并分析字符串的问题。

我建议您将所有源代码压缩,然后使用文件共享服务(例如DropboxGoogle Drive)并发布链接,因为与错误相关的代码不在此处

于 2013-10-03T21:08:43.590 回答
0

(涉及到一点猜测。)

actionPerformed在事件调度程序线程调用的方法中完成所有工作;那是负责处理事件和绘制 UI 的线程。因此,当您在这种方法中工作时,无法处理其他事件(如按钮或菜单单击)。

为此,您需要将工作放入单独的Thread.

public void actionPerformed(ActionEvent event2) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            new ReadExcel();
            ReadExcel.excel(".xls", 0);
            new graphgen();
            graphgen.main(null);
        }
    }).start();
}

如果这会打开一个新窗口,取决于 graphgen 中的代码。如果这段代码为每次调用静态main方法打开一个新窗口,并且如果代码正确处理 Swing 线程,那么它将执行此操作,但我们无法查看该代码。

通过调用静态main方法来调用其他 Java 代码总是很可疑的。您应该有一个可以使用的公开的、记录在案的 API。

于 2013-10-03T15:32:17.517 回答