By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

Need help in my code

crystal2005
P: 44
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.

[HTML]package gui_layout_BM;

import javax.swing.JFrame;

public class layoutDriver {

public static void main(String[] args) {
JFrame frame = new JFrame("Bovinetine Maker Simulator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setJMenuBar(new MenuBarPanel().MainMenuBar()); //This line doesn't seem to call the function
frame.setVisible(true);
frame.setSize(480,320);

}

}[/HTML]

And another function
[HTML]
package gui_layout_BM;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

class MenuBarPanel extends JMenuBar{

public JMenuBar MainMenuBar() {
JMenuBar menu = new JMenuBar();

menu.add(CreateFileMenu());
menu.add(new JSeparator());
menu.add(CreateActionMenu());

return menu;
}

public JMenu CreateFileMenu() {
JMenu filemenu = new JMenu("File");

JMenuItem fileItem1 = new JMenuItem("Reset");
JMenuItem fileItem2 = new JMenuItem("Exit");

filemenu.add(fileItem1);
filemenu.add(fileItem2);

return filemenu;

}

public JMenu CreateActionMenu() {
JMenu actionmenu = new JMenu("Action");
//menu.setMnemonic("A");

JMenuItem actionItem1 = new JMenuItem("Add Pot");
JMenuItem actionItem2 = new JMenuItem("Add Ground");
JMenuItem actionItem3 = new JMenuItem("Add Water");
actionItem3.add(new JSeparator());
JMenuItem actionItem4 = new JMenuItem("Remove Pot");
JMenuItem actionItem5 = new JMenuItem("Remove Ground");
JMenuItem actionItem6 = new JMenuItem("Remove Water");
actionItem6.add(new JSeparator());
JMenuItem actionItem7 = new JMenuItem("Brew");

actionmenu.add(actionItem1);
actionmenu.add(actionItem2);
actionmenu.add(actionItem3);
actionmenu.add(actionItem4);
actionmenu.add(actionItem5);
actionmenu.add(actionItem6);
actionmenu.add(actionItem7);

return actionmenu;

}

}
[/HTML]

I hope there is no fatal error in my code.

Thank you for any kind of help.
May 12 '08 #1
Share this Question
Share on Google+
6 Replies


Expert 10K+
P: 11,448
Look at your menu bar class:

Expand|Select|Wrap|Line Numbers
  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.     ...
  13.  
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
May 12 '08 #2

crystal2005
P: 44
Look at your menu bar class:

Expand|Select|Wrap|Line Numbers
  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.     ...
  13.  
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
May 13 '08 #3

crystal2005
P: 44
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.



Expand|Select|Wrap|Line Numbers
  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_CLOSE);
  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. }
  161.  
  162.  
May 15 '08 #4

Expert 10K+
P: 11,448
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
May 15 '08 #5

crystal2005
P: 44
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.
May 16 '08 #6

Expert 10K+
P: 11,448
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
May 16 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.