Error during Compile

N

NickyNick

My program has a MenuFrame with with MenuItems in it. If I click on
"New", I would like to open the Filedialog box. I am a beginner and
experimenting with some basic programs. I get an error <identifier>
expected, on the line where I have specified
" f.setVisible(true); "
Can some one please provide any insight into this. I have looked into
this program a number of times and I am unable to rectify this error.

Thanks in advance.
Nicky.



import java.awt.*;
import java.awt.event.*;
import java.applet.*;


/*
<applet code="Menufile" width=600 height=600>
</applet>
*/


//Create a subclass for frame
class MenuFrame extends Frame
{
String msg="";
CheckboxMenuItem debug, test;


MenuFrame(String title)
{
super(title);


//create menu bar and add it to frame
MenuBar mbar=new MenuBar();
setMenuBar(mbar);


//create the menu items
Menu file=new Menu ("File");
MenuItem item1,item2,item3,item4,item5,item6;
file.add(item1=new MenuItem("New...."));
file.add(item2=new MenuItem("Open...."));
file.add (item3=new MenuItem("Close...."));
file.add(item4=new MenuItem("Save...."));
file.add(item5=new MenuItem("-"));
file.add(item6=new MenuItem("Quit...."));


mbar.add(file);


Menu edit=new Menu("Edit");
MenuItem item7, item8, item9, item10;
edit.add(item7=new MenuItem("Cut...."));
edit.add(item8=new MenuItem("Copy...."));
edit.add(item9=new MenuItem("Paste...."));
edit.add(item10=new MenuItem("-"));
Menu sub=new Menu("Special");
MenuItem item11, item12, item13;
sub.add(item11=new MenuItem("First"));
sub.add(item12=new MenuItem("Second"));
sub.add(item13=new MenuItem("Third"));
edit.add(sub);


//these are checkable menu items
debug=new CheckboxMenuItem("Debug");
edit.add(debug);
test=new CheckboxMenuItem("testing");
edit.add(test);
mbar.add(edit);


//create an object to handle action and item events
MyMenuHandler handler=new MyMenuHandler(this);
//register it to receive those events
item1.addActionListener (handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener (handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
//item10.addActionListener(handler);
item11.addActionListener(handler);
item12.addActionListener(handler);
item13.addActionListener (handler);
debug.addItemListener(handler);
test.addItemListener(handler);


//create an object to handle window events
MyWindowAdapter adapter=new MyWindowAdapter(this);
//register it to receive those events
addWindowListener(adapter);
}


public void paint(Graphics g)
{
g.drawString(msg,10,200);
if(debug.getState())
g.drawString("Debug is on....",10,220);
else
g.drawString("Debug is off....",10,220);
if(test.getState())
g.drawString("Testing is on....",10,240);
else
g.drawString("Testing is off....",10,240);
}



}


class MyWindowAdapter extends WindowAdapter
{
MenuFrame menuFrame;
public MyWindowAdapter(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}
public void windowClosing(WindowEvent we)
{
menuFrame.setVisible(false);
}


}


class FileDialogDemo
{
Frame f=new MenuFrame("File Dialog Demo");
f.setVisible(true);
}

class MyMenuHandler implements ActionListener, ItemListener
{
MenuFrame menuFrame;
public MyMenuHandler(MenuFrame menuFrame)
{
this.menuFrame=menuFrame;
}


//Handle action events
public void actionPerformed(ActionEvent ae)
{
String msg="You selected";
String arg=(String)ae.getActionCommand();
if (arg.equals("New..."))
{
msg+="New...";
FileDialog fd=new FileDialog(f,"File Dialog", FileDialog.LOAD);

fd.setVisible(true);
// fd.setSize(100,100);
}


else if (arg.equals("Open..."))
msg+="Open...";
else if (arg.equals("Close..."))
msg+="Close...";
else if (arg.equals("Save..."))
msg+="Save...";
else if (arg.equals("Quit..."))
msg+="Quit...";
else if (arg.equals("Edit..."))
msg+="Edit...";
else if (arg.equals("Cut..."))
msg+="Cut...";
else if (arg.equals("Copy..."))
msg+="Copy...";
else if (arg.equals("Paste..."))
msg+="Paste...";
else if (arg.equals("First..."))
msg+="First...";
else if (arg.equals("Second..."))
msg+="Second...";
else if (arg.equals("Third..."))
msg+="Third...";


else if (arg.equals("Debug..."))
msg+="Debug...";
else if (arg.equals("Testing..."))
msg+="Testing...";
menuFrame.msg=msg;
menuFrame.repaint();
}


//Handle item events
public void itemStateChanged(ItemEvent ie)
{
menuFrame.repaint ();
}


}
//Create frame window
public class Menufile extends Applet
{
Frame f;
public void init()
{
f=new MenuFrame("Menu Demo");
int width=Integer.parseInt(getParameter("width"));
int height=Integer.parseInt(getParameter("height"));
setSize(new Dimension(width,height));
f.setSize(width,height);
f.setVisible(true);
}
public void start()
{
f.setVisible (true);
}


public void stop()
{
f.setVisible(false);
}
}
 
T

Thomas Hawtin

NickyNick said:
My program has a MenuFrame with with MenuItems in it. If I click on
"New", I would like to open the Filedialog box. I am a beginner and
experimenting with some basic programs. I get an error <identifier>
expected, on the line where I have specified
" f.setVisible(true); "

Here's the code in question.
class FileDialogDemo
{
Frame f=new MenuFrame("File Dialog Demo");
f.setVisible(true);
}

Presumably you mean that to run when the program is started as an
application (the class isn't referenced anywhere else). The way to do
that is:

class FileDialogDemo {
public static void main(String[] args) {
Frame f=new MenuFrame("File Dialog Demo");
f.setVisible(true);
}
}

If it were a Swing program (and probably a good idea anyway), for
obscure reasons, the standard boilerplate is:


class FileDialogDemo {
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Frame f=new MenuFrame("File Dialog Demo");
f.setVisible(true);
}
});
}
}

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top