gpt4 book ai didi

java - Eclipse 运行以前的程序?

转载 作者:行者123 更新时间:2023-11-29 07:39:16 24 4
gpt4 key购买 nike

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

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)

最佳答案

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

(另外,这个类应该命名为Main,而不是main)

关于java - Eclipse 运行以前的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32128499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com