每次运行一个类时,它都会打开一个新框架

时间:2013-10-03 15:00:27

标签: java class jframe

我在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)

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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

我建议您获取所有源代码,压缩它并使用DropboxGoogle Drive之类的文件共享服务并发布链接,因为与此错误相关的代码不在此处