我到处搜索,但我找不到这个错误的答案。
我输入了这个例子:
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:error:找不到符号 if(obj.getSelectedItem()。toString()。equals(" obj1")){
symbol:方法getSelectedItem() location:Object []"
类型的变量obj我忘了
import
东西吗?
答案 0 :(得分:0)
getSelectedItem()
在数组中不存在,因此您收到错误。
我认为你需要
if(obj[0].equals("obj1")
{
JFrame testframe = new JFrame();
JOptionPane.showMessageDialog(testframe, "testing", "test screen", JOptionPane.INFORMATION_MESSAGE);
}