JComboBox给出了IllegalComponentStateException,我不明白为什么

时间:2014-02-23 21:29:19

标签: java exception user-interface jcombobox

我正在编写一个模拟RAM块的简单程序的GUI,我想使用JComboBox让用户从下拉列表中选择一个命令选项。但是,每当我尝试从列表中选择一个选项时,它会给我一个IllegalComponentStateException,并说该组件必须在屏幕上显示以确定其位置。这是创建主屏幕的代码。

  public static void createMainMenu(){
    mainFrame.getContentPane().removeAll();

    mainFrame.setSize(700 , 500);
    mainFrame.setBackground(Color.WHITE);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setLayout(new GridLayout(3 , 3));

    JPanel ramStatus = new JPanel();
    ramStatus.setBackground(Color.WHITE);
    ramStatus.setSize(200 , 200);

    JLabel heading = new JLabel("Please Select An Option to Continue.");

    JComboBox<String> userChoice = new JComboBox<String>();

    for(int i = 0; i < choices.length; i++)
        userChoice.addItem(choices[i]);

    userChoice.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
        JComboBox combo = (JComboBox)e.getSource();

        choice = combo.getSelectedItem().toString();
    }});

    JButton enter = new JButton("Enter");
    enter.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){

        handleSelection(choice);
    }});

    mainFrame.add(new JPanel());
    mainFrame.add(userChoice);
    mainFrame.add(enter);

    mainFrame.add(new JPanel());
    mainFrame.add(heading);
    mainFrame.add(new JPanel());

    mainFrame.add(new JPanel());
    mainFrame.add(ramStatus);
    mainFrame.add(new JPanel());

    //Set the frame visible.
    mainFrame.setVisible(true);
}

我应该认为该选择是在类声明之后声明的静态String变量。任何人都可以解释为什么会发生这种情况以及如何解决它?

** * ** * ** * 的**** 修改 * ** * ** * ** < EM> * ** * ** * ** * ** * ** < EM> * ** * ** * ** * ** * ***

以下是从下拉菜单中选择新选项时生成的错误消息。在我按Enter键之前抛出异常,因此它与外部方法无关。

 java.awt.IllegalComponentStateException: component must be showing on the screen to determine       
 its location
    at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2044)
    at java.awt.Component.getLocationOnScreen(Component.java:2018)
    at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:390)
    at sun.lwawt.macosx.CAccessibility$22.call(CAccessibility.java:388)
    at sun.lwawt.macosx.LWCToolkit$CallableWrapper.run(LWCToolkit.java:532)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
    at sun.lwawt.macosx.LWCToolkit$CPeerEvent.dispatch(LWCToolkit.java:689)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:0)

为什么使用addActionListener收听ComboBox项目选择?您是否尝试过使用addItemListener

答案 1 :(得分:0)

Can anyone explain why this is happening and how I can fix it?

不,因为您没有提供完整的信息。

由于这个问题不值得为你做一个可运行的例子,我做到了:我添加了vars mainFrame,选项和选项,并给了数组的字符串值,我从JComboBox类型中删除了“String”参数(运行1.6,JComboBox不通用),我注释掉了对handleSelection的调用。我启动了UI,可以多次从下拉列表中选择值而没有错误。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


    public class ComboBoxPlay
    {

      private static JFrame mainFrame = null;
      private static String[] choices = {"one", "two", "three" };
      private static String choice = null;

      public static void main(String[] args)
      {
         mainFrame = new JFrame();
        createMainMenu();

      }

      public static void createMainMenu(){
        mainFrame.getContentPane().removeAll();

        mainFrame.setSize(700 , 500);
        mainFrame.setBackground(Color.WHITE);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLayout(new GridLayout(3 , 3));

        JPanel ramStatus = new JPanel();
        ramStatus.setBackground(Color.WHITE);
        ramStatus.setSize(200 , 200);

        JLabel heading = new JLabel("Please Select An Option to Continue.");

        JComboBox userChoice = new JComboBox();

        for(int i = 0; i < choices.length; i++)
            userChoice.addItem(choices[i]);

        userChoice.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
            JComboBox combo = (JComboBox)e.getSource();

            choice = combo.getSelectedItem().toString();
        }});

        JButton enter = new JButton("Enter");
        enter.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){

    //        handleSelection(choice);
        }});

        mainFrame.add(new JPanel());
        mainFrame.add(userChoice);
        mainFrame.add(enter);

        mainFrame.add(new JPanel());
        mainFrame.add(heading);
        mainFrame.add(new JPanel());

        mainFrame.add(new JPanel());
        mainFrame.add(ramStatus);
        mainFrame.add(new JPanel());

        //Set the frame visible.
        mainFrame.setVisible(true);
    }
    }