import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ProductDatabase extends JFrame
{
private JFrame frame;
private JPanel textPanel;
private JPanel buttonPanel;
private JButton searchButton;
private JButton editButton;
private JTextArea textArea;
private JMenuBar menuBar;
private JMenu file;
private JMenu help;
private JMenuItem quit;
private JMenuItem about;
public ProductDatabase()
{
frame = new JFrame("Product Database");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
textPanel = new JPanel();
textArea = new JTextArea("Floor Coverings International \n Virginia Beach");
textPanel.add(textArea);
frame.add(textPanel);
buttonPanel = new JPanel();
searchButton = new JButton("Search");
searchButton.setVerticalTextPosition(AbstractButton.CENTER);
searchButton.setHorizontalTextPosition(AbstractButton.LEADING);
editButton = new JButton("Edit");
editButton.setVerticalTextPosition(AbstractButton.CENTER);
editButton.setHorizontalTextPosition(AbstractButton.TRAILING);
buttonPanel.add(searchButton);
buttonPanel.add(editButton);
frame.add(buttonPanel);
menuBar = new JMenuBar();
file = new JMenu("File");
menuBar.add(file);
quit = new JMenuItem("Quit");
file.add(quit);
help = new JMenu("Help");
menuBar.add(help);
about = new JMenuItem("About");
help.add(about);
frame.add(menuBar);
frame.setSize(400,400);
frame.setVisible(true);
}
private static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
ProductDatabase frame = new ProductDatabase();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
JMenuBar Issues
Page 1 of 1
JMenuBar Issues
#1
Posted 16 February 2009 - 02:26 PM
I am creating a product database for a flooring company. I am working on the first screen right now, it will have a menu bar, a panel with a text area, and a panel with two buttons. When I run the code, everything is good, except the menu bar does not show and I cannot figure out why. Any help would be greatly appreciated.
#2
Posted 16 February 2009 - 02:58 PM
Your layouts for your main frame and the panels are set to null... I would try a border layout and put the menu bar in the NORTH position. So your main panel should look something like this when you declare it...
JPanel panel = new JPanel(new BorderLayout());
This allows the layout to be set in the main frame. That way when you add components to the panel then you will be adding them to a specific location and nothing will overlap... Like this...
panel.add(menuBar, BorderLayout.NORTH);
Hope this helps. Cheers
JPanel panel = new JPanel(new BorderLayout());
This allows the layout to be set in the main frame. That way when you add components to the panel then you will be adding them to a specific location and nothing will overlap... Like this...
panel.add(menuBar, BorderLayout.NORTH);
Hope this helps. Cheers
#3
Posted 16 February 2009 - 03:19 PM
Your ProductDatabase is already a JFrame no need to add another one
You are not correctly using the BoxLayout this is not the way it should be used ... use another one it you be easier
JMenuBar can be added anywhere but if you want it in the op you have to use setJMenuBar
Your CreaeANdSHwGui is misleading... do not use a static method to create an instance of a GUI
Good luck
You are not correctly using the BoxLayout this is not the way it should be used ... use another one it you be easier
JMenuBar can be added anywhere but if you want it in the op you have to use setJMenuBar
Your CreaeANdSHwGui is misleading... do not use a static method to create an instance of a GUI
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ProductDatabase extends JFrame
{
private JPanel textPanel;
private JPanel buttonPanel;
private JButton searchButton;
private JButton editButton;
private JTextArea textArea;
private JMenuBar menuBar;
private JMenu file;
private JMenu help;
private JMenuItem quit;
private JMenuItem about;
public ProductDatabase()
{
super("Product Database");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
textPanel = new JPanel();
textArea = new JTextArea("Floor Coverings International \n Virginia Beach");
textPanel.add(textArea);
add(textPanel);
buttonPanel = new JPanel();
searchButton = new JButton("Search");
searchButton.setVerticalTextPosition(AbstractButton.CENTER);
searchButton.setHorizontalTextPosition(AbstractButton.LEADING);
editButton = new JButton("Edit");
editButton.setVerticalTextPosition(AbstractButton.CENTER);
editButton.setHorizontalTextPosition(AbstractButton.TRAILING);
buttonPanel.add(searchButton);
buttonPanel.add(editButton);
add(buttonPanel);
menuBar = new JMenuBar();
file = new JMenu("File");
menuBar.add(file);
quit = new JMenuItem("Quit");
file.add(quit);
help = new JMenu("Help");
menuBar.add(help);
about = new JMenuItem("About");
help.add(about);
setJMenuBar(menuBar);
setSize(400,400);
setVisible(true);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
ProductDatabase pd = new ProductDatabase();
}
});
}
}
Good luck
Page 1 of 1

Start a new topic
Add Reply




MultiQuote
| 


