Menu not getting displayed!
Hi guys! The below code opens the applet and the frame, but nothing is really getting displayed.. What could be the problem?
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class MenuDemo extends Frame {
String msg = " ";
MenuDemo(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 = new MenuItem("New");
MenuItem item2 = new MenuItem("Open");
File.add(item1);
File.add(item2);
mbar.add(File);
MyMenuHandler handler = new MyMenuHandler(this);
// register it to receive those events
item1.addActionListener(handler);
item2.addActionListener(handler);
MyWindowAdapter adapter = new MyWindowAdapter(this);
// register it to receive those events
addWindowListener(adapter);
}
class MyWindowAdapter extends WindowAdapter{
MenuDemo menuFrame;
public MyWindowAdapter(MenuDemo menuFrame){
this.menuFrame = menuFrame;
}
public void windowClosing(WindowEvent we){
menuFrame.setVisible(false);
}
}
class MyMenuHandler implements ActionListener, ItemListener {
MenuDemo menuFrame;
public MyMenuHandler(MenuDemo 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.";
else if(arg.equals("Open..."))
msg += "Open.";
}
public void itemStateChanged(ItemEvent ie) {
menuFrame.repaint();
}
}
public void paint(Graphics g) {
g.drawString(msg, 10, 10);
}
}
public class MenuFrame extends Applet {
Frame f;
public void init(){
f = new MenuDemo("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);
}
}
Re: Menu not getting displayed!
Moved from New to Java
db
Re: Menu not getting displayed!
Why are you using the antiquated AWT when Swing has been around for more than 10 years?
db
Re: Menu not getting displayed!
Im trying to learn core java...I am still not familiar with swing...
Re: Menu not getting displayed!
Swing *is* part of core Java, and has been for more than 10 years.
Would you opt to learn driving in one of these?
db
Re: Menu not getting displayed!
I would surely do that .. but can you please let me know the fault with my program ?