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);
}
}
2 Answers 2
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);
}
}
10 Comments
Panel and adding the buttons on that. Frame's can only handle 1 object!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.
BorderLayoutJavaDocs, it doesn't have a field calledRed(or the other values you've tried to use). Then take a look at How do I compare strings in Java? becausearg == "Yellow"is not howString's are compared in Java...foo.equals("bar"). This would bearg.equals("Yellow")in your code.Buttonshould beJButton. The variablel names don't match the lines where you addActionListener.ButtonMagenta.addActionListner(this);should beaddActionListener(this), mind the E in Listener