Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problem in Swings

  Asked By: Kevin    Date: Aug 26    Category: Java    Views: 637
  

Below is the code for which I am getting the error, can anyone of you help me in

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FinalMenu extends JFrame
{
public FinalMenu()
{
Container contentPane = getContentPane();

JMenuBar mb = new JMenuBar();

JMenu master = new JMenu("Master");
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem general,employee,item,model;

master.add(general = new JMenuItem("General"));
master.add(employee = new JMenuItem("Employee"));
master.addSeparator();
master.add(item = new JMenuItem("Item"));
master.add(model = new JMenuItem("Model"));
master.addSeparator();
master.add(exitItem);



MyMenuHandler handler = new MyMenuHandler(this);
general.addActionListener(handler);
employee.addActionListener(handler);
item.addActionListener(handler);
model.addActionListener(handler);

MyWindowAdapter adapter = new MyWindowAdapter(this);
addWindowListener(adapter);

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

class MyMenuHandler implements ActionListener, ItemListener
{
FinalMenu mnuFrame;
public MyWindowAdapter(FinalMenu mnuFrame)
{
this.mnuFrame = mnuFrame;
}
public void actionPerformed(ActionEvent ae)
{
String arg = (String)ae.getActionCommand();
if (arg.equals("General"))
System.out.println("Want to call the General Master Form");
}
public void itemStateChanged(ItemEvent ie)
{
mnuFrame.repaint();
}
}

KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.ALT_MASK);

exitItem.setAccelerator(ks);

master.setMnemonic('M');
exitItem.setMnemonic(KeyEvent.VK_X);

mb.add(master);
setJMenuBar(mb);


}

public static void main(String args[])
{

Frame f = new FinalMenu();

f.setVisible(true);
}
}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Brian Ross     Answered On: Aug 26

After rearranging and slight modification of your code, here now
is your runnable code  

import javax.swing.*;import java.awt.*;import java.awt.event.*;
public class  FinalMenu extends JFrame
public  FinalMenu()
{ Container contentPane = getContentPane();
JMenuBar mb = new JMenuBar();
JMenu master  = new JMenu("Master");
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem general,employee,item,model;
master.add(general = new JMenuItem("General"));
master.add(employee = new JMenuItem("Employee"));
master.addSeparator();
master.add(item = new JMenuItem("Item"));
master.add(model = new JMenuItem("Model"));
master.addSeparator();
master.add(exitItem);
MyMenuHandler handler  = new MyMenuHandler(this);
general.addActionListener(handler);
employee.addActionListener(handler);
item.addActionListener(handler);
model.addActionListener(handler);
MyWindowAdapter adapter  = new MyWindowAdapter(this);
addWindowListener(adapter);
KeyStroke ks = KeyStroke.getKeyStroke
(KeyEvent.VK_X,Event.ALT_MASK);
exitItem.setAccelerator(ks);
master.setMnemonic('M');
exitItem.setMnemonic(KeyEvent.VK_X);
mb.add(master); setJMenuBar(mb); }

public static void  main(String args[])
{ Frame f = new FinalMenu();
f.setVisible(true); } }
class MyWindowAdapter extends WindowAdapter
{ FinalMenu mnuFrame;
public MyWindowAdapter(FinalMenu mnuFrame)
{ this.mnuFrame = mnuFrame; }
public void windowClosing(WindowEvent we)
{ System.exit(0); } }
class MyMenuHandler implements ActionListener, ItemListener
{ FinalMenu mnuFrame;
public MyMenuHandler(FinalMenu mnuFrame)
{ this.mnuFrame = mnuFrame; }
public void actionPerformed(ActionEvent ae)
{ String arg = (String)ae.getActionCommand();
if (arg.equals("General"))
System.out.println("Want to call  the general  Master Form"); }
public void itemStateChanged(ItemEvent ie)
{ mnuFrame.repaint(); } }

 
Answer #2    Answered By: Heidi Larson     Answered On: Aug 26

I think u might be getting "invalid method declaration" ERROR.
The problem  is in Inner class  "MyMenuHandler".
In this class,
public MyWindowAdapter (FinalMenu mnuFrame) is treated as method. and
this method have no return type. that why it show error.

Also, the way u have implemented ut class, It will not compile, even
if u replace "MyWindowAdapter " with "MyMenuHandler" in the above
method(which will make it constructor). This because of circular
dependency. Move ur inner classes out of class "FinalMenu". look at
following code.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FinalMenu
extends JFrame {
public FinalMenu() {
Container contentPane = getContentPane();

JMenuBar mb = new JMenuBar();

JMenu master  = new JMenu("Master");
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem general, employee, item, model;

master.add(general = new JMenuItem("General"));
master.add(employee = new JMenuItem("Employee"));
master.addSeparator();
master.add(item = new JMenuItem("Item"));
master.add(model = new JMenuItem("Model"));
master.addSeparator();
master.add(exitItem);

MyMenuHandler handler  = new MyMenuHandler(this);
general.addActionListener(handler);
employee.addActionListener(handler);
item.addActionListener(handler);
model.addActionListener(handler);

MyWindowAdapter adapter  = new MyWindowAdapter(this);
addWindowListener(adapter);

// class MyWindowAdapter
// extends WindowAdapter {
// FinalMenu mnuFrame;
// public  MyWindowAdapter(FinalMenu mnuFrame) {
// this.mnuFrame = mnuFrame;
// }
//
// public void  windowClosing(WindowEvent we) {
// mnuFrame.setVisible(false);
// }
// }
//
// class MyMenuHandler
// implements ActionListener, ItemListener {
// FinalMenu mnuFrame;
//
// public MyMenuHandler(FinalMenu mnuFrame) {
// this.mnuFrame = mnuFrame;
// }
//
//
// public void actionPerformed(ActionEvent ae) {
// String arg = (String) ae.getActionCommand();
// if (arg.equals("General")) {
// System.out.println("Want to call  the General
Master Form");
// }
// }
//
// public void itemStateChanged(ItemEvent ie) {
// mnuFrame.repaint();
// }
// }

KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_X,
Event.ALT_MASK);

exitItem.setAccelerator(ks);

master.setMnemonic('M');
exitItem.setMnemonic(KeyEvent.VK_X);

mb.add(master);
setJMenuBar(mb);

}

public static  void main(String args[]) {

Frame f = new FinalMenu();

f.setVisible(true);
}
}

class MyWindowAdapter
extends WindowAdapter {
FinalMenu mnuFrame;
public MyWindowAdapter(FinalMenu mnuFrame) {
this.mnuFrame = mnuFrame;
}

public void windowClosing(WindowEvent we) {
mnuFrame.setVisible(false);
}
}

class MyMenuHandler
implements ActionListener, ItemListener {
FinalMenu mnuFrame;

public MyMenuHandler(FinalMenu mnuFrame) {
this.mnuFrame = mnuFrame;
}

public void actionPerformed(ActionEvent ae) {
String arg = (String) ae.getActionCommand();
if (arg.equals("General")) {
System.out.println("Want to call the general  Master
Form");
}
}

public void itemStateChanged(ItemEvent ie) {
mnuFrame.repaint();
}
}

 
Answer #3    Answered By: Nagina Mian     Answered On: Aug 26

You have compilation errors in ur program. Remove them and ur program
should work fine.

 
Didn't find what you were looking for? Find more on Problem in Swings Or get search suggestion and latest updates.




Tagged: