简体   繁体   English

线程“主”中的异常java.lang.IllegalArgumentException:setSelectedIndex:4超出范围

[英]Exception in thread “main” java.lang.IllegalArgumentException: setSelectedIndex: 4 out of bounds

I am trying to make a game, but my code simply does not work and I have no idea why. 我正在尝试制作游戏,但是我的代码根本无法正常工作,我也不知道为什么。 I am a novice programmer with little experience, and have become very frustrated with my code. 我是一个新手程序员,经验不足,对我的代码感到非常沮丧。 I have been trying to fix this problem, but to no avail. 我一直在尝试解决此问题,但无济于事。 Please assist me. 请帮我。

public class hello {
    //Int's and things
    static JButton Play = new JButton("<PLAY>");
    static JFrame pane = new JFrame("CIrCUT 0.0.2");
    static JLabel Title = new JLabel("CIrCUT");
    static JLabel none = new JLabel(" ");
    static JPanel panel = new JPanel(new GridLayout(10, 10, 10, 10));
    static JButton Options = new JButton("<OPTIONS>");
    static JPanel panel2 = new JPanel(new GridLayout(10, 10, 10, 10));
    static String b[] = new String[3];
    static int panelLoct = 1;
    JComboBox optionlist = new JComboBox();

    void initialize() {
        b[0] = "High";
        b[1] = "Medium";
        b[2] = "Low";

        //title
        pane.setTitle("CIrCUT 0.0.2");
        //drop down
        optionlist .setModel(new DefaultComboBoxModel(new String[] {"Option", "High", "Medium",  "Low"}));
        optionlist.setSelectedIndex(4);
        optionlist.addActionListener((ActionListener) this);
        //other pane-related things
        if (panelLoct == 1) {
            pane.setLayout(new GridLayout(10, 10));
            panel.setMaximumSize(new Dimension(500, 500));
            pane.setSize(500, 500);
            pane.setMaximumSize(new Dimension(500, 500));
            panel.add(Title);
            panel.add(none);
            panel.add(Play);
            panel.add(Options);
            panel2.add(optionlist);
            Play.setSize(new Dimension(500, 450));
            pane.setLocation(500, 50);
            pane.setBackground(Color.lightGray);
            pane.setContentPane(panel);
            pane.pack();
            pane.setMinimumSize(new Dimension(500, 500));
            pane.setContentPane(panel);
            OptionButtonHandler cbHandler = new OptionButtonHandler();
            Options.addActionListener(cbHandler);
            pane.setVisible(true);
        }
    }

    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    private static class OptionButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            pane.remove(panel);
            pane.add(panel2);
        }
    }

    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox) e.getSource();
        cb.getSelectedItem();
    }

    public static void main(String args[]) {
        hello a = new hello();
        a.initialize();
    }
}

I'm thinking that the problem is the JComboBox, but whenever i remove it, i just get an error. 我在想问题是JComboBox,但是每当我删除它时,我都会得到一个错误。

EDIT 编辑

here is the error 这是错误

at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at hello.initialize(hello.java:36)
at hello.main(hello.java:81)
JComboBox optionlist = new JComboBox();
optionlist.setSelectedIndex(4);

This will cause an exception since you're setting the index of an empty JComboBox. 这将导致异常,因为您正在设置 JComboBox的索引。 Simply don't do this. 根本不要这样做。 Fill the combo box with items before selecting an index. 选择索引之前,请在组合框中填充项目。

In fact, where do you add any items to the JComboBox? 实际上,您在哪里向JComboBox添加任何项目?

Note that in the future if you have a question about an exception that your code generates, you'll want to post the actual exception stacktrace here as well as indicate for us which line throws the exception. 请注意,将来如果您对代码所生成的异常有疑问,则需要在此处发布实际的异常堆栈跟踪,并为我们指出哪一行引发了异常。

