Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Need help in my code

Question posted by: crystal2005 (Newbie) on May 12th, 2008 05:46 AM
Hi there, I need some help to correct my code. I would like to create a menu bar with the implementation of OO. So, i think i can edit and change it easyly if i use some classes.

Code: ( text )
  1. package gui_layout_BM;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class layoutDriver {
  6.  
  7.     public static void main(String[] args) {
  8.         JFrame frame = new JFrame("Bovinetine Maker Simulator");
  9.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS  E);
  10.         frame.setJMenuBar(new MenuBarPanel().MainMenuBar()); //This line doesn't seem to call the function
  11.         frame.setVisible(true);
  12.         frame.setSize(480,320);
  13.  
  14.     }
  15.  
  16. }


And another function
Code: ( text )
  1. package gui_layout_BM;
  2.  
  3. import javax.swing.JMenu;
  4. import javax.swing.JMenuBar;
  5. import javax.swing.JMenuItem;
  6. import javax.swing.JSeparator;
  7.  
  8. class MenuBarPanel extends JMenuBar{
  9.    
  10.     public JMenuBar MainMenuBar() {
  11.         JMenuBar menu = new JMenuBar();
  12.        
  13.         menu.add(CreateFileMenu());
  14.         menu.add(new JSeparator());
  15.         menu.add(CreateActionMenu());
  16.        
  17.         return menu;
  18.     }
  19.    
  20.     public JMenu CreateFileMenu() {
  21.         JMenu filemenu = new JMenu("File");
  22.        
  23.         JMenuItem fileItem1 = new JMenuItem("Reset");
  24.         JMenuItem fileItem2 = new JMenuItem("Exit");
  25.        
  26.         filemenu.add(fileItem1);
  27.         filemenu.add(fileItem2);
  28.         
  29.         return filemenu;
  30.        
  31.     }
  32.    
  33.     public JMenu CreateActionMenu() {
  34.         JMenu actionmenu = new JMenu("Action");
  35.         //menu.setMnemonic("A");
  36.        
  37.         JMenuItem actionItem1 = new JMenuItem("Add Pot");
  38.         JMenuItem actionItem2 = new JMenuItem("Add Ground");
  39.         JMenuItem actionItem3 = new JMenuItem("Add Water");
  40.         actionItem3.add(new JSeparator());
  41.         JMenuItem actionItem4 = new JMenuItem("Remove Pot");
  42.         JMenuItem actionItem5 = new JMenuItem("Remove Ground");
  43.         JMenuItem actionItem6 = new JMenuItem("Remove Water");
  44.         actionItem6.add(new JSeparator());
  45.         JMenuItem actionItem7 = new JMenuItem("Brew");
  46.        
  47.         actionmenu.add(actionItem1);
  48.         actionmenu.add(actionItem2);
  49.         actionmenu.add(actionItem3);
  50.         actionmenu.add(actionItem4);
  51.         actionmenu.add(actionItem5);
  52.         actionmenu.add(actionItem6);
  53.         actionmenu.add(actionItem7);
  54.         
  55.         return actionmenu;
  56.        
  57.     }
  58.  
  59. }


I hope there is no fatal error in my code.

Thank you for any kind of help.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
JosAH's Avatar
JosAH
Chief Editor
6,797 Posts
May 12th, 2008
07:22 AM
#2

Re: Need help in my code
Look at your menu bar class:

