【问题标题】:Java Error: cannot find symbol in method getSelectedItem() [duplicate]Java错误:在方法getSelectedItem()中找不到符号[重复]
【发布时间】:2016-06-22 14:06:14
【问题描述】:

我到处搜索,但找不到这个错误的答案。

我输入这个例如:

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.BevelBorder;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.*;

public class ComboBox {
public static void main(String[] args) {
Object[] obj = { "obj1", "obj2", "obj3", "obj4" };
    String initialSelection = "obj1";
    Object selection = JOptionPane.showInputDialog(null, "Please select an option.",
 "ComboBox", JOptionPane.QUESTION_MESSAGE, null, obj, initialSelection);
 if(obj.getSelectedItem().toString().equals("obj1")) {
     JFrame testframe = new JFrame();
                JOptionPane.showMessageDialog(testframe, "testing", "test screen", JOptionPane.INFORMATION_MESSAGE);
 }
}
}       

我收到以下错误消息:

"ComboBox.java:14: 错误:找不到符号 if(obj.getSelectedItem().toString().equals("obj1")) {

符号:方法 getSelectedItem() 位置:Object[] 类型的变量 obj"

我是不是忘记了

import

什么?

【问题讨论】:

    标签: java selecteditem


    【解决方案1】:

    getSelectedItem() 不在数组中,因此您会收到错误消息。

    我认为你需要

        if(obj[0].equals("obj1")
    
     {
         JFrame testframe = new JFrame();
         JOptionPane.showMessageDialog(testframe, "testing", "test screen", JOptionPane.INFORMATION_MESSAGE);
     }
    

    【讨论】:

      猜你喜欢
      • 2014-03-14
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2016-11-10
      • 2018-05-13
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多