Creating a menu using packages
I am creating a GUI package and just made a CreateFrame & CreateMenuBar class.
The CreateFrame class sets up a 800x600 Frame.
In that frame I want to create a menu with the class CreateMenuBar.
How can I display the menu in that frame, in other words how do I point to the frame I created with CreateFrame ?
[351 byte] By [
appo_neo] at [2007-9-26 1:47:20]

public static void main(String[] args) {
/**
* Setup the Graphical User Interface (GUI)
*
* @param appoTitle: title of the frame
* @param appoWidth: width of the frame
* @param appoHeight: height of the frame
* @param appoLaF: look and feel of the frame
*/
CreateFrame frame = new CreateFrame(appoTitle, appoWidth, appoHeight, appoLaF);
CreateMenuBar menu = new CreateMenuBar(appoWidth, 30);
frame.setJMenuBar(menu);
CreateFrame creates a frame -> this works ok !
CreateMenuBar should create the menubar, compiled ok !
I gey an error at the frame.setJMenuBar(menu); part.
What could I be doing wrong ?
I would like to use just these following lines in my main class to create the frame with the menu :
CreateFrame frame = new CreateFrame(appoTitle, appoWidth, appoHeight, appoLaF);
CreateMenuBar menu = new CreateMenuBar(appoWidth, 30);
here is the code for the CreateMenuBar :
package appo.gui;
/** 2001 3me me ia
*
* @author A. Postma aka aPPo neo
* -
* CreateMenuBar
* -
* This class will create & show a default new frame
*
* version 0.01 24.07.2001 - Create a basic menu bar
*/
// import Swing packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.border.Border;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
// import Abstract Window Toolkit package
// Create and show the GUI
public class CreateMenuBar extends JMenuBar {
// Border references(top, left, bottom, right)
static Border BorderUp= BorderFactory.createEmptyBorder(4,5,2,5);
static Border BorderDown= BorderFactory.createEmptyBorder(2,5,4,5);
static Border blackLine= BorderFactory.createLineBorder(Color.black);
static Border etchedLine= BorderFactory.createEtchedBorder();
static Border raisedBevel = BorderFactory.createRaisedBevelBorder();
static Border loweredBevel = BorderFactory.createLoweredBevelBorder();
static Border menuBorder= BorderFactory.createCompoundBorder
(BorderUp, etchedLine);
static Border panelBorder = BorderFactory.createCompoundBorder
(BorderDown, loweredBevel);
static JMenuBar menuBar = new JMenuBar();
/**
* Create a menu bar
*
* @param menuWidth: the width of the menu
* @param menuHeight: the height of the menu
*/
public CreateMenuBar(int menuWidth, int menuHeight) {
menuBar.setOpaque(true);
menuBar.setBorder(menuBorder);
menuBar.setPreferredSize(new Dimension(menuWidth, 30));
setJMenuBar(menuBar);
}
}
How do I have to change this or add code to it to get it working ? The problem is the setJMenuBar. I guess I have to refer to the existing frame, but how do I do that ? If I understand how this works I can rapidly start coding the rest of the package ... I just have to understand the basics of referring to the frame created with CreateFrame.
thanks again (in advance)