Code: ( text )
  1. class MenuBarPanel extends JMenuBar{
  2.    
  3.     public JMenuBar MainMenuBar() {
  4.         JMenuBar menu = new JMenuBar();
  5.        
  6.         menu.add(CreateFileMenu());
  7.         menu.add(new JSeparator());
  8.         menu.add(CreateActionMenu());
  9.        
  10.         return menu;
  11.     }
  12.     ...


Your class *is a* JMenuBar because you extend from it but it also *has a*
JMenuBar because it makes one in the MainMenuBar method. You probably
want to populate a MenuBarPanel from its constructor. btw, is that a correct
name for your menu bar, i.e. is it really a panel? Also: only names of classes,
interfaces and enums start with a capital letter by convention. All the others
start with a lowercase letter and most of the time the name of a method has
a verb in it. It really improves readability.

kind regards,

Jos

Reply
crystal2005's Avatar
crystal2005
Newbie
26 Posts
May 13th, 2008
04:43 AM
#3

Re: Need help in my code
Quote:
Originally Posted by JosAH
Look at your menu bar class:

Code: ( text )
  1. class MenuBarPanel extends JMenuBar{
  2.    
  3.     public JMenuBar MainMenuBar() {
  4.         JMenuBar menu = new JMenuBar();
  5.        
  6.         menu.add(CreateFileMenu());
  7.         menu.add(new JSeparator());
  8.         menu.add(CreateActionMenu());
  9.        
  10.         return menu;
  11.     }
  12.     ...


Your class *is a* JMenuBar because you extend from it but it also *has a*
JMenuBar because it makes one in the MainMenuBar method. You probably
want to populate a MenuBarPanel from its constructor. btw, is that a correct
name for your menu bar, i.e. is it really a panel? Also: only names of classes,
interfaces and enums start with a capital letter by convention. All the others
start with a lowercase letter and most of the time the name of a method has
a verb in it. It really improves readability.

kind regards,

Jos



You're right, I want to populate it to make it easier to be called from main class. MenuBarPanel is just a name of class. Thanks to remind me about the naming, i almost forget about it.

I'll figure out again, if i get something, i'll keep posting in this thread. Thanks

Reply
crystal2005's Avatar
crystal2005
Newbie
26 Posts
May 15th, 2008
01:55 AM
#4

Re: Need help in my code
Hi,

I have changed my code almost completely recently, And now I have problem in Layout Interface. It looks like a nice interface, but it doesn't. When i resize the frame, some buttons aren't fixed. Could someone tell me what is the most appropriate layout for my interface??

This layout has no action listener yet.

Thank you.



Code: ( text )
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. public class MenuLook {
  7.  
  8.     public JMenuBar createMenuBar() {
  9.         JMenuBar menuBar = new JMenuBar(); //Create the menu bar.
  10.  
  11.         menuBar.add(createFileMenu());
  12.         menuBar.add(createActionMenu());
  13.         menuBar.add(createHelpMenu());
  14.  
  15.         return menuBar;
  16.     }
  17.    
  18.     public JMenu createFileMenu() {
  19.         JMenu file = new JMenu("File");
  20.         file.setMnemonic(KeyEvent.VK_F);
  21.        
  22.         //Group of 'File' JMenuItems
  23.         JMenuItem fileItem1 = new JMenuItem("Reset", KeyEvent.VK_R);
  24.         JMenuItem fileItem2 = new JMenuItem("Exit", KeyEvent.VK_R);
  25.  
  26.         file.add(fileItem1);
  27.         file.addSeparator();
  28.         file.add(fileItem2);
  29.        
  30.         return file;       
  31.     }
  32.    
  33.     public JMenu createActionMenu() {
  34.         JMenu action = new JMenu("Action");
  35.         action.setMnemonic(KeyEvent.VK_A);
  36.        
  37.         //Group of 'Action' JMenuItems
  38.         JMenuItem actionItem1 = new JMenuItem("Add Pot");
  39.         JMenuItem actionItem2 = new JMenuItem("Add Ground");
  40.         JMenuItem actionItem3 = new JMenuItem("Add Water");
  41.         JMenuItem actionItem4 = new JMenuItem("Remove Pot");
  42.         JMenuItem actionItem5 = new JMenuItem("Remove Ground");
  43.         JMenuItem actionItem6 = new JMenuItem("Remove Water");
  44.         JMenuItem actionItem7 = new JMenuItem("Brew");
  45.        
  46.         action.add(actionItem1);
  47.         action.add(actionItem2);
  48.         action.add(actionItem3);
  49.         action.addSeparator();
  50.         action.add(actionItem4);
  51.         action.add(actionItem5);
  52.         action.add(actionItem6);
  53.         action.addSeparator();
  54.         action.add(actionItem7);
  55.        
  56.         return action;     
  57.     }
  58.    
  59.     public JMenu createHelpMenu() {
  60.         JMenu help = new JMenu("Help");
  61.         help.setMnemonic(KeyEvent.VK_H);
  62.        
  63.         //Group of 'help' JMenuItems
  64.         JMenuItem helpItem1 = new JMenuItem("About BM Simulator");
  65.        
  66.         help.add(helpItem1);
  67.        
  68.         return help;       
  69.     }
  70.  
  71.     public Container createContentPane() {
  72.         //Create the content-pane-to-be.
  73.         JPanel contentPane = new JPanel(new BorderLayout());
  74.         //contentPane.setOpaque(true);
  75.  
  76.         //Create a scrolled text area.
  77.         JTextArea output = new JTextArea(5, 30);
  78.         output.setEditable(true);
  79.         JScrollPane scrollPane = new JScrollPane(output);
  80.  
  81.         //Add the text area to the content pane.
  82.         contentPane.add(scrollPane, BorderLayout.CENTER);     
  83.        
  84.         contentPane.add(createWestPanel(), BorderLayout.WEST);       
  85.         contentPane.add(createSouthPanel(), BorderLayout.SOUTH);
  86.  
  87.         return contentPane;
  88.     }
  89.    
  90.     public JPanel createWestPanel() {
  91.         JPanel westPanel = new JPanel(new BorderLayout()); //Probably not appropriate Layout
  92.        
  93.         //Panels inside 'westPanel'
  94.         JPanel addActionPanel = new JPanel(new GridLayout(3,1,10,10));
  95.         JPanel removeActionPanel = new JPanel(new GridLayout(3,1,10,10));
  96.         JPanel brewActionPanel = new JPanel(new GridLayout(1,1,10,10));
  97.        
  98.         TitledBorder addActionBorder = new TitledBorder("Add Action");
  99.         TitledBorder removeActionBorder = new TitledBorder("Remove Action");
  100.         TitledBorder brewActionBorder = new TitledBorder("Brew Action");
  101.        
  102.         JButton b1 = new JButton("Add Pot");
  103.         JButton b2 = new JButton("Add Ground");
  104.         JButton b3 = new JButton("Add Water");
  105.         JButton b4 = new JButton("Remove Pot");
  106.         JButton b5 = new JButton("Remove Ground");
  107.         JButton b6 = new JButton("Remove Water");
  108.         JButton b7 = new JButton("Brew");
  109.        
  110.         addActionPanel.add(b1);
  111.         addActionPanel.add(b2);
  112.         addActionPanel.add(b3);
  113.         addActionPanel.setBorder(addActionBorder);
  114.         removeActionPanel.add(b4);
  115.         removeActionPanel.add(b5);
  116.         removeActionPanel.add(b6);
  117.         removeActionPanel.setBorder(removeActionBorder);
  118.         brewActionPanel.add(b7);
  119.         brewActionPanel.setBorder(brewActionBorder);
  120.        
  121.         //Assign Panels to 'westPanel'
  122.         westPanel.add(addActionPanel, BorderLayout.PAGE_START);
  123.         westPanel.add(removeActionPanel, BorderLayout.CENTER); //Problem With this line
  124.         westPanel.add(brewActionPanel, BorderLayout.PAGE_END);
  125.        
  126.         return westPanel;
  127.     }
  128.    
  129.     public JPanel createSouthPanel() {
  130.         JPanel southPanel = new JPanel();
  131.        
  132.         JLabel statusBar = new JLabel("This test");
  133.         statusBar.setText("Testing");
  134.        
  135.         southPanel.add(statusBar);
  136.        
  137.         return southPanel;
  138.        
  139.     }
  140.  
  141.     private static void createAndShowGUI() {
  142.         //Create and set up the window.
  143.         JFrame frame = new JFrame("Bovinetine Maker Simulator");
  144.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS  E);
  145.  
  146.         //Create and set up the content pane.
  147.         MenuLook demo = new MenuLook();
  148.         frame.setJMenuBar(demo.createMenuBar());
  149.         frame.setContentPane(demo.createContentPane());
  150.  
  151.         //Display the window.
  152.         frame.setSize(640,380);
  153.         frame.setVisible(true);
  154.     }
  155.  
  156.     public static void main(String[] args) {
  157.         createAndShowGUI();
  158.     }
  159.    
  160. }

Reply
JosAH's Avatar
JosAH
Chief Editor
6,797 Posts
May 15th, 2008
06:43 AM
#5

Re: Need help in my code
Quote:
Originally Posted by crystal2005
Hi,

I have changed my code almost completely recently, And now I have problem in Layout Interface. It looks like a nice interface, but it doesn't. When i resize the frame, some buttons aren't fixed. Could someone tell me what is the most appropriate layout for my interface??


Can you tell in plain English what you want it to look and what it doesn't do now?
I browsed a bit through all that code and it looks sensible overall.

kind regards,

Jos

Reply
crystal2005's Avatar
crystal2005
Newbie
26 Posts
May 16th, 2008
12:21 AM
#6

Re: Need help in my code
This is what basically my interface looks like




But when i try to resize it, the center panel of my west-panel seems to resize itself and become like this




What i expect is, when i resize the window i want all the button to be fixed. So that it would be a nicer look.

Thanks for any advice.

Reply
JosAH's Avatar
JosAH
Chief Editor
6,797 Posts
May 16th, 2008
07:13 AM
#7

Re: Need help in my code
For that left/west button panel you can use a SpringLayout; the API documentation
of that class points to a nice tutorial with examples.

kind regards,

Jos

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,098 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Java Forum Contributors