A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
Have all menuitems in own class
Torvald Helmer
Greenhorn
Joined: Oct 26, 2005
Posts: 8
posted
Aug 20, 2007 14:20:00
0
I have a class Frame like this:
class Frame extends JFrame /*implements ActionListener*/ { //Menu m = new Menu(); public Frame() { setTitle(""); setSize(500,300); setLocationRelativeTo(null); JMenuBar mB = new JMenuBar(); setJMenuBar(mB); }
I want to have a class Menu like this, with all
menuitems:
class Menu { JMenu fileMenu = new JMenu("File"); public Menu() { add(fileMenu); } }
But this not work. What am I doing wrong? Does anyone
have a idea on how to do this?
When I try to add menutiem in the Frame-class, it works
fine. But I want to have all related to the menu in a
separate file.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
Aug 20, 2007 15:14:00
0
import java.awt.*; import javax.swing.*; class MyFrame extends JFrame { public MyFrame() { setTitle(""); setSize(500,300); setLocationRelativeTo(null); setJMenuBar(new MyMenuBar()); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new MyFrame().setVisible(true); } }); } } class MyMenuBar extends JMenuBar { public MyMenuBar() { JMenu fileMenu = new JMenu("File"); fileMenu.add(new JMenuItem("Open")); add(fileMenu); } }
Torvald Helmer
Greenhorn
Joined: Oct 26, 2005
Posts: 8
posted
Aug 20, 2007 15:33:00
0
Thanks!
[ August 20, 2007: Message edited by: Torvald Helmer ]
I agree. Here's the link:
http://aspose.com/file-tools
subject: Have all menuitems in own class
Similar Threads
OO Question
Menus and replacing the prompts fr JFrame
FBN: Design Help Required for GUI
Creating one class from another
JFrame and JMenu
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter