0

I am following instructions from an assignment in book. I am creating buttons and on one button "yellow" when a user clicks on it the background changes to yellow. I am getting compile errors.

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

Here is my code.

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

}
apxcode
  • 7,696
  • 7
  • 30
  • 41
  • 3
    Start by taking a look at [`BorderLayout`](https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html) JavaDocs, it doesn't have a field called `Red` (or the other values you've tried to use). Then take a look at [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) because `arg == "Yellow"` is not how `String`'s are compared in Java... – MadProgrammer Nov 18 '14 at 06:32
  • Strings should be compared with `foo.equals("bar")`. This would be `arg.equals("Yellow")` in your code. `Button` should be `JButton`. The variablel names don't match the lines where you add `ActionListener`. `ButtonMagenta.addActionListner(this);` should be `addActionListener(this)`, mind the E in List**e**ner – Charlie Nov 18 '14 at 06:32
  • 1
    What is 'ButtonRed' ? I think its 'Red', Change to Red.addActionListener(this); – Anptk Nov 18 '14 at 06:40

2 Answers2

1

You have a lot of problems in your code. I will explain, but this code below works. You can use it as a guide to continue working, since you did not specify what exactly the code should do.

Biggest mistake is declaring buttons such as Button Red = new Button("Red"); but then trying to use ButtonRed as a variable.

Strings should be compared using equals() not ==.

BorderLayout doesn't have a field named Red. I took the liberty of using an arbitrary positioning, NORTH, SOUTH, EAST, WEST.

Now you can continue improving the project.


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);
    }
}
apxcode
  • 7,696
  • 7
  • 30
  • 41
  • When running, I see just 1 button (white) at the bottom of the page. Try adding a `Panel` and adding the buttons on that. `Frame`'s can only handle 1 object! – Charlie Nov 18 '14 at 06:43
  • 1
    Who ever downvoted me please explain what is wrong. If you make a valid point I will gladly delete it. – apxcode Nov 18 '14 at 06:43
  • @Charlie did OP specify where the buttons should go? – apxcode Nov 18 '14 at 06:44
  • He didn't explicitly, but why would he make so many buttons if he only wanted one ? – Charlie Nov 18 '14 at 06:45
  • @Charlie since you are very good at making assumptions, where should these buttons go? All over the screen, one next to each other? – apxcode Nov 18 '14 at 06:45
  • I'm not mad at you, I'm trying to help... I think the buttons should be from top to bottom, skipping the `BorderLayout`. The OP tried to use them to make a colored border, maybe add something like that. Also, in the `actionPerformed()` function, let the `String` be compared with `.equals()` – Charlie Nov 18 '14 at 06:47
  • You used and explained the right variables and `.equals()` and as the OP didn't say where the buttons should go, I take back my downvote. The code doesn't work as expected yet, but that's the OP's problem I guess. – Charlie Nov 18 '14 at 06:52
  • @Charlie Thanks, I moved the buttons to NORTH, SOUTH, WEST, and EAST. Perhaps to make OP work for his assignment a bit. – apxcode Nov 18 '14 at 06:55
  • To be clearer I am following an assignment with instructions to change names of the buttons, set background color, add actionlistener to the buttons and so forth. Thank you for your input It has been helpful. – Jesse Edwards Nov 20 '14 at 07:50
  • @JesseEdwards if your question is solved you can accept 1 answer to let others know it is solved. – apxcode Nov 20 '14 at 08:34
1

First off all welcome to the world off Java

I fixed several parts of your code to make it run, but mainly this part had the most problems

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

Fixed code using your naming schema:

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

You should use a naming convention for variables, Red => red, because it isn't a class and just a property.

Your complete code that's running:

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

Some tips to make it work:

public void paint(Graphics g)
{
    setBackground(Color.red);
}

this will always paint the background red on every refresh every draw action, so your button event is ALWAYS overruled by the paint event. So you could move it to the constructor.

Edit:

You should use equals or contentEquals more on this See this discussion on equal vs contentEquals

if (arg.contentEquals("Yellow"))
{
    setBackground(Color.yellow);
}

TIP: You should using a IDEA like Eclipse or Intellj, your code formatting was a mess.

Community
  • 1
  • 1
  • Thank you. That was the part i didnt really understand. I am a beginner but i think I am understanding it better now. Also I am following an assignment with instructions it told me to set the background red and then add the buttons and actionlistener so that when the buttons (red, yellow, etc) is clicked it changes the background. Sorry about my format when I when to post it initially I was told my code was not formated properly by 4 spaces so I flushed everything to the left then hit ctrl k. I am new to this site but now I know I didnt have to that. – Jesse Edwards Nov 20 '14 at 08:00
  • I like how the code in your answer looks a lot like mine. – apxcode Nov 20 '14 at 08:36