Java初学者找不到符号错误

时间:2014-11-18 06:29:22

标签: 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);
              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);
    }

}

2 个答案:

答案 0 :(得分:1)

您的代码中存在很多问题。我将解释,但下面的代码可行。您可以将其用作指南以继续工作,因为您没有指定代码应该做什么。

最大的错误是声明Button Red = new Button("Red");之类的按钮,然后尝试将ButtonRed用作变量。

应使用equals()而不是==来比较字符串。

BorderLayout没有名为Red的字段。我冒昧地使用任意定位,北,南,东,西。

现在您可以继续改进项目了。


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
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.equals("Yellow")) 
            setBackground(Color.yellow);
    }

    public Buttons() 
    {
        setLayout(new BorderLayout(20, 5));

        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.NORTH);
        add(Yellow, BorderLayout.WEST);
        add(Cyan, BorderLayout.EAST);
        add(White, BorderLayout.SOUTH);

        Red.addActionListener(this);
        Yellow.addActionListener(this);
        Cyan.addActionListener(this);
        White.addActionListener(this);

        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);
    }
}

答案 1 :(得分:1)

首先欢迎来到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);

您应该使用naming convention表示变量,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);
}

这将始终在每次刷新每次绘制操作时将背景涂成红色,因此您的按钮事件始终被paint事件覆盖。所以你可以将它移动到构造函数。

编辑:

您应该在此See this discussion on equal vs contentEquals

上多使用equals或contentEquals
if (arg.contentEquals("Yellow"))
{
    setBackground(Color.yellow);
}

提示:你应该使用像Eclipse或Intellj这样的IDEA,你的代码格式化是一团糟。