簡體   English   中英

JComboBox給出了IllegalComponentStateException並且我不明白為什么

[英]JComboBox gives IllegalComponentStateException and I don't understand why

我正在為一個模仿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變量。 誰能解釋為什么會這樣,我該如何解決?

** * ** * ** * **** 編輯 * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ***

這是當我從下拉菜單中選擇一個新選項時生成的錯誤消息。 在我按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)

為什么使用addActionListener來偵聽ComboBox項的選擇? 您是否嘗試過使用addItemListener代替?

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

不,因為您沒有提供完整的信息。

由於該問題不值得為您提供一個可運行的示例,因此我做到了:我添加了vars mainFrame,choices和choice,並將字符串值提供給choices數組,因此從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);
    }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM