Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error messages

 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private JTextField message;

needs to be declared inside the class.

Like this:


[ May 03, 2005: Message edited by: Henrik Engert ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your help!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic