Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java错误:在方法getSelectedItem()中找不到符号_Java_Selecteditem - Fatal编程技术网

Java错误:在方法getSelectedItem()中找不到符号

Java错误:在方法getSelectedItem()中找不到符号,java,selecteditem,Java,Selecteditem,我到处都找遍了,但就是找不到这个错误的答案 我键入以下示例: 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 stat

我到处都找遍了,但就是找不到这个错误的答案

我键入以下示例:

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:错误:找不到符号 如果(obj.getSelectedItem().toString().equals(“obj1”)){

符号:方法getSelectedItem() 位置:对象[]类型的变量obj“

我忘了吗

import
有什么?

getSelectedItem()
不在数组中,因此您得到了错误

我想你需要

    if(obj[0].equals("obj1")

 {
     JFrame testframe = new JFrame();
     JOptionPane.showMessageDialog(testframe, "testing", "test screen", JOptionPane.INFORMATION_MESSAGE);
 }