Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

ergent! please help!

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Class comlexMenu is an applet that has an associated frame that can be displayed or hidden fashion to The frame contains a menu bar containing two menus called Colours and Font respectively.
The Colours menu allows the user to select the frame�s background colour from the following: white, light grey and yellow. The default colour is white. The following text os displayed, in black, in the middle of a label object: �A frame demonstration�.
The Font menu allows the user to select from the following font sizes and updates the text in the label object accordingly: 10, 12, 14, 16, 18 points. The default size is 12 points. An additional item called Message, allows the user to change the message displayed in the label. When selected it causes a modal dialog box, similar to that in class SimpleDialog, to appear. The dialog box contains a text field, width 30 columns, which displays the message �Enter your new message�, and a button marked �OK�. The user then enters a new message and presses the OK button whereupon the dialog box disappers and the new message appears in the frame�s label. Submit PDL designs for class ComplexMenu�s methods together with a Java implementation suitably indented and commented.
You are to implement an applet, called FileName with the user interface shown in the diagram. The user inputs a file name by clicking on the appropriate buttons. As each character is input it is displayed in the upper text field.
In addition to these buttons, a menu called Options must be included in the interface.
This menu should contain two items: Test and Reset. On selecting the Test menu option an appropriate message should appear in the lower text field indicating whether or not the current file name is valid. After this, button presses should not be accepted until the user selects the Reset menu option to clear both text fields ready for a new file name to be entered. At other times Reset should simply clear any current filename in the top text field.

//an applet with two menus and a dialog box.
import java.awt.*;

public class ComplexMenu extends java.applet.Applet
{
Button open, close; //create two instance variables to hold button objects.
Frame f; //declare variable to hold frame object

public void init( )
{
open = new Button ("Open");
add (open); //add open button.
close = new Button ("Close");
add (close); //add close button.
f = new MenuFrame ("A complex menu"); //create new menuframe.
f.resize (300, 300 );//sets it size in pixels.
f.show ( ); // make it visible.
}

public boolean action (Event evt, Object arg)//action method receives an argument of type 'event'
{ //whose 'target' field we use to identify which of the two buttons caused the event.
if (evt.target == open) //if the open button was pressed then
{ //appropriate action would be taken.
if (! f.isShowing ( ) ) //if the frame is 'not' visible
f.show ( ); //make it visible.
}
else
{
if (f.isShowing ( ) ) //if the frame 'is' visible
f.hide ( ); //make it invisible.
}
return true; //signal that we have handled the event.
}
}

