hi...
i am new to this community, lately i was looking for some help in my code and this website came to my rescue in many topic.
i am just implementing the menubar in swing, can somebody help me in my code.
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
public class FrameExample extends JFrame {
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu optionMenu;
private JMenuItem newFileItem;
private JMenuItem openFileItem;
private JMenuItem exitItem;
public FrameExample()
{
menuBar = new JMenuBar();
setJMenuBar(menuBar);
MyMenuHandler handler = new MyMenuHandler();
newFileItem=new JMenuItem();
newFileItem.setText("New");
newFileItem.setToolTipText("Click to open new file");
newFileItem.setActionCommand("new");
newFileItem.addActionListener(handler(this));
openFileItem=new JMenuItem();
openFileItem.setText("Open");
openFileItem.setToolTipText("Click to open existing file");
openFileItem.setActionCommand("open");
openFileItem.addActionListener(handler(this));
exitItem=new JMenuItem();
exitItem.setText("Exit");
exitItem.setToolTipText("Click to Exit");
exitItem.setActionCommand("exit");
exitItem.addActionListener(handler(this));
fileMenu=new JMenu();
fileMenu.add(newFileItem);
fileMenu.add(openFileItem);
fileMenu.add(exitItem);
addWindowListener(new MyWindowAdapter());
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public static void main(String[] args)
{
FrameExample frame=new FrameExample();
frame.setSize(200,200);
frame.setTitle("A Frame");
frame.setVisible(true);
}
class MyMenuHandler extends ActionListener{
FrameExample f;
public void actionPerformed(ActionEvent ae)
{
String arg=(String)ae.getActionCommand();
if(arg.equals("new"))
{
JOptionPane.showMessageDialog(FrameExample.this, "New File Chosen", JOptionPane.CANCEL_OPTION);
}
}
}
}
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we)
{
int result = JOptionPane.showConfirmDialog( FrameExample.this, "Do you really Want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION );
if( result == JOptionPane.YES_OPTION )
{
System.exit( 0 );
}
}
}