-
September 30th, 2006, 08:32 PM
#1
Member
JavaMenuBar ?? won't show up
I have a programming assignment where I need to add a JMenuBar at the top with very basic functions
File - Query - Help
I believe all of my code is correct, but no menu bar shows up when I try to:
frame.setJMenuBar(JMB);
I've been looking on the net and the syntax seems to be correct, but like I said...no bar at the top..
Any suggestions?
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyFrame extends JFrame implements ActionListener
{
//Create new objects
JFrame frame;
JPanel p1, p2, p3;
JTextField txtSSN, txtFname, txtLname, txtMname, txtAddress, txtCity, txtState, txtZip;
JLabel lblSSN, lblFname, lblLname, lblMname, lblAddress, lblCity, lblState, lblZip;
JButton btnOK, btnCancel, btnExit;
JLabel dispLblSSN, dispLblName, dispLblAddress, dispLblCSZ;
String Fname, Lname, Mname, SSN, Address, State, City, Zip;
// From Assignment 2
JMenuBar JMB;
JMenu menuFile, menuQuery, menuHelp;
JMenuItem menuFileSave, menuFileClear, menuFileExit;
JMenuItem menuQueryExe;
JMenuItem menuHelpAbout;
public MyFrame()
{
setTitle("Jeremy's GUI");
//Initialize all objects
Container cp = getContentPane();
dispLblSSN = new JLabel("");
dispLblSSN.setForeground(Color.gray);
dispLblName = new JLabel("");
dispLblName.setForeground(Color.gray);
dispLblAddress = new JLabel("");
dispLblAddress.setForeground(Color.gray);
dispLblCSZ = new JLabel("");
dispLblCSZ.setForeground(Color.gray);
lblSSN = new JLabel("<html><font color='white'>SSN:</font></html>");
lblFname = new JLabel("First Name:");
lblFname.setForeground(Color.white);
lblLname = new JLabel("Last Name:");
lblLname.setForeground(Color.white);
lblMname = new JLabel("Middle Initial:");
lblMname.setForeground(Color.white);
lblAddress = new JLabel("Address:");
lblAddress.setForeground(Color.white);
lblCity = new JLabel("City:");
lblCity.setForeground(Color.white);
lblState = new JLabel("State:");
lblState.setForeground(Color.white);
lblZip = new JLabel("Zip Code:");
lblZip.setForeground(Color.white);
txtSSN = new JTextField(10);
txtFname = new JTextField(20);
txtLname = new JTextField(20);
txtMname = new JTextField(2);
txtAddress = new JTextField(20);
txtCity = new JTextField(7);
txtState = new JTextField(3);
txtZip = new JTextField(5);
btnOK = new JButton("OK");
btnOK.addActionListener(this);
btnOK.setBackground(Color.black);
btnOK.setForeground(Color.red);
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(this);
btnCancel.setBackground(Color.black);
btnCancel.setForeground(Color.red);
btnExit = new JButton("Exit");
btnExit.addActionListener(this);
btnExit.setBackground(Color.black);
btnExit.setForeground(Color.red);
//Assignement 2
JMB = new JMenuBar();
frame = new JFrame("Menu");
menuFile = new JMenu("File");
menuFile.setMnemonic('F');
menuQuery = new JMenu("Query");
menuQuery.setMnemonic('Q');
menuHelp = new JMenu("Help");
menuHelp.setMnemonic('H');
menuFileSave = new JMenuItem("Save");
menuFileSave.setMnemonic('S');
menuFileSave.addActionListener(this);
menuFileClear = new JMenuItem("Clear");
menuFileClear.setMnemonic('C');
menuFileClear.addActionListener(this);
menuFileExit = new JMenuItem("Exit");
menuFileExit.setMnemonic('X');
menuFileExit.addActionListener(this);
menuQueryExe = new JMenuItem("Execute");
menuQueryExe.setMnemonic('X');
menuQueryExe.addActionListener(this);
menuHelpAbout = new JMenuItem("About");
menuHelpAbout.setMnemonic('A');
menuHelpAbout.addActionListener(this);
menuFile.add(menuFileSave);
menuFile.add(menuFileClear);
menuFile.add(menuFileExit);
menuQuery.add(menuQueryExe);
menuHelp.add(menuHelpAbout);
JMB.add(menuFile);
JMB.add(menuQuery);
JMB.add(menuHelp);
frame.setJMenuBar(JMB);
//Panel 1 - Information
p1 = new JPanel();
p1.setLayout(new GridLayout(10,1));
p1.setBackground(Color.black);
p1.add(lblFname);
p1.add(txtFname);
p1.add(lblMname);
p1.add(txtMname);
p1.add(lblLname);
p1.add(txtLname);
p1.add(lblSSN);
p1.add(txtSSN);
p1.add(lblAddress);
p1.add(txtAddress);
p1.add(lblCity);
p1.add(txtCity);
p1.add(lblState);
p1.add(txtState);
p1.add(lblZip);
p1.add(txtZip);
//Panel 2 - Display Info
p2 = new JPanel();
p2.setLayout(new GridLayout(4,1));
p2.setBackground(Color.black);
p2.add(dispLblSSN);
p2.add(dispLblName);
p2.add(dispLblAddress);
p2.add(dispLblCSZ);
//Panel 3 - Buttons
p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
p3.setBackground(Color.black);
p3.add(btnOK);
p3.add(btnCancel);
p3.add(btnExit);
// Add to Container
cp.add(p1, BorderLayout.NORTH);
cp.add(p2, BorderLayout.SOUTH);
cp.add(p3, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==btnOK)
{
//Take in value
Fname = (txtFname.getText());
Mname = (txtMname.getText());
Lname = (txtLname.getText());
SSN = (txtSSN.getText());
Address = (txtAddress.getText());
City = (txtCity.getText());
State = (txtState.getText());
Zip = (txtZip.getText());
//Set display label
dispLblSSN.setText(" SSN:\t" + SSN);
dispLblName.setText(" Name:\t" + Fname + " " + Mname + " " + Lname);
dispLblAddress.setText(" Address:\t" + Address);
dispLblCSZ.setText(" City/State/Zip:\t" + City + ", " + State + " " + Zip);
//Clear
txtFname.setText("");
txtLname.setText("");
txtMname.setText("");
txtSSN.setText("");
txtAddress.setText("");
txtCity.setText("");
txtState.setText("");
txtZip.setText("");
txtFname.requestFocus();
}
if(evt.getSource()==btnCancel)
{
//Clear
txtFname.setText("");
txtLname.setText("");
txtMname.setText("");
txtSSN.setText("");
txtAddress.setText("");
txtCity.setText("");
txtState.setText("");
txtZip.setText("");
dispLblSSN.setText("");
dispLblName.setText("");
dispLblAddress.setText("");
dispLblCSZ.setText("");
txtFname.requestFocus();
}
if(evt.getSource()==btnExit)
{
if(JOptionPane.showConfirmDialog(null,"Are you sure you want to Exit?")== JOptionPane.YES_OPTION) System.exit(0);
}
if(evt.getSource()==menuFileSave)
{
// Save
}
if(evt.getSource()==menuFileExit)
{
if(JOptionPane.showConfirmDialog(null,"Are you sure you want to Exit?")== JOptionPane.YES_OPTION) System.exit(0);
}
if(evt.getSource()==menuQueryExe)
{
// Execute
}
if(evt.getSource()==menuHelpAbout)
{
// Display About Information
}
}
public static void main (String []args)
{
MyFrame my = new MyFrame();
my.setSize(600,600);
my.show();
}
}
EDIT: NEVERMIND FIGURED IT OUT
setJMenuBar(JMB);
-
September 30th, 2006, 10:57 PM
#2
You're making a new frame and adding the menubar to the new frame. Instead you should add it to the original frame. Your class extends JFrame, which means it is a frame. Instead of frame.setJMenuBar(JMB);, just do setJMenuBar(JMB);
I'm pretty sure that will do it for you. You should organize your code a little better . Good luck.
Edit:
Just saw your edit. Haha. Did you remove that extra frame when you figured that out? It's completely pointless .
-
October 5th, 2006, 02:28 AM
#3
Member
haha yeah, sorry about that...wasnt soon after that I figured it out. Thanks for the help though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|