Eclipse运行以前的程序?

时间:2015-08-20 21:13:53

标签: java eclipse swing

在创建程序后,我继续在新类中创建一个新的。当我运行这个程序时,它运行了以前的程序。我已经阅读了这个问题的几个答案,但改变运行配置也不起作用......?这是我的代码,它是否在你的日食中运行?

package Userinterfaces;

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

 class Dropdown extends JFrame {

private JComboBox box;
private JLabel picture;

private static String[] filename = {"swag.png", "code.png"};
private Icon[] pics = {new ImageIcon(getClass().getResource(filename [0]))};


public Dropdown(){
    super("The title");
    setLayout(new FlowLayout());


    box = new JComboBox (filename); //Automatically put the array in a list for us

    box.addItemListener(
            new ItemListener(){ //automatically implements itemlistener
                public void itemStateChanged(ItemEvent event){
                    if (event.getStateChange() == ItemEvent.SELECTED)
                        picture.setIcon(pics[box.getSelectedIndex()]);
                }

            }
    );

    add(box);
    picture = new JLabel (pics [0]);
    add(picture);

}
}

public class main {
public static void main (String args){

    Dropdown down = new Dropdown();
    down.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    down.setSize(300,200);
    down.setVisible(true);


}
 }

现在当我更改下拉选项时,我收到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at Userinterfaces.Dropdown$1.itemStateChanged(main.java:33)
    at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
    at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.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$500(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)

5 个答案:

答案 0 :(得分:4)

主要方法必须完全正确。在主方法定义中将args更改为String []。现在Eclipse应该允许您在导航器中右键单击它并选择run。

(此外,该类应命名为Main,而不是main)

答案 1 :(得分:1)

选项1:右键单击要运行的类,然后选择运行。

选项2:通过转到“运行配置”窗口删除所有先前的配置,然后在添加文件的位置添加新配置。

答案 2 :(得分:0)

这可能是由于您的Eclipse配置。您可能已将其设置为当您单击绿色的“运行”按钮时,它将执行您执行的最后一个程序。尝试在Package Explorer中右键单击类名,然后选择“Run As - > Java Application”

答案 3 :(得分:0)

尝试改变,

public static void main (String args){

public static void main (String args[]){

答案 4 :(得分:-1)

我花了很多时间在google上但没有为我找到解决方案后,解决此问题的最简单方法是,我开设了一个新的工作场所,将我所有的课程复制到了新的工作场所,并删除了旧的工作场所。然后它像往常一样顺利运行。