You are setting the model of ComboBox that is having only 4 elements, so the maximum allowed index will be 3(as its 0,1,2,3). 您正在设置仅包含4个元素的ComboBox模型,因此允许的最大索引为3(因为其0、1、2、3)。 So if you are trying to set the selected index to 4th element you must set it 3 as follows : 因此,如果尝试将所选索引设置为第4个元素,则必须将其设置为3,如下所示:

optionlist .setModel(new DefaultComboBoxModel(new String[] {"Option", "High", "Medium",  "Low"}));
    optionlist.setSelectedIndex(3);

First add some option in optionlist then do this optionlist.setSelectedIndex(4); 首先在选项列表中添加一些选项,然后执行此选项列表。setSelectedIndex(4);

 JComboBox optionlist = new JComboBox();
 optionlist .setModel(new DefaultComboBoxModel(new String[] {"Option", "Baby Ride Frame   s/s", "Baby Ring",  "Baby Tri Cycle Frame", "Baby Tri Cycle Frame D/Seat"}));
 optionlist.setSelectedIndex(4);

I was having the exact same problem with eclipse. 我在日食方面遇到了完全相同的问题。 The error was "setSelctedIndex(0) out of bounds. I solved it by making sure all servers were running on eclipse. That includes mysql, apache and the tomcat server to test for webpages. If you are using eclipse then make sure all local server are on. 错误是“ setSelctedIndex(0)超出范围。我通过确保所有服务器都在eclipse上运行来解决它。这包括mysql,apache和tomcat服务器来测试网页。如果使用eclipse,请确保所有本地服务器在...上。

That is Because your are trying to show that item which is not Available into the Array list. 那是因为您正试图在数组列表中显示不可用的项目。 so make sure that what ever you are trying to show that is Available into the Array list remember that Arraylist is Starting from 因此,请确保您要尝试显示的所有内容都可以在Array列表中使用,请记住Arraylist是从

JComboBox optionlist = new JComboBox();
optionlist.setSelectedIndex(4);

and you dont have 4th index. 而且您没有第4个索引。 Even your combobox is empty so there is not available 4th one. 即使您的组合框是空的,所以没有可用的第四组合框。 and or do this. 和或这样做。

JComboBox optionlist = new JComboBox();
optionlist.setSelectedIndex(-1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Querydsl - 线程“main”中的异常java.lang.IllegalArgumentException:没有给出的来源 - Querydsl - Exception in thread “main” java.lang.IllegalArgumentException: No sources given 线程“ main”中的异常java.lang.IllegalArgumentException:绑定必须为正 - Exception in thread “main” java.lang.IllegalArgumentException: bound must be positive 线程“main”中的异常java.lang.IllegalArgumentException:n必须为正数 - Exception in thread “main” java.lang.IllegalArgumentException: n must be positive 线程“主”java.lang.IllegalArgumentException 中的异常:输入 == null - Exception in thread "main" java.lang.IllegalArgumentException: input == null REST API 中的线程“主”java.lang.IllegalArgumentException 中的异常 - Exception in thread “main” java.lang.IllegalArgumentException in REST API 线程“主”中的异常java.lang.IllegalArgumentException:无效的ObjectId [7887978] - Exception in thread “main” java.lang.IllegalArgumentException: invalid ObjectId [7887978] 我在带有jsoup的线程“ main” java.lang.IllegalArgumentException中有一个异常 - I have an exception in thread “main” java.lang.IllegalArgumentException with jsoup 线程“ main”中的异常java.lang.IllegalArgumentException:in不能为null - Exception in thread “main” java.lang.IllegalArgumentException: in must not be null 线程“ main”中的异常java.lang.IllegalArgumentException:URI不是分层的 - Exception in thread “main” java.lang.IllegalArgumentException: URI is not hierar chical scala 线程“主”中的 vscode 异常 java.lang.IllegalArgumentException:名称 - scala vscode Exception in thread "main" java.lang.IllegalArgumentException: name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM