Java 找不到符号错误。找不到getSelectedItem()和setBackground()方法
当试图运行我的程序时,我得到一个错误,因为它找不到我的方法。 以下是第一个错误代码:Java 找不到符号错误。找不到getSelectedItem()和setBackground()方法,java,compiler-errors,actionlistener,jcombobox,Java,Compiler Errors,Actionlistener,Jcombobox,当试图运行我的程序时,我得到一个错误,因为它找不到我的方法。 以下是第一个错误代码: Error: cannot find symbol symbol: method getSelectedItem() location: variable event of type java.awt.event.ActionEvent 下面是第二个错误代码: Error: cannot find symbol symbol: method setBackground(java.awt.Co
Error: cannot find symbol
symbol: method getSelectedItem()
location: variable event of type java.awt.event.ActionEvent
下面是第二个错误代码:
Error: cannot find symbol
symbol: method setBackground(java.awt.Color)
location: variable x of type java.lang.Object
以下是程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class PanelColor extends JFrame implements ActionListener{
// Declare all instance data (primitives and objects used)
private int WIDTH = 501;
private int HEIGHT = 501;
JPanel panelN;
JPanel panelS;
JPanel panelE;
JPanel panelW;
JPanel panelC;
Container con;
JComboBox box;
Dimension dPN;
Dimension dPS;
Dimension dPE;
Dimension dPW;
Dimension dPC;
//constructor
public PanelColor(){
super("Panel Color");
con = getContentPane();
con.setLayout(new BorderLayout());
setSize(WIDTH, HEIGHT);
panelN = new JPanel();
panelS = new JPanel();
panelE = new JPanel();
panelW = new JPanel();
panelC = new JPanel();
dPC = new Dimension(25,25);
dPS = new Dimension(200,200);
dPE = new Dimension(400,200);
dPW = new Dimension(400,200);
dPN = new Dimension(200,200);
panelC.setBackground(Color.RED);
panelN.setBackground(Color.YELLOW);
panelE.setBackground(Color.BLUE);
panelW.setBackground(Color.GREEN);
panelS.setBackground(Color.MAGENTA);
panelC.setPreferredSize(dPC);
panelN.setPreferredSize(dPN);
panelE.setPreferredSize(dPE);
panelW.setPreferredSize(dPW);
panelS.setPreferredSize(dPS);
box = new JComboBox();
box.addItem("Panel North");
box.addItem("Panel South");
box.addItem("Panel East");
box.addItem("Panel West");
box.addItem("Panel Center");
box.addActionListener(this);
con.add(panelC, BorderLayout.CENTER);
con.add(panelE, BorderLayout.EAST);
con.add(panelW, BorderLayout.WEST);
con.add(panelN, BorderLayout.NORTH);
con.add(panelS, BorderLayout.SOUTH);
con.add(box, BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
Random r = new Random();
int red = r.nextInt(255) + 1;
int green = r.nextInt(255) + 1;
int blue = r.nextInt(255) + 1;
Color c = new Color(red, green, blue);
String text = event.getSelectedItem();
Object x = event.getSelectedItem();
if(source == text){
x.setBackground(c);
}
}
public static void main(String[] args){
PanelColor object = new PanelColor();
}
}
我知道有很多关于“找不到符号”错误的帖子,但在我看过的所有帖子中,没有一篇对我解决这个问题有所帮助我想知道为什么它会给我这些错误?以及如何修复它,以便我知道以供将来参考。本段下面列出了错误所在的区域。我希望这也能帮助其他人解决类似问题
以下是存在错误的区域:
String text = event.getSelectedItem();
Object x = event.getSelectedItem();
if(source == text){
x.setBackground(c);
}
是一种JComboBox
方法,而不是ActionEvent
JComboBox comboBox = (JComboBox) event.getSource();
...
String text = comboBox.getSelectedItem().toString();
是一种JComboBox
方法,而不是ActionEvent
JComboBox comboBox = (JComboBox) event.getSource();
...
String text = comboBox.getSelectedItem().toString();
问题是
getSelectedItem()代码>在ActionEvent
类中定义
您需要将其更改为以下内容:
String text = (String) box.getSelectedItem();
问题是getSelectedItem()代码>在ActionEvent
类中定义
您需要将其更改为以下内容:
String text = (String) box.getSelectedItem();
如果您已经阅读了所有这些答案,那么找不到符号的含义是什么?不要阅读其他答案来找到专门针对您的问题的解决方案。了解此类错误发生的时间和原因,并了解如何修复。如果您已经阅读了所有这些答案,那么找不到符号的含义是什么?不要阅读其他答案来找到专门针对您的问题的解决方案。了解此类错误发生的时间和原因,并了解如何修复它。或者使用event.getSource()
我想。但是在这种情况下,box
是直截了当的。但是我如何修复要找到的setBackground方法?我如何将其分配给x对象?尽管如此,我更改了代码,使其不依赖于此,感谢帮助String
x
不是一个组件,因此您无法设置其背景颜色。您似乎正在尝试设置JComboBox
所选项目的颜色。在这种情况下,我想您需要一个或使用event.getSource()
。但是在这种情况下,box
是直截了当的。但是我如何修复要找到的setBackground方法?我如何将其分配给x对象?尽管如此,我更改了代码,使其不依赖于此,感谢帮助String
x
不是一个组件,因此您无法设置其背景颜色。您似乎正在尝试设置JComboBox
所选项目的颜色。在这种情况下,您需要一个