Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Error messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error messages" Watch "Error messages" New topic
Author

Error messages

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Why do I get these errors? Im so lost...

F:\Java II\MenuReporter\RoseSarah\MenuSwing2.java:14: 'class' or 'interface' expected
private JTextField message;
^
F:\Java II\MenuReporter\RoseSarah\MenuSwing2.java:137: cannot resolve symbol
symbol : variable message
location: class MenuSwing2
message = new JTextField(30);
^
F:\Java II\MenuReporter\RoseSarah\MenuSwing2.java:138: cannot resolve symbol
symbol : variable message
location: class MenuSwing2
content.add(message, BorderLayout.CENTER);
^
F:\Java II\MenuReporter\RoseSarah\MenuSwing2.java:138: cannot resolve symbol
symbol : variable content
location: class MenuSwing2
content.add(message, BorderLayout.CENTER);
^
F:\Java II\MenuReporter\RoseSarah\MenuSwing2.java:145: cannot resolve symbol
symbol : variable message
location: class MenuSwing2
message.setText("Yummy");
^
5 errors

Tool completed with exit code 1




[CODE]

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

private JTextField message;



public class MenuSwing2 extends JFrame implements ActionListener

{
//declare variables for components we will need
private JPanel foodPanel;
private JButton food1;
private JButton food2;
private JButton food3;
private JButton food4;
private JButton food5;
private JButton food6;
private JButton food7;
private JButton food8;

private JPanel rankPanel;
private JButton rankBtn5;
private JButton rankBtn4;
private JButton rankBtn3;
private JButton rankBtn2;
private JButton rankBtn1;

private JPanel outputPanel;
private JLabel banner;
private JLabel enterEntree;
private JTextField entreeNum;

//variables to hold food names (until file input in phased in)
private String s1="Classic Nachos";
private String s2="Fajitas";
private String s3="Cajun Chicken";
private String s4="Cheeseburger Deluxe";
private String s5="Tuna Steak";
private String s6="Ribeye Steak";
private String s7="Baby Back Ribs";
private String s8="Fried Chicken";

public MenuSwing2() // constructor
{

setSize (WIDTH, HEIGHT);
addWindowListener(new WindowDestroyer());
Container contentPane = getContentPane();
JPanel buttonPanel = new JPanel();
JPanel buttonPanel1 = new JPanel();

buttonPanel.setLayout(new GridLayout(2,8));
buttonPanel1.setLayout(new GridLayout(1,5));

JButton food1 = new JButton ("Classic Nachos");
food1.setBackground (Color.RED);
food1.addActionListener(this);
buttonPanel.add(food1);


JButton food2 = new JButton ("Fajitas");
food2.setBackground (Color.RED);
food2.addActionListener(this);
buttonPanel.add(food2);

JButton food3 = new JButton ("Cajun Chicken");
food3.setBackground (Color.RED);
food3.addActionListener(this);
buttonPanel.add(food3);

JButton food4 = new JButton ("Cheeseburger Deluxe");
food4.setBackground (Color.RED);
food4.addActionListener(this);
buttonPanel.add(food4);

JButton food5 = new JButton ("Tuna Steak");
food5.setBackground (Color.RED);
food5.addActionListener(this);
buttonPanel.add(food5);

JButton food6 = new JButton ("Ribeye Steak");
food6.setBackground (Color.RED);
food6.addActionListener(this);
buttonPanel.add(food6);

JButton food7 = new JButton ("Babyback Ribs");
food7.setBackground (Color.RED);
food7.addActionListener(this);
buttonPanel.add(food7);

JButton food8 = new JButton ("Fried Chicken");
food8.setBackground (Color.RED);
food8.addActionListener(this);
buttonPanel.add(food8);

JButton rankBtn1 = new JButton ("Excellent");
rankBtn1.setBackground (Color.RED);
rankBtn1.addActionListener(this);
buttonPanel1.add(rankBtn1);

JButton rankBtn2 = new JButton ("Very Good");
rankBtn2.setBackground (Color.RED);
rankBtn2.addActionListener(this);
buttonPanel1.add(rankBtn2);

JButton rankBtn3 = new JButton ("Average");
rankBtn3.setBackground (Color.RED);
rankBtn3.addActionListener(this);
buttonPanel1.add(rankBtn3);

JButton rankBtn4 = new JButton ("Fair");
rankBtn4.setBackground (Color.RED);
rankBtn4.addActionListener(this);
buttonPanel1.add(rankBtn4);

JButton rankBtn5 = new JButton ("Poor");
rankBtn5.setBackground (Color.RED);
rankBtn5.addActionListener(this);
buttonPanel1.add(rankBtn5);


contentPane.add(buttonPanel, BorderLayout.NORTH );
contentPane.add(buttonPanel1, BorderLayout.SOUTH);


message = new JTextField(30);
content.add(message, BorderLayout.CENTER);

// refer to the "Steps for Advanced Swing programs
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Classic Nachos"))
message.setText("Yummy");


// stub
// do not complete this method yet
}
public static void main(String[] args)
{
// inFile is named MenuList.txt
// open File
// read in data for each menu item

MenuSwing2 chilis = new MenuSwing2(); //instantiate a GUI Object
chilis.setBounds(200,200,650,300);
chilis.setBackground(Color.CYAN);
chilis.setTitle("Menu Grader!");
chilis.setVisible(true);
}
}


[/CODE


) ) ) )
Henrik Engert
Ranch Hand

Joined: Apr 26, 2005
Posts: 70
private JTextField message;

needs to be declared inside the class.

Like this:


[ May 03, 2005: Message edited by: Henrik Engert ]

SCJP 5.0, SCWCD
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
First three things in a class must be
1.) package statement (if applicable)
2.) import statements (if needed)
3.) class declaration

The first error should have clued you in. It says it's expecting a class or interface, yet you declared a variable. The error messages can often tell you quite a bit, get used to reading them.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

the first error message has a line:

F:\Java II\MenuReporter\RoseSarah\MenuSwing2.java:14: 'class' or 'interface' expected

this is telling you to look at line 14 in the MenuSwing2.java file. this is about where the problem is (often it's on this line, sometimes, just above).

so, look at line 14... the error message tells you that you are doing something it's not expecting - it wants either the word 'class' or 'interface' to be there, and it didn't see it. why?

because you're declaring a variable there. In Java, variables can't really live outside of a class. so perhaps we should move it to inside the class itself.

when you get a long list of errors like this, fix the first one first, then recompile. if you fixt this first one, many if not all of the others should dissappear. although others might now pop up.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thank you so much for your help!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error messages
 
Similar Threads
Sudoku grid shading
adding a picture
Buttons on Java...
adding a picture
Buttons on Java...