Java初学者找不到符号错误
我在按照书中作业的指示做。我正在创建按钮,当用户单击一个按钮“黄色”时,背景变为黄色。我得到编译错误Java初学者找不到符号错误,java,awt,Java,Awt,我在按照书中作业的指示做。我正在创建按钮,当用户单击一个按钮“黄色”时,背景变为黄色。我得到编译错误 Error; cannot find symbol add(Red, BorderLayout.Red); same goes for add(Yellow, BorderLayout.Yellow); add(Cyan, BorderLayout.CYAN); add(Magenta, BorderLayout.MAGENTA);
Error; cannot find symbol
add(Red, BorderLayout.Red);
same goes for add(Yellow, BorderLayout.Yellow);
add(Cyan, BorderLayout.CYAN);
add(Magenta, BorderLayout.MAGENTA);
add(White, BorderLayout.WHITE);
also error; cannot find symbol
for ButtonRed.addActionListener(this);
ButtonYellow.addActionListener(this);
ButtonCyan.addActionListener(this);
ButtonMagenta.addActionListner(this);
ButtonWhite.addActionListener(this);
这是我的密码
/*
Chapter 6: Borders
Programmer:Jesse-le Edwards
Date:11-16-14
Filename: Buttons.java
Purpose:
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Buttons extends Frame implements ActionListener {
public void paint(Graphics g) {
setBackground(Color.red);
}
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
if (arg == "Yellow") {
setBackground(Color.yellow);
}
}
public Buttons() {
//set the layout
setLayout(new BorderLayout(20, 5));
//Add buttons
Button Red = new Button("Red");
Button Yellow = new Button("Yellow");
Button Cyan = new Button("Cyan");
Button West = new Button("Magenta");
Button White = new Button("White");
add(Red, BorderLayout.RED);
add(Yellow, BorderLayout.YELLOW);
add(Cyan, BorderLayout.CYAN);
add(Magenta, BorderLayout.MAGENTA);
add(White, BorderLayout.WHITE);
ButtonRed.addActionListener(this);
ButtonYellow.addActionListener(this);
ButtonCyan.addActionListener(this);
ButtonMagenta.addActionListner(this);
ButtonWhite.addActionListener(this);
//override the windowClosing event
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
public static void main(String[] args) {
// set frame properties
Buttons f = new Buttons();
f.setTitle("Border Application");
f.setBounds(200, 200, 300, 300);
f.setVisible(true);
}
}
您的代码中有很多问题。我会解释,但下面的代码是有效的。您可以使用它作为继续工作的指南,因为您没有指定代码应该做什么 最大的错误是声明按钮,例如<代码>按钮红色=新按钮(“红色”)但尝试使用
按钮red
作为变量
应该使用equals()
而不是=
来比较字符串
BorderLayout没有名为Red的字段。我随意使用了一个任意的定位,北、南、东、西
现在,您可以继续改进项目
首先,欢迎来到Java以外的世界 我修复了代码的几个部分以使其运行,但主要是这部分问题最多
//set the layout
setLayout(new BorderLayout(20,5));
//Add buttons
Button Red = new Button("Red");
Button Yellow = new Button("Yellow");
Button Cyan = new Button("Cyan");
Button West = new Button("Magenta");
Button White = new Button("White");
add(Red, BorderLayout.RED);
add(Yellow, BorderLayout.YELLOW);
add(Cyan, BorderLayout.CYAN);
add(Magenta, BorderLayout.MAGENTA);
add(White, BorderLayout.WHITE);
ButtonRed.addActionListener(this);
ButtonYellow.addActionListener(this);
ButtonCyan.addActionListener(this);
ButtonMagenta.addActionListner(this);
ButtonWhite.addActionListener(this);
//set the layout
setLayout(new BorderLayout(20,5));
修复了使用命名模式的代码:
//Add buttons
Button Red = new Button("Red");
Button Yellow = new Button("Yellow");
Button Cyan = new Button("Cyan");
Button Magenta = new Button("Magenta");
Button White = new Button("White");
add(Red,BorderLayout.NORTH);
add(Yellow,BorderLayout.EAST);
add(Cyan,BorderLayout.SOUTH);
add(Magenta,BorderLayout.WEST);
add(White,BorderLayout.CENTER);
Red.addActionListener(this);
Yellow.addActionListener(this);
Cyan.addActionListener(this);
Magenta.addActionListener(this);
White.addActionListener(this);
您应该为变量使用Red=>Red,因为它不是一个类,只是一个属性
正在运行的完整代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Buttons extends Frame implements ActionListener
{
public void paint(Graphics g)
{
setBackground(Color.red);
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Yellow")
{
setBackground(Color.yellow);
}
}
public Buttons()
{
//set the layout
setLayout(new BorderLayout(20, 5));
//Add buttons
Button Red = new Button("Red");
Button Yellow = new Button("Yellow");
Button Cyan = new Button("Cyan");
Button Magenta = new Button("Magenta");
Button White = new Button("White");
add(Red, BorderLayout.NORTH);
add(Yellow, BorderLayout.EAST);
add(Cyan, BorderLayout.SOUTH);
add(Magenta, BorderLayout.WEST);
add(White, BorderLayout.CENTER);
Red.addActionListener(this);
Yellow.addActionListener(this);
Cyan.addActionListener(this);
Magenta.addActionListener(this);
White.addActionListener(this);
//override the windowClosing event
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
// set frame properties
Buttons f = new Buttons();
f.setTitle("Border Application");
f.setBounds(200, 200, 300, 300);
f.setVisible(true);
}
}
让它工作的一些技巧:
public void paint(Graphics g)
{
setBackground(Color.red);
}
这将始终在每次刷新和每次绘制操作时将背景绘制为红色,因此您的按钮事件始终被绘制事件覆盖。所以你可以把它移到构造器
编辑:
在这个问题上,您应该使用equals或contentEquals
提示:您应该使用Eclipse或Intellj这样的想法,因为您的代码格式太乱了。首先看一下JavaDocs,它没有一个名为
Red
的字段(或者您尝试使用的其他值)。然后看一看,因为arg==“Yellow”
不是Java中比较String
的方式……字符串应该与foo.equals(“bar”)
进行比较。这将是代码中的arg.equals(“黄色”)
<代码>按钮应为JButton
。变量名称与您添加ActionListener
的行不匹配<代码>按钮genta.addActionListner(此)应该是addActionListener(this)
,注意Listener中的E“ButtonRed”是什么?我想它是红色的,改成红色。addActionListener(这个);运行时,我在页面底部只看到一个按钮(白色)。尝试添加一个面板
,并在其上添加按钮<代码>帧只能处理1个对象!曾经否决过我的人请解释一下是什么错了。如果你的观点正确,我很乐意将其删除。@Charlie OP有没有指定按钮应该放在哪里?他没有明确说明,但如果他只想要一个按钮,为什么他会制作这么多按钮?@Charlie既然你很擅长做假设,这些按钮应该放在哪里?整个屏幕上,一个挨着一个?谢谢。那是我不太明白的部分。我是一个初学者,但我想我现在更了解它了。此外,我正在按照一项作业的说明进行操作,作业告诉我将背景设置为红色,然后添加按钮和actionlistener,以便在单击按钮(红色、黄色等)时更改背景。当我开始发布时,我被告知我的代码没有用4个空格正确格式化,所以我将所有内容刷新到左侧,然后按ctrl-k。我是这个网站的新手,但现在我知道我不必这么做。我喜欢你答案中的代码看起来和我的很像。
public void paint(Graphics g)
{
setBackground(Color.red);
}
if (arg.contentEquals("Yellow"))
{
setBackground(Color.yellow);
}