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);
 }
}
axwcode
7,8207 gold badges32 silver badges41 bronze badges
asked Nov 18, 2014 at 6:29
3
  • 3
    Start by taking a look at BorderLayout 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? because arg == "Yellow" is not how String's are compared in Java... Commented Nov 18, 2014 at 6: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 Listener Commented Nov 18, 2014 at 6:32
  • 1
    What is 'ButtonRed' ? I think its 'Red', Change to Red.addActionListener(this); Commented Nov 18, 2014 at 6:40

2 Answers 2

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);
 }
}
answered Nov 18, 2014 at 6:41
Sign up to request clarification or add additional context in comments.

10 Comments

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!
Who ever downvoted me please explain what is wrong. If you make a valid point I will gladly delete it.
@Charlie did OP specify where the buttons should go?
He didn't explicitly, but why would he make so many buttons if he only wanted one ?
@Charlie since you are very good at making assumptions, where should these buttons go? All over the screen, one next to each other?
|
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.

answered Nov 18, 2014 at 7:09

2 Comments

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.
I like how the code in your answer looks a lot like mine.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.