Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

The problem with menus without using JSwing

Hi there...

i have a program that i am working on but i want to add a menubar at the top of the applet labeled 'options' with two fields 'Test' and 'Reset'. I have made the separte class for it but the menu pops up in its own window :SICK: . At a the moment i'm tearing my hair out because i can't seem to integrate the menu in the main applet window.
Has anyone one got any ideas?....thanks in advance. sorry for the long code but it does compile no problems 8-D

/*start with a letter but should contain a mix of letters and digits
upto 8
characters
*decimal point can have three digits or letters
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.lang.String.*;
import java.awt.Frame.*;
import java.awt.Menu;

public class RecogniseNumbers2 extends java.applet.Applet
implements ActionListener
{
final int IDLE = 0;
final int BUILDING_REAL = 1;
final int BUILDING_INT = 2;
final int INVALID = 3;
int state;//current user holds state
Panel ip;//contains user buttons
TextField input;//displays current number
TextField result;//displays result of number check
Label nameinput = new Label();//new label object
Label nameresult = new Label();//new label object
Frame f;
StringBuffer currentinput;//input characters stored here
char currentchar;
char filename;
char extension;
int dotPos = -1;
String name = null;
String ext = null;


public void init()
{
f = new CompMenuFrame("A Complex Menu");

//-------------//
// Layout Manager for the labels //
//-------------//
Panel p2 = new Panel();//new panel object p2 for labels
p2.setLayout(new GridLayout(2,1,2,2));
add("West", p2);//add the labels to the west
Label Labelinput = new Label("Filename: ");
p2.add(Labelinput);
Label Labelresult = new Label("Result: ");
p2.add(Labelresult);




//------------//
// BorderLayout for the Textfields //
//------------//
// create a 2 x 1 grid for the displays and add it to the north
// section of the root panel
Panel p = new Panel();//new panel object 'p' textfields
p.setLayout(new GridLayout(2,1,2,2));//set a grid layout for the panel
add("East", p);//add panel in the north

// create the displays and add them to the 2 x 1 grid
input = new TextField(24);//textfield size
input.setEditable(false);//read only
p.add(input);//add input textfield

// create the displays and add them to the 2 x 1 grid
result = new TextField(24);//textfield size
result.setEditable(false);//read only
p.add(result);//add result textfield

//-------------//
// Gridlayout for the panel //
//-------------//
Panel ip = new Panel();//input panel for the keypad
ip.setLayout(new GridLayout(4,3,4,4));//gridlayout for the keypad
Button b;
for (char input='a'; input < 'z' + 1; input++)//make buttons for a to z
{
b = new Button ("" + input);//send button request
b.addActionListener(this);
ip.add(b);
}//end for loop
for (char input='0'; input < '9' + 1; input++ )//make button from 0 to 9
{
b = new Button ("" + input);//send button request
b.addActionListener(this);
ip.add(b);//add buttons inside the input panel object
}//end for loop

b = new Button(".");
b.addActionListener(this);
ip.add(b);

b = new Button("fi");
b.addActionListener(this);
ip.add(b);

add("South", ip);//adds the Input panel(ip) object to the south
currentinput = new StringBuffer();
state = IDLE;//initial state

f.setVisible(true);
}//end init

class CompMenuFrame extends Frame
{
//NameDialog d;
MenuBar mb = new MenuBar();
Menu m1 = new Menu("Colour");
//Label l = new Label("A frame demonstration", Label.CENTER);
CompMenuFrame(String name)
{
super(name); //call superclass constructor
MenuBar mb = new MenuBar();
Menu m1 = new Menu("Options");
m1.add(new MenuItem("Test"));
m1.add(new MenuItem("Reset"));
mb.add(m1);
setMenuBar(mb);
//add("South",l);
//d = new NameDialog(this, "Enter your new message", true);
}
}

public void handleChar()
{

switch(state)
{
case IDLE:
result.setText("");//clear result of any display
input.setText("");//clear any previous display
if (currentchar == '.')//decimal point so must be real

{
state = BUILDING_REAL;
currentinput.append(currentchar);
//add to the input string

input.setText(currentinput.toString());
//refresh output window

break;
}

else//must be a digit
{
state = BUILDING_INT;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
case BUILDING_REAL:
{

if (currentchar == '.')//cant have two

{
state = INVALID;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
else
{
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}

}
case BUILDING_INT:
if (currentchar == '.')//has no decimal point
{
state = BUILDING_REAL;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
else
{
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;
}
case INVALID:
state = INVALID;
currentinput.append(currentchar);
input.setText(currentinput.toString());
break;

}//end switch

}//end handleChar

public void correctSize()
{
int fileNameCount = 0;
int extensionCount = 0;
boolean foundDot = false;
for(int i = 0; i < currentinput.toString().length(); i++)
{
if(foundDot)extensionCount++;
if(currentinput.toString().charAt(i) == '.')foundDot = true;
if(!foundDot)fileNameCount++;

}
if(fileNameCount > 8 || extensionCount > 3)
state = INVALID;

}

public void finish()
{
switch(state)
{
case BUILDING_REAL:
result.setText("Valid");
state = IDLE;
currentinput = new StringBuffer();
//discard old string bufffer
break;

case BUILDING_INT:
result.setText("Valid");
state = IDLE;
currentinput = new StringBuffer();
break;

case INVALID:
result.setText("Invalid");
state = IDLE;
currentinput = new StringBuffer();
}//end switch
}//end finish

public void actionPerformed(ActionEvent event)

{
String command = event.getActionCommand();

if (event.getSource() instanceof Button)
{
if (command.equals("fi"))
{
finish();
}
else
{

currentchar = command.charAt(0);
handleChar();
correctSize();
}
}
}
}
[9665 byte] By [cascade] at [2007-11-11 10:31:46]