class MenuFrame extends Frame //helper class which can reside in same file as complex menu because its not declared to be coderanch.
{
Label l; //declare variable to hold label object.
MessageDialog d; //declare variable to hold messagedialog object.

MenuFrame (String name)
{
super (name); //cal superclass constructor.
MenuBar mb = new MenuBar ( ); //create new menubar.
Menu m = new Menu ("Colors"); //create new menu of type color.
m.add (new MenuItem ("White") ); //add items of type string to menu.
m.add (new MenuItem ("Lightgray") );
m.add (new MenuItem ("Yellow") );
mb.add (m); //add complete menu to applets drawing surface.
setMenuBar (mb); //tell the frame about the menubar.
l = new Label ("A frame demonstration", Label.CENTER); //create new label and assign it to instance variable 1.
add ("South", l); //add the label to the applet's drawing suface, positioned bottom on bottom edge.

m = new Menu ("Font");
m.add (new MenuItem ("10") );
m.add (new MenuItem ("12") );
m.add (new MenuItem ("14") );
m.add (new MenuItem ("16") );
m.add (new MenuItem ("18") );
m.add (new MenuItem ("Message") );
mb.add (m);
setMenuBar (mb);
l = new Label ("12", Label.CENTER);
add ("North", l);
d = new MessageDialog (this, "Message", true);
}

public boolean action (Event evt, Object arg)
{
if ( evt. target instanceof MenuItem)
{
String label = (String) arg; //label of item obtained by casting arg to instance of string. string instance method if (label.equals ("White") ) //'equals' used to check string object against names and set color appropriately.
setBackground (Color.white);
else if (label.equals ("Lightgray") )
setBackground (Color.lightGray);
else if (label.equals ("Yellow") )
setBackground (Color.yellow);
return true;
}

public boolean action (Event evt, Object arg)
{
if (evt.target instanceof MenuItem)
{
String label = (String) arg;
if (label.equals ("10") )
setSize (Font.10);
else if (label.equals ("12") )
setSize (Font.12);
else if (label.equals ("14") )
setSize (Font.14);
else if (label.equals ("16") )
setSize (Font.16);
else if (label.equals ("18") )
setSize (Font.18);
else if (label.equals ("Message") )
d.show( );
return true;
}
return false;
}

public void setMessage (String message)
{
setText ("Message " + message);
}
}


class MessageDialog extends Dialog //messagedialoge, added to handle dialoge setup and functionality.
{
TextField tf;
MenuFrame parent;

MessageDialog (MenuFrame owner, String title, boolean modal)
{
super (owner, title, modal); //create actual dialog by calling constuctor with same parameters passed to messagedialog.
this.parent = owner; //'this' argument associates dialog with the frame.
setLayout (new GridLayout (2, 1, 25, 25 ) ); //gridlayout with two rows one column, separation between //component is 25 pixels in both horizontal and virtical directions.
tf = new TextField ("Enter your new message", 30);
add (tf); //add to dialog.
add (new Button ("OK") );
resize (150, 150);
}

public boolean action (Event evt, Object arg)
{
if (evt.target instanceof Button)//check for a button press.
{
String label = (String) arg;
if (label.equals ("OK") );
{
this.parent.setMessage (tf.getText ( ) );//call 'setMessage' method within 'MenuFrame' and pass
hide ( );//whatever the user typed in the 'TextField'.
}
}
return true;
}





//an applet that recognises filenames, and a menu which test whether the current filename is valid.
import java.awt.*;
import java.awt.event.*;
public class FileNames extends java.applet.Applet
implements ActionListiener
{
Button open, close;
Frame f;
final int IDLE = 0;
final int BUILDING_REAL =1;
final int BUILDING_INT =2;
final int INVALID = 3;
int state; //holds current state.
Panel ip; //contains user buttons.
TextField input; //displays current filenames.
TextField result; //displays result of number check.
StringBuffer currentinput; //input characters stored here.
char currentchar;

public void init()
{
open = new Button ("Open");
add (open);
close = new Button ("Close");
add (close);
f = new MenuFrame ("Options menu ");
f.resize (300,300); //set its size in pixels.
f.show ( ); //make it visible.

setlayout (new BorderLayout ( ) );
input = new TextField (25);
input.setEditable (false); read only.
add (new Label ("Filename:") );
add ("North", input );
result = new TextField (30);
result.setEditable (false); //read only.
1 = new Label ("Result:"; max 8 char);
add (1);
add ("North", result);

ip = new Panel ( ); //pass an applet reference.
ip.setLayout (new GridLayout (4, 8, 5, 5) );
ip.setLayout (new GridLayout (0, 5, 5, 5) );
this.parent = fn; //link back to applet
for (char input = 'a'; input < 'z' + 1; input++)
for (char input = '0'; input < '9' + 1; input++)
{
Character c = new Character (input);
ip.add (new Button (c.toString ( ) ) );
} //end for
b = new Button (".");
ip.add (b);
add ("South", ip);
currentinput = new StringBuffer ( );
state = IDLE; //initial state.
} //end init.

public boolean action (Event evt, Object arg)
{
if (evt.target == open)
{
if (!f.isShowing ( ) )
f.Show( );
}
else
{
if (f.isshowing ( ) )
f.Hide( );
}
return true;
}
}
public void handleChar ( )
{
switch (state)
{
case IDLE:
result.setText (" "); //clear result of
//any previous display.
input.setText (" "); //clear of any previous
//display.
if (ip.currentchar == 'test123.doc') //start with a letter, a mixture of letters and digits,
//followed by 3 letters and/or digits, so must be real.
{
state = BUILDING_VALID;
currentinput.append (ip.currentchar);
//add to the input string.
input.setText (currentinput.toString ( ) );
//refresh output window.
break;
}
else //must be a digit.
{
state = BUILDING_INT;
currentinput.append (ip.currentchar);
input.setText (currentinput.toString ( ) );
break;
}
case BUILDING_VALID:
if (ip.currentchar == '8test123.doc')
{
state = INVALID;
currentinput.append (ip.currentchar);
input.setText (currentinput.toString ( ) );
break;
}
else
{
currentinput.append (ip.currentchar);
input.setText (currentinput.toString ( ) );
break;
}
case BUILDING_INT:
if (currentchar == 'testing.doc')
{
state = BUILDING_REAL;
currentinput.append (ip.currentchar);
input.setText (currentinput.toString ( ) );
break;
}
else
{
currentinput.append (ip.currentchar);
input.setText (currentinput.toString ( ) );
break;
}
case INVALID:
currentinput.append (ip.currentchar);
input.setText (currentinput.toString ( ) );
break;
}//end switch
}//end handleChar


public void test ( )
{
switch (state)
{
case BUILDING_REAL:
result.setText ("The filename is valid");
state = IDLE:
currentinput = new StringBuffer ( );
//discard old string buffer
break;
case BUILDING_INT:
result.setText ("The filename is valid");
state = IDLE:
currentinput = new StringBuffer ( );
break;
case INVALID:
result.setText ("The filename is invalid");
state = IDLE:
currentinput = new StringBuffer ( );
}//end switch
}//end test


class MenuFrame extends Frame
{
MenuFrame (String name)
{
super (name); // call superclass constructor
MenuBar mb = MenuBar( );
Menu m = new Menu ("Options");
m.addActionListener (this);
m.add (new MenuItem ("Test") );
m.addActionListener (this);
m.add (new MenuItem ("Reset") );
m.addActionListener (this);
mb.add (m);
setMenuBar (mb); //tell the frame about the menubar.
}

public boolean action (Event evt, object arg)
{
if (evt.target instanceof MenuItem)
{
if ( evt.target == Test)
{
filename = test.getText ( );
{
else
}
if ( evt.target == Test.TextLenth ( ) >8)

(result.getText
{
if (reset.getText ( ).char ( ) =>8test123.doc)
if (reset.getText ( ).Char ( )=>8test456.123)
{
1.setText ("Filename invalid");
}
else
{
result = textfeild.getText ( );
1.setText ("Valid filename");
show( );
}
}
retun true;
}
}

please can anyone help!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Keith, we aren't going to do your work for you. As Ulf pointed out in your previous post, you need to let us know what your problem is. Since you are new here, have a look at How to Ask Questions on Javaranch. You should Use A Meaningful Subject Line, Tell the Details and be patient at the very least.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I'm closing this thread, since it's a duplicate of this one. Please continue the discussion over there